Permalink
Browse files

initial import of plugin from http://svn.symfony-projects.org/plugins…

  • Loading branch information...
0 parents commit a0fe23abca764c2a0b9cddc9925efdba1fdb8b9d @bshaffer committed Nov 9, 2010
No changes.
123 README.md
@@ -0,0 +1,123 @@
+csDoctrineActAsSortablePlugin
+=============================
+
+The `csDoctrineActAsSortablePlugin` is a symfony plugin that allows use of the doctrine behavior actAsSortable.
+
+This behavior provides methods on your model for setting display order/position.
+
+This plugin also contains images to implement for ordering.
+
+Installation
+------------
+
+ * Install the plugin
+
+ $ symfony plugin:install csDoctrineActAsSortablePlugin
+
+ * Apply the behavior to your model in your schema file `config/doctrine/schema.yml`, ie:
+
+ [yml]
+ model:
+ actAs: [Sortable]
+ Optionally accepts a UniqueBy attribute which will be used on a model with a one-to-many relationship
+
+ model:
+ actAs: [Sortable]
+ uniqueBy: [parent_id]
+
+ * Rebuild your models and database
+
+ $ symfony doctrine:build-all-reload
+
+ alternatively you could build the models, the sql, then run the sql manually
+
+ * Publish your assets
+
+ $ symfony plugin:publish-assets
+
+ * Clear your cache
+
+ $ symfony cc
+
+
+Available Record Methods
+------------------------
+
+ * promote
+
+ [php]
+ $record->promote();
+
+ * demote
+
+ [php]
+ $record->demote();
+
+ * moveToFirst
+
+ [php]
+ $record->moveToFirst();
+
+ * moveToLast
+
+ [php]
+ $record->moveToLast();
+
+ * moveToPosition
+
+ [php]
+ $record->moveToPosition($newPosition);
+
+
+Available Table Methods
+------------------------
+
+ * sort - accepts the array created by the symfony/prototype sortableElement tag
+
+ [php]
+ Doctrine::getTable('Model')->sort($order);
+
+ * findAllSorted - Accepts sort order (asc, desc)
+
+ [php]
+ Doctrine::getTable('Model')->findAllSorted('ASCENDING');
+
+ * findAllSortedWithParent - accepts the parent column name, the value, and sort order (asc, desc)
+
+ [php]
+ Doctrine::getTable('Model')->findAllSortedWithParent($fk_value, $fk_name, 'ASCENDING');
+
+
+Example Usage With Admin Generator
+----------------------------------
+
+ * In your module, edit `config/generator.yml`, and under list, object actions, add:
+
+ [yml]
+ object_actions:
+ promote:
+ action: promote
+ demote:
+ action: demote
+ _edit: -
+ _delete: -
+
+ * In your module, edit ``, Add the following actions:
+
+ [php]
+ public function executePromote()
+ {
+ $object=Doctrine::getTable('MyModel')->findOneById($this->getRequestParameter('id'));
+
+
+ $object->promote();
+ $this->redirect("@moduleIndexRoute");
+ }
+
+ public function executeDemote()
+ {
+ $object=Doctrine::getTable('MyModel')->findOneById($this->getRequestParameter('id'));
+
+ $object->demote();
+ $this->redirect("@moduleIndexRoute");
+ }
@@ -0,0 +1,76 @@
+<?php
+
+
+/**
+ * Easily sort each record based on position
+ *
+ * @package csDoctrineSortablePlugin
+ * @subpackage listener
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @link www.phpdoctrine.org
+ * @since 1.0
+ * @version $Revision$
+ * @author Travis Black <tblack@centresource.com>
+ */
+class Doctrine_Template_Listener_Sortable extends Doctrine_Record_Listener
+{
+ /**
+ * Array of sortable options
+ *
+ * @var array
+ */
+ protected $_options = array();
+
+
+ /**
+ * __construct
+ *
+ * @param array $options
+ * @return void
+ */
+ public function __construct(array $options)
+ {
+ $this->_options = $options;
+ }
+
+
+ /**
+ * Set the position value automatically when a new sortable object is created
+ *
+ * @param Doctrine_Event $event
+ * @return void
+ */
+ public function preInsert(Doctrine_Event $event)
+ {
+ $fieldName = $this->_options['name'];
+ $object = $event->getInvoker();
+ $object->$fieldName = $object->getFinalPosition()+1;
+ }
+
+
+ /**
+ * When a sortable object is deleted, promote all objects positioned lower than itself
+ *
+ * @param string $Doctrine_Event
+ * @return void
+ */
+ public function postDelete(Doctrine_Event $event)
+ {
+ $fieldName = $this->_options['name'];
+ $object = $event->getInvoker();
+ $position = $object->$fieldName;
+
+ $q = $object->getTable()->createQuery()
+ ->update(get_class($object))
+ ->set($fieldName, $fieldName . ' - ?', '1')
+ ->where($fieldName . ' > ?', $position)
+ ->orderBy($fieldName);
+
+ foreach ($this->_options['uniqueBy'] as $field)
+ {
+ $q->addWhere($field . ' = ?', $object[$field]);
+ }
+
+ $q->execute();
+ }
+}
Oops, something went wrong.

0 comments on commit a0fe23a

Please sign in to comment.