Author: weaverryan
Date: 2010-01-30 18:55:55 +0100 (Sat, 30 Jan 2010)
New Revision: 27331
Added:
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/templates/objectsSuccess.php
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/js/objects.js
Modified:
plugins/sfSympalPlugin/trunk/config/app.yml
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAssetsPlugin/lib/sfSympalAssetReplacer.class.php
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/config/routing.yml
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/lib/Basesympal_editorActions.class.php
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/templates/_editor.php
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/css/editor.css
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/js/editor.js
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalRenderingPlugin/config/sfSympalRenderingPluginConfiguration.class.php
Log:
[1.4][sfSympalPlugin][1.0] Adding the idea of being able to include and render
"objects". This extends the current idea of including "links" (relations to
sfSympalContent rendered as links) and "assets" (relations to sfSympalAsset
rendered however that asset type should be rendered).
This allows you to define any other type of thing to be replaced. Objects are
selected in the same way that links and assets are selected and then are
rendered via a partial.
I do have a few concerns about the location of some code - code without a home
:) - and efficiency since we're always querying out for objects in these cases.
Modified: plugins/sfSympalPlugin/trunk/config/app.yml
===================================================================
--- plugins/sfSympalPlugin/trunk/config/app.yml 2010-01-30 16:32:03 UTC (rev
27330)
+++ plugins/sfSympalPlugin/trunk/config/app.yml 2010-01-30 17:55:55 UTC (rev
27331)
@@ -203,6 +203,14 @@
form_renderer: sympal_edit_slot/slot_editor_renderer
widget_options:
config: 'handle_event_callback : "sympalHandleTinyMCEEvent"'
+
+ # configure doctrine objects that can be inserted into slots
+ content_slot_objects: {}
+ #MyObjectSlot:
+ # label: Label of My Object Slot
+ # class: MyObjectSlotClass
+ # template: myModule/template
+
# Enable and disable the check for upgrades via the web when on the Sympal
dashboard
check_for_upgrades_on_dashboard: false
Modified:
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAssetsPlugin/lib/sfSympalAssetReplacer.class.php
===================================================================
---
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAssetsPlugin/lib/sfSympalAssetReplacer.class.php
2010-01-30 16:32:03 UTC (rev 27330)
+++
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalAssetsPlugin/lib/sfSympalAssetReplacer.class.php
2010-01-30 17:55:55 UTC (rev 27331)
@@ -16,6 +16,16 @@
private
$_slot,
$_content;
+
+ /**
+ * Array of possible keys (e.g. asset, link) that should be searched
+ * for along with the callback that should be called to make that replacement
+ *
+ * 'link' => array('sfSympalAssetReplacer' => '_replaceLinks')
+ * 'asset' => array('sfSympalAssetReplacer' => '_replaceAssets')
+ */
+ protected
+ $_replacementMap = null;
public function __construct(sfSympalContentSlot $slot)
{
@@ -26,6 +36,7 @@
public static function listenToFilterSlotContent(sfEvent $event, $content)
{
$replacer = new self($event->getSubject());
+
return $replacer->replace($content);
}
@@ -35,22 +46,40 @@
{
$ids = $parsed['ids'];
$replacements = $parsed['replacements'];
-
- if (isset($ids['asset']) && $ids['asset'])
+
+ $replacementKeys = array_keys($ids);
+ // iterate through all of the replacement types
+ foreach($replacementKeys as $key)
{
- $content = $this->_replaceAssets($ids['asset'],
$replacements['asset'], $content);
+ $content = $this->handleReplacementCallback($key, $ids[$key],
$replacements[$key], $content);
}
- if (isset($ids['link']) && $ids['link'])
- {
- $content = $this->_replaceLinks($ids['link'], $replacements['link'],
$content);
- }
}
+
return $content;
}
-
+
+ /**
+ * Handles the callbacks for each type of replacement and returns the
+ * filtered content
+ */
+ protected function handleReplacementCallback($key, $ids, $replacements,
$content)
+ {
+ $map = $this->getReplacementMap();
+ $callback = $map[$key];
+
+ return call_user_func($callback, $key, $ids, $replacements, $content,
$this);
+ }
+
+ /**
+ * Searches through the content and extracts out any matches. The return
+ * value is a formatted array of what needs to be replaced
+ */
private function _parseSyntaxes($content)
{
- preg_match_all("/\[(link|asset):(.*?)\]/", $content, $matches);
+ // create the replacement string (e.g. link|asset|myObject)
+ $replacementString = implode('|', array_keys($this->getReplacementMap()));
+
+ preg_match_all("/\[($replacementString):(.*?)\]/", $content, $matches);
if (isset($matches[0]) && $matches[0])
{
@@ -78,10 +107,13 @@
return false;
}
}
-
- private function _replaceAssets($ids, $replacements, $content)
+
+ /**
+ * Handles the replacement of "asset" keys
+ */
+ public static function _replaceAssets($key, $ids, $replacements, $content,
sfSympalAssetReplacer $replacer)
{
- if (array_diff($ids, $this->_content->Assets->getPrimaryKeys()) ||
array_diff($this->_content->Assets->getPrimaryKeys(), $ids))
+ if (array_diff($ids, $replacer->getContent()->Assets->getPrimaryKeys()) ||
array_diff($replacer->getContent()->Assets->getPrimaryKeys(), $ids))
{
$assetObjects = Doctrine_Core::getTable('sfSympalAsset')
->createQuery()
@@ -90,12 +122,12 @@
->execute();
foreach ($assetObjects as $assetObject)
{
- $this->_content->Assets[] = $assetObject;
+ $replacer->getContent()->Assets[] = $assetObject;
}
- $this->_content->save();
+ $replacer->getContent()->save();
}
- $assetObjects = $this->_buildObjects($this->_content->Assets);
+ $assetObjects = self::_buildObjects($replacer->getContent()->Assets);
foreach ($replacements as $replacement)
{
$assetObject = $assetObjects[$replacement['id']];
@@ -103,10 +135,13 @@
}
return $content;
}
-
- private function _replaceLinks($ids, $replacements, $content)
+
+ /**
+ * Handles the replacement of "link" keys
+ */
+ public static function _replaceLinks($key, $ids, $replacements, $content,
sfSympalAssetReplacer $replacer)
{
- if (array_diff($ids, $this->_content->Links->getPrimaryKeys()) ||
array_diff($this->_content->Links->getPrimaryKeys(), $ids))
+ if (array_diff($ids, $replacer->getContent()->Links->getPrimaryKeys()) ||
array_diff($replacer->getContent()->Links->getPrimaryKeys(), $ids))
{
$q = Doctrine_Core::getTable('sfSympalAsset')
->createQuery('c')
@@ -122,12 +157,12 @@
$contentObjects = $q->execute();
foreach ($contentObjects as $contentObject)
{
- $this->_content->Links[] = $contentObject;
+ $replacer->getContent()->Links[] = $contentObject;
}
- $this->_content->save();
+ $replacer->getContent()->save();
}
- $contentObjects = $this->_buildObjects($this->_content->Links);
+ $contentObjects = self::_buildObjects($replacer->getContent()->Links);
foreach ($replacements as $replacement)
{
$contentObject = $contentObjects[$replacement['id']];
@@ -141,7 +176,7 @@
}
else
{
- $label = isset($replacement['options']['label']) ?
$this->replace($replacement['options']['label']) : 'Link to content id
#'.$replacement['id'];
+ $label = isset($replacement['options']['label']) ?
$replacer->replace($replacement['options']['label']) : 'Link to content id
#'.$replacement['id'];
unset($replacement['options']['label']);
$content = str_replace($replacement['replace'], link_to($label,
$contentObject->getRoute(), $replacement['options']), $content);
@@ -149,14 +184,101 @@
}
return $content;
}
-
- private function _buildObjects(Doctrine_Collection $collection)
+
+ /**
+ * Replaces objects, we can correspond to a wide-variety of keys
+ */
+ public static function _replaceObjects($key, $ids, $replacements, $content,
sfSympalAssetReplacer $replacer)
{
+ $slotObjectConfig = sfSympalConfig::get('content_slot_objects', $key,
array());
+
+ // of we can't locate the key, just replace everything with nothing
+ if (!isset($slotObjectConfig['class']))
+ {
+ foreach($replacements as $replacement)
+ {
+ $content = str_replace($replacement['replace'], '', $content);
+ }
+
+ return $content;
+ }
+
+ $template = $slotObjectConfig['template'];
+ $class = $slotObjectConfig['class'];
+
+ // retrieve the Doctrine_Collection of objects
+ $tbl = Doctrine_Core::getTable($class);
+ if (method_exists($tbl, 'fetchForSlot'))
+ {
+ $slotObjects = $tbl->fetchForSlot($ids);
+ }
+ else
+ {
+ $slotObjects = $tbl->createQuery('s')
+ ->whereIn('s.id', $ids)
+ ->execute();
+ }
+
+ $slotObjects = self::_buildObjects($slotObjects);
+ foreach ($replacements as $replacement)
+ {
+ $slotObject = $slotObjects[$replacement['id']];
+
+ $replacement['options'][$class] = $slotObject;
+ $ret = get_partial($template, $replacement['options']);
+
+ $content = str_replace($replacement['replace'], $ret, $content);
+ }
+
+ return $content;
+ }
+
+ /**
+ * Remaps a doctrine collection so that the keys of the colleciton "array"
+ * are the ids of the underlying objects. This makes the collection
+ * easier to deal with then replacing objects
+ *
+ * @return Doctrine_Collection
+ */
+ public static function _buildObjects(Doctrine_Collection $collection)
+ {
$objects = new Doctrine_Collection($collection->getTable());
foreach ($collection as $key => $value)
{
$objects[$value->id] = $value;
}
+
return $objects;
}
+
+ /**
+ * Retrieves all of the replacements and their associated callbacks
+ */
+ protected function getReplacementMap()
+ {
+ if ($this->_replacementMap === null)
+ {
+ $map = array(
+ 'link' => array('sfSympalAssetReplacer' => '_replaceLinks'),
+ 'asset' => array('sfSympalAssetReplacer' => '_replaceAssets')
+ );
+
+ $dispatcher =
sfContext::getInstance()->getConfiguration()->getEventDispatcher();
+
+ $this->_replacementMap = $dispatcher->filter(
+ new sfEvent($this, 'sympal.asset_replacer.filter_map'),
+ $map
+ )->getReturnValue();
+ }
+
+ return $this->_replacementMap;
+ }
+
+ /**
+ * @return sfSympalContent
+ */
+ public function getContent()
+ {
+ return $this->_content;
+ }
}
\ No newline at end of file
Modified:
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/config/routing.yml
===================================================================
---
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/config/routing.yml
2010-01-30 16:32:03 UTC (rev 27330)
+++
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/config/routing.yml
2010-01-30 17:55:55 UTC (rev 27331)
@@ -8,4 +8,8 @@
sympal_editor_links:
url: /editor/links
- param: { module: sympal_editor, action: links }
\ No newline at end of file
+ param: { module: sympal_editor, action: links }
+
+sympal_editor_objects:
+ url: /editor/objects
+ param: { module: sympal_editor, action: objects }
Modified:
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/lib/Basesympal_editorActions.class.php
===================================================================
---
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/lib/Basesympal_editorActions.class.php
2010-01-30 16:32:03 UTC (rev 27330)
+++
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/lib/Basesympal_editorActions.class.php
2010-01-30 17:55:55 UTC (rev 27331)
@@ -46,4 +46,34 @@
->orderBy('m.root_id, m.lft ASC')
->execute();
}
+
+ /**
+ * Ajax action that populates the object choose on the editor toolbar
+ */
+ public function executeObjects(sfWebRequest $request)
+ {
+ $this->slotKeys = array_keys(sfSympalConfig::get('content_slot_objects',
null, array()));
+
+ if (count($this->slotKeys))
+ {
+ if (!$object_slug = $request->getParameter('slot_key'))
+ {
+ $this->slotKey = $this->slotKeys[0];
+ } else {
+ $this->slotKey = $object_slug;
+ }
+
+ $config = sfSympalConfig::get('content_slot_objects', $this->slotKey);
+ $class = $config['class'];
+
+ $tbl = Doctrine_Core::getTable($class);
+
+ $q = (method_exists($tbl, 'getObjectSlotQuery')) ?
$tbl->getObjectSlotQuery() : $tbl->createQuery();
+ $this->objects = $q->execute();
+ }
+ else
+ {
+ $this->slotKey = false;
+ }
+ }
}
\ No newline at end of file
Modified:
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/templates/_editor.php
===================================================================
---
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/templates/_editor.php
2010-01-30 16:32:03 UTC (rev 27330)
+++
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/templates/_editor.php
2010-01-30 17:55:55 UTC (rev 27331)
@@ -68,6 +68,7 @@
<ul class="sympal_inline_edit_bar_big_buttons
sympal_inline_edit_bar_buttons">
<li><input type="button" class="toggle_sympal_assets" name="assets"
rel="<?php echo url_for('@sympal_assets_select') ?>" value="<?php echo
__('Assets') ?>" /></li>
<li><input type="button" class="toggle_sympal_links" name="links"
rel="<?php echo url_for('@sympal_editor_links') ?>" value="<?php echo
__('Links') ?>" /></li>
+ <li><input type="button" class="toggle_sympal_objects" name="objects"
rel="<?php echo url_for('@sympal_editor_objects') ?>" value="<?php echo
__('Objects') ?>" /></li>
<?php if ($sf_sympal_content->getEditableSlotsExistOnPage()): ?>
<li><input type="button" class="sympal_save_content_slots" name="save"
value="<?php echo __('Save') ?>" /></li>
@@ -80,6 +81,7 @@
<div id="sympal_assets"></div>
<div id="sympal_links"></div>
+<div id="sympal_objects"></div>
<div id="sympal_dashboard"></div>
<div id="sympal_slot_errors"></div>
<div id="sympal_slot_errors_icon"></div>
\ No newline at end of file
Added:
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/templates/objectsSuccess.php
===================================================================
---
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/templates/objectsSuccess.php
(rev 0)
+++
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/modules/sympal_editor/templates/objectsSuccess.php
2010-01-30 17:55:55 UTC (rev 27331)
@@ -0,0 +1,56 @@
+<?php sympal_use_jquery() ?>
+<script type="text/javascript" src="<?php echo
javascript_path('/sfSympalEditorPlugin/js/objects.js') ?>"></script>
+
+<div id="sympal_objects_container">
+ <h1><?php echo __('Object Browser') ?></h1>
+
+ <p>
+ <?php echo __(
+ 'Browse your objects below and insert objects into the currently focused
editor by '.
+ 'just clicking the object you want to choose.') ?>
+ </p>
+
+ <div id="content_types">
+ <?php if (count($slotKeys)): ?>
+ <h2><?php echo __('Classes') ?></h2>
+ <ul id="editor_slot_object_classes">
+ <?php foreach ($slotKeys as $key): ?>
+ <li>
+ <?php if ($slotKey === $key): ?>
+ <strong><?php echo $key ?></strong>
+ <?php else: ?>
+ <?php echo jq_link_to_remote($key, array(
+ 'url' => url_for('@sympal_editor_objects?slot_key='.$key),
+ 'update' => 'sympal_objects_container'
+ )) ?>
+ <?php endif; ?>
+ </li>
+ <?php endforeach; ?>
+ </ul>
+ <?php else: ?>
+ <h2><?php echo __('No objects slots have been defined') ?></h2>
+ <?php endif; ?>
+ </div>
+
+ <div id="sympal_objects_list">
+ <?php if ($slotKey): ?>
+ <h2><?php echo $slotKey ?> <?php echo __('Objects') ?></h2>
+ <ul>
+ <?php if (count($objects)): ?>
+
+ <?php foreach ($objects as $object): ?>
+ <li rel="<?php echo $slotKey.':'.$object->id ?>">
+ <?php echo image_tag('/sfSympalPlugin/images/file_icon.gif') ?>
+
+ <a href="#"><?php echo $object ?></a>
+ </li>
+ <?php endforeach; ?>
+ <?php else: ?>
+ <?php echo __('Nothing found') ?>
+ <?php endif; ?>
+ </ul>
+ <?php endif; ?>
+ </div>
+
+ <a class="sympal_close_menu"><?php echo __('Close') ?></a>
+</div>
\ No newline at end of file
Modified:
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/css/editor.css
===================================================================
---
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/css/editor.css
2010-01-30 16:32:03 UTC (rev 27330)
+++
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/css/editor.css
2010-01-30 17:55:55 UTC (rev 27331)
@@ -2,7 +2,7 @@
margin-top: 30px;
}
-#sympal_dashboard, #sympal_editor, #sympal_assets, #sympal_links ,
#sympal_assets {
+#sympal_dashboard, #sympal_editor, #sympal_assets, #sympal_links ,
#sympal_assets, #sympal_objects {
background: #fff;
padding: 12px;
border: 1px solid #ccc;
@@ -324,6 +324,11 @@
text-indent: 15px;
}
+.sympal_inline_edit_bar input.toggle_sympal_objects {
+ background: #f5f5f5 url(../../sfSympalPlugin/images/file_icon.gif) 2px 5px
no-repeat !important;
+ text-indent: 15px;
+}
+
.sympal_inline_edit_bar input.toggle_edit_mode {
background: #f5f5f5 url(../../sf/sf_admin/images/tick.png) 2px 3px no-repeat
!important;
text-indent: 15px;
Modified:
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/js/editor.js
===================================================================
---
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/js/editor.js
2010-01-30 16:32:03 UTC (rev 27330)
+++
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/js/editor.js
2010-01-30 17:55:55 UTC (rev 27331)
@@ -281,6 +281,7 @@
$('#sympal_editor').hide();
$('#sympal_assets').hide();
$('#sympal_links').hide();
+ $('#sympal_objects').hide();
}
function sympalSetupDropdownMenus()
@@ -288,4 +289,5 @@
sympalSetupDropdownMenu('#sympal_dashboard', '.toggle_dashboard_menu');
sympalSetupDropdownMenu('#sympal_assets', '.toggle_sympal_assets');
sympalSetupDropdownMenu('#sympal_links', '.toggle_sympal_links');
+ sympalSetupDropdownMenu('#sympal_objects', '.toggle_sympal_objects');
}
\ No newline at end of file
Added:
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/js/objects.js
===================================================================
---
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/js/objects.js
(rev 0)
+++
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalEditorPlugin/web/js/objects.js
2010-01-30 17:55:55 UTC (rev 27331)
@@ -0,0 +1,10 @@
+$(function() {
+ $('#sympal_objects_container #sympal_objects_list ul li a').click(function()
{
+ parentEle = $(this).parents('li');
+
+ var text = '['+ parentEle.attr('rel') + ']';
+ sympalInsertIntoCurrentEditor(text);
+
+ return false;
+ });
+});
\ No newline at end of file
Modified:
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalRenderingPlugin/config/sfSympalRenderingPluginConfiguration.class.php
===================================================================
---
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalRenderingPlugin/config/sfSympalRenderingPluginConfiguration.class.php
2010-01-30 16:32:03 UTC (rev 27330)
+++
plugins/sfSympalPlugin/trunk/lib/plugins/sfSympalRenderingPlugin/config/sfSympalRenderingPluginConfiguration.class.php
2010-01-30 17:55:55 UTC (rev 27331)
@@ -5,6 +5,8 @@
public function initialize()
{
$this->dispatcher->connect('response.filter_content', array($this,
'listenToResponseFilterContent'));
+
+ $this->dispatcher->connect('sympal.asset_replacer.filter_map',
array($this, 'listenToAssetReplacerFilterMap'));
}
public function listenToResponseFilterContent(sfEvent $event, $content)
@@ -22,4 +24,20 @@
return $content;
}
}
+
+ /**
+ * Listens to sympal.asset_replacer.filter_map and adds in all of the
+ * content slot objects that should be filtered
+ */
+ public function listenToAssetReplacerFilterMap(sfEvent $event, $map)
+ {
+ $slotObjects = sfSympalConfig::get('content_slot_objects', null, array());
+ foreach($slotObjects as $slotKey => $slotConfig)
+ {
+ $callback = isset($slotConfig['replacement_callback']) ?
$slotConfig['replacement_callback'] : array('sfSympalAssetReplacer',
'_replaceObjects');
+ $map[$slotKey] = $callback;
+ }
+
+ return $map;
+ }
}
\ No newline at end of file
--
You received this message because you are subscribed to the Google Groups
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/symfony-svn?hl=en.