Changeset 20199
- Timestamp:
- 07/16/09 04:30:13 (4 years ago)
- Files:
-
- plugins/sfCommentsPlugin/trunk/config/doctrine/comment.yml (modified) (1 diff)
- plugins/sfCommentsPlugin/trunk/config/doctrine/commenter.yml (modified) (1 diff)
- plugins/sfCommentsPlugin/trunk/lib/form/DefaultCommentForm.class.php (modified) (1 diff)
- plugins/sfCommentsPlugin/trunk/lib/generator/CommentFormGenerator.class.php (modified) (1 diff)
- plugins/sfCommentsPlugin/trunk/lib/helper/CommentHelper.php (modified) (1 diff)
- plugins/sfCommentsPlugin/trunk/lib/model/doctrine/PluginComment.class.php (modified) (2 diffs)
- plugins/sfCommentsPlugin/trunk/lib/model/doctrine/PluginCommentTable.class.php (modified) (1 diff)
- plugins/sfCommentsPlugin/trunk/lib/model/doctrine/PluginCommenter.class.php (modified) (1 diff)
- plugins/sfCommentsPlugin/trunk/lib/model/doctrine/PluginCommenterTable.class.php (added)
- plugins/sfCommentsPlugin/trunk/lib/template/CommentLinkGenerator.class.php (modified) (1 diff)
- plugins/sfCommentsPlugin/trunk/lib/template/Commentable.php (modified) (3 diffs)
- plugins/sfCommentsPlugin/trunk/lib/template/CommentableComment.class.php (deleted)
- plugins/sfCommentsPlugin/trunk/modules/csComments/lib/BasecsCommentsActions.class.php (modified) (1 diff)
- plugins/sfCommentsPlugin/trunk/modules/csComments/lib/BasecsCommentsComponents.class.php (modified) (1 diff)
- plugins/sfCommentsPlugin/trunk/modules/csComments/templates/_comments.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfCommentsPlugin/trunk/config/doctrine/comment.yml
r20153 r20199 11 11 primary: true 12 12 autoincrement: true 13 subject:14 type: string(255)15 13 body: 16 14 type: clob 15 approved: 16 type: boolean 17 default: false 18 approved_at: 19 type: timestamp 17 20 user_id: 18 21 type: integer plugins/sfCommentsPlugin/trunk/config/doctrine/commenter.yml
r20105 r20199 1 # <?php if(sfConfig::get('app_comments_commenter_class') == 'Commenter'): ?> 2 # Commenter: 3 # columns: 4 # username: 5 # type: string(255) 6 # email: 7 # type: string(255) 8 # relations: 9 # Comment: 10 # local: id 11 # type: one 12 # alias: Comment 13 # foreign: user_id 14 # foreignType: one 15 # foreignAlias: Commenter 16 # <?php endif ?> 1 Commenter: 2 columns: 3 username: 4 type: string(255) 5 email: 6 type: string(255) 7 relations: 8 Comment: 9 local: id 10 type: one 11 alias: Comment 12 foreign: user_id 13 foreignType: one 14 foreignAlias: Commenter plugins/sfCommentsPlugin/trunk/lib/form/DefaultCommentForm.class.php
r20105 r20199 10 10 class DefaultCommentForm extends BaseCommentForm 11 11 { 12 public function configure()12 public function configure() 13 13 { 14 parent::configure(); 15 unset($this['created_at'], $this['updated_at'], $this['approved'], $this['approved_at'], $this['approved_by'], $this['lft'], $this['rgt'], $this['level'], $this['object_id'], $this['object_class'], $this['user_id']); 16 $this->widgetSchema->setLabel('body', 'Your Comment'); 14 $this->setWidgets(array( 15 'body' => new sfWidgetFormTextarea(), 16 )); 17 18 $this->widgetSchema->setLabel('body', 'Your Comment'); 19 20 $this->setValidators(array( 21 'body' => new sfValidatorString(), 22 )); 17 23 } 18 24 } plugins/sfCommentsPlugin/trunk/lib/generator/CommentFormGenerator.class.php
r20153 r20199 14 14 return array('NewsComment'); 15 15 } 16 17 public function setUp() 18 { 19 // generate this form 20 $generatorManager = new sfGeneratorManager(ProjectConfiguration::getApplicationConfiguration('backend', 'dev', true)); 21 $generatorManager->generate('sfDoctrineFormGenerator', array( 22 'connection' => 'doctrine', 23 'model_dir_name' => 'model', 24 'form_dir_name' => 'form', 25 )); 26 $generator = new CommentFormGenerator($generatorManager); 27 $generator->generate(array('connection' => 'doctrine')); 28 29 } 16 30 17 31 /** plugins/sfCommentsPlugin/trunk/lib/helper/CommentHelper.php
r20105 r20199 35 35 36 36 37 function get_ doctrine_comments($record)37 function get_comments($record) 38 38 { 39 39 sfContext::getInstance()->getResponse()->addStylesheet('/sfDoctrineCommentsPlugin/css/comments.css'); plugins/sfCommentsPlugin/trunk/lib/model/doctrine/PluginComment.class.php
r20153 r20199 6 6 abstract class PluginComment extends BaseComment 7 7 { 8 protected $_commentables = array(); 8 // Temporary function, commenter relationships not yet implemented 9 public function hasCommenter() 10 { 11 return false; 12 } 9 13 10 public function hasCommenter() 11 { 12 return ((string)$this['Commenter'] != null && (string)$this['Commenter'] != ''); 13 } 14 15 public function setTableDefinition() 16 { 17 // Inexusable hack for creating dynamic relationships on Comment class 18 foreach ($this->getCommentables() as $class) 19 { 14 // =================================== 15 // = Options for Approving a Comment = 16 // =================================== 17 public function approve() 18 { 19 $this->setApproved(true); 20 $this->setApprovedAt(date('Y-m-d')); 21 $this->save(); 22 } 23 24 public function unapprove() 25 { 26 $this->setApproved(false); 27 $this->setApprovedAt(null); 28 $this->save(); 29 } 30 31 public function isApproved() 32 { 33 return $this->getApproved(); 34 } 35 36 // Get all modules acting as Commentable 37 public function getCommentables() 38 { 39 $dispatcher = ProjectConfiguration::getActive()->getEventDispatcher(); 40 $event = new sfEvent($this, 'commentable.add_commentable_class'); 41 42 $dispatcher->notify($event); 43 44 return $event->getReturnValue(); 45 } 46 47 // Set every record as a root node 48 public function preSave($event) 49 { 50 if (!$this['lft'] && !$this['rgt']) 51 { 52 $this['lft'] = 1; 53 $this['rgt'] = 2; 54 $this['level'] = 0; 55 } 56 } 57 58 public function setTableDefinition() 59 { 60 // Add all the Commentable relations 61 foreach ($this->getCommentables() as $class) 62 { 20 63 $this->hasMany($class, array('refClass' => $class.'Comment', 21 64 'local' => 'comment_id', … … 24 67 $this->hasOne($class.'Comment', array('local' => 'id', 25 68 'foreign' => 'comment_id')); 26 } 27 28 return parent::setTableDefinition(); 29 } 30 31 // Get all modules acting as Commentable 32 public function getCommentables() 33 { 34 $dispatcher = ProjectConfiguration::getActive()->getEventDispatcher(); 35 $event = new sfEvent($this, 'commentable.add_commentable_class'); 36 37 $dispatcher->notify($event); 38 39 return $event->getReturnValue(); 40 } 41 42 public function addCommentable(sfEvent $event) 43 { 44 $this->_commentables[] = $event['commentable']; 45 } 69 } 70 71 return parent::setTableDefinition(); 72 } 46 73 } plugins/sfCommentsPlugin/trunk/lib/model/doctrine/PluginCommentTable.class.php
r20153 r20199 7 7 { 8 8 9 public function approveAll() 10 { 11 $query = $this->createQuery()->update()->set('approved', 1); 12 return $query->execute(); 13 } 14 15 public function findApproved() 16 { 17 $query = $this->createQuery('co') 18 ->addWhere('co.approved', true); 19 20 return $query->execute(); 21 } 22 23 public function getApprovedQuery(&$query) 24 { 25 $query->addWhere($query->getRootAlias().'.approved = ?', true); 26 27 return $query; 28 } 29 30 public function findUnapproved() 31 { 32 $query = $this->createQuery('co') 33 ->addWhere('co.approved = false'); 34 35 return $query->execute(); 36 } 37 38 public function findApprovedBy($user) 39 { 40 $query = $this->createQuery('co') 41 ->addWhere('co.approved_by = ?', true); 42 43 return $query->execute(); 44 } 45 9 46 } plugins/sfCommentsPlugin/trunk/lib/model/doctrine/PluginCommenter.class.php
r20105 r20199 4 4 * This class has been auto-generated by the Doctrine ORM Framework 5 5 */ 6 //abstract class PluginCommenter extends BaseCommenter7 //{8 //public function getName()9 //{10 //return $this['username'];11 //}12 //}6 abstract class PluginCommenter extends BaseCommenter 7 { 8 public function getName() 9 { 10 return $this['username']; 11 } 12 } plugins/sfCommentsPlugin/trunk/lib/template/CommentLinkGenerator.class.php
r20153 r20199 3 3 class Doctrine_Commentable extends Doctrine_Record_Generator 4 4 { 5 protected $_options = array('commentClass' => 'Comment',6 'commentAlias' => 'Comments',7 'className' => '%CLASS%Comment',8 'local' => 'id',9 'generateFiles' => true,10 'table' => false,11 'pluginTable' => false,12 'children' => array(),13 'builderOptions' => array('BaseClassesDirectory' => 'base'));14 15 protected $_commentables = array();5 protected $_options = array('commentClass' => 'Comment', 6 'commentAlias' => 'Comments', 7 'className' => '%CLASS%Comment', 8 'local' => 'id', 9 'generateFiles' => false, 10 'table' => false, 11 'pluginTable' => false, 12 'children' => array(), 13 'builderOptions' => array('BaseClassesDirectory' => 'base')); 14 15 protected $_commentables = array(); 16 16 17 /**18 * __construct19 *20 * @param string $options21 * @return void22 */23 public function __construct(array $options = array())24 {25 $dispatcher = ProjectConfiguration::getActive()->getEventDispatcher();26 $dispatcher->connect('commentable.add_commentable_class', array($this, 'getCommentables'));27 28 $options['generatePath'] = sfConfig::get('sf_lib_dir').'/model/doctrine/sfCommentsPlugin';29 $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);30 }17 /** 18 * __construct 19 * 20 * @param string $options 21 * @return void 22 */ 23 public function __construct(array $options = array()) 24 { 25 $dispatcher = ProjectConfiguration::getActive()->getEventDispatcher(); 26 $dispatcher->connect('commentable.add_commentable_class', array($this, 'getCommentables')); 27 28 $options['generatePath'] = sfConfig::get('sf_lib_dir').'/model/doctrine/sfCommentsPlugin'; 29 $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options); 30 } 31 31 32 public function setTableDefinition()33 {34 $this->hasColumn('comment_id', 'integer', null, array('primary' => true));35 }32 public function setTableDefinition() 33 { 34 $this->hasColumn('comment_id', 'integer', null, array('primary' => true)); 35 } 36 36 37 public function buildRelation()38 {39 $this->addCommentable($this->getOption('table')->getComponentName());40 41 // Set index on Comment Table42 $options = array('local' => 'comment_id',43 'foreign' => 'id',44 'onDelete' => 'CASCADE',45 'onUpdate' => 'CASCADE');46 47 $this->_table->bind(array($this->_options['commentClass'], $options), Doctrine_Relation::ONE);37 public function buildRelation() 38 { 39 $this->addCommentable($this->getOption('table')->getComponentName()); 40 41 // Set index on Comment Table 42 $options = array('local' => 'comment_id', 43 'foreign' => 'id', 44 'onDelete' => 'CASCADE', 45 'onUpdate' => 'CASCADE'); 46 47 $this->_table->bind(array($this->_options['commentClass'], $options), Doctrine_Relation::ONE); 48 48 49 //Set index on Commentable Object Table50 $options = array('local' => 'id',51 'foreign' => 'comment_id',52 'refClass' => $this->getOption('table')->getComponentName() . $this->_options['commentClass']);53 54 $this->getOption('table')->bind(array($this->_options['commentClass'] . ' as ' . $this->_options['commentAlias'], $options), Doctrine_Relation::ONE);49 //Set index on Commentable Object Table 50 $options = array('local' => 'id', 51 'foreign' => 'comment_id', 52 'refClass' => $this->getOption('table')->getComponentName() . $this->_options['commentClass']); 53 54 $this->getOption('table')->bind(array($this->_options['commentClass'] . ' as ' . $this->_options['commentAlias'], $options), Doctrine_Relation::ONE); 55 55 56 parent::buildRelation(); 57 } 58 59 public function getCommentables(sfEvent $event) 60 { 61 $event->setReturnValue($this->_commentables); 62 } 63 public function addCommentable($class) 64 { 65 $this->_commentables[] = $class; 66 } 67 /** 68 * generateClass 69 * 70 * generates the class definition for plugin class 71 * 72 * @param array $definition Definition array defining columns, relations and options 73 * for the model 74 * @return void 75 */ 76 public function generateClass(array $definition = array()) 77 { 78 $definition['actAs'] = array('Comment'); 79 return parent::generateClass($definition); 80 } 56 parent::buildRelation(); 57 } 81 58 82 public function notifyNewCommentableClass($class) 83 { 84 $dispatcher = ProjectConfiguration::getActive()->getEventDispatcher(); 85 $dispatcher->notify(new sfEvent($this, 'commentable.add_commentable_class', array('commentable' => $class))); 86 } 59 public function getCommentables(sfEvent $event) 60 { 61 $event->setReturnValue($this->_commentables); 62 } 63 64 public function addCommentable($class) 65 { 66 $this->_commentables[] = $class; 67 } 87 68 } plugins/sfCommentsPlugin/trunk/lib/template/Commentable.php
r20153 r20199 9 9 */ 10 10 protected $_options = array( 11 'Comment' => array(12 'form_class' => 'DefaultCommentForm'13 ),14 11 'Commenter' => array( 15 12 'enabled' => true, 16 13 'model' => 'Commenter', 17 14 'table_method' => 'addCommenter', 18 ), 19 'relations' => array( 20 'refClass' => false, 21 )); 15 ) 16 ); 22 17 23 18 public function __construct(array $options = array()) … … 27 22 } 28 23 29 public function setUp()24 public function getCommentUserFormClass() 30 25 { 31 $this->_plugin->initialize($this->_table); 32 // $generatorManager = new sfGeneratorManager(ProjectConfiguration::getApplicationConfiguration('backend', 'dev', true)); 33 // $generatorManager->generate('sfDoctrineFormGenerator', array( 34 // 'connection' => 'doctrine', 35 // 'model_dir_name' => 'model', 36 // 'form_dir_name' => 'form', 37 // )); 38 // $generator = new CommentFormGenerator($generatorManager); 39 // $generator->generate(array('connection' => 'doctrine')); 26 return 'DefaultCommenterForm'; 40 27 } 41 }42 43 44 //45 // Commentable.php46 // csActAsCommentablePlugin47 //48 // Created by Brent Shaffer on 2009-01-29.49 // Copyright 2008 Centre{source}. Al9 rights reserved.50 //51 52 class Doctrine_Template_Commentable_Old extends Doctrine_Template53 {54 28 55 public function addComment($comment, $root_id = null) 29 public function getCommentFormClass() 30 { 31 return 'DefaultCommentForm'; 32 } 33 34 public function addComment($comment, $parent_id = null) 56 35 { 57 36 $object = $this->getInvoker(); 58 $comment->setObjectClass(get_class($object)); 37 59 38 if(!$object['id']) 60 39 { 61 40 $object->save(); 62 41 } 63 $comment->setObjectId($object->getId());64 42 65 $root = $root_id ? Doctrine::getTable('Comment')->find($root_id) : ($object->getCommentRootId() ? Doctrine::getTable('Comment')->find($object->getCommentRootId()) : $this->addCommentRoot()); 66 67 $root->getNode()->addChild($comment); 43 if ($parent_id) 44 { 45 $parent = Doctrine::getTable('Comment')->find($parent_id); 46 $parent->getNode()->addChild($comment); 47 } 48 49 $object['Comments'][] = $comment; 68 50 } 69 51 70 public function getCommentThread( )52 public function getCommentThread($root_id) 71 53 { 72 return Doctrine::getTable('Comment')->getTree()->fetchBranch($ this->getInvoker()->getCommentRootId());54 return Doctrine::getTable('Comment')->getTree()->fetchBranch($root_id); 73 55 } 74 75 public function addCommentRoot() 76 { 77 $object = $this->getInvoker(); 78 $root = $object->createCommentRoot(); 79 $root->refresh(); 80 $object->setCommentRootId($root->getId()); 81 return $root; 82 } 83 84 public function createCommentRoot() 85 { 86 $object = $this->getInvoker(); 87 $root = new Comment(); 88 $root->setBody('root'); 89 $root->setObjectClass(get_class($object)); 90 if(!$object['id']) 91 { 92 $object->save(); 93 } 94 $root->setObjectId($object->getId()); 95 $root->save(); 96 Doctrine::getTable('Comment')->getTree()->createRoot($root); 97 return $root; 98 } 99 56 100 57 public function addCommentFromArray($commentArr) 101 58 { … … 106 63 } 107 64 } 108 109 public function getCommentsQueryTableProxy($id = null) 110 { 111 $query = Doctrine::getTable('Comment')->createQuery(); 112 return $this->addCommentsQueryTableProxy($query, $id); 113 } 114 115 public function addCommentsQueryTableProxy(&$query, $id) 116 { 117 $query->addWhere('Comment.object_class = ?', get_class($this->getInvoker())); 118 if($id) 119 { 120 $query->addWhere('Comment.object_id = ?', $id); 121 } 122 return $query; 123 } 124 125 public function getCommentsTableProxy($id = null) 126 { 127 return $this->getCommentsQueryTableProxy($id)->execute(); 128 } 129 130 // Hacks to mimic Doctrine Relationship 131 public function getComments() 65 66 public function getNumComments() 132 67 { 133 68 $object = $this->getInvoker(); 134 return $object ->getTable()->getComments($object->getId());69 return $object['Comments']->count(); 135 70 } 136 71 137 // Hacks to mimic Doctrine Relationship 138 public function setComments($comments) 72 public function setUp() 139 73 { 140 foreach ($comments as $comment) { 141 $this->addCommentFromArray($comment); 142 } 143 } 144 145 // Can be overriden in your models to use custom forms 146 public function getCommentFormClass() 147 { 148 return $this->_options['comment']['form_class']; 149 } 150 151 // Defaults to the form of your commenter class. 152 // Can be overriden in your models. 153 public function getCommentUserFormClass() 154 { 155 return sfConfig::get('app_comments_commenter_class').'Form'; 156 } 157 158 public function getCommentUserTableMethodTableProxy() 159 { 160 return $this->_options['Commenter']['table_method']; 161 } 162 163 public function addCommenterTableProxy($comment, $form) 164 { 165 $userClass = $this->_options['Commenter']['model']; 166 $user = new $userClass(); 167 foreach ($form->getValues() as $key => $value) { 168 $user->$key = $value; 169 } 170 // $user->save(); 171 $comment['Commenter'] = $user; 172 $comment->save(); 173 } 174 175 public function getNumComments() 176 { 177 $q = Doctrine::getTable('Comment') 178 ->createQuery() 179 ->where('object_id = ?', $this->getInvoker()->getCommentRootId()); 180 181 $count = $q->count(); 182 return $count ? $count - 1 : 0; 74 $this->_plugin->initialize($this->_table); 183 75 } 184 76 } plugins/sfCommentsPlugin/trunk/modules/csComments/lib/BasecsCommentsActions.class.php
r20105 r20199 13 13 public function executeAdd() 14 14 { 15 return $this->renderComponent('csComments', 'add_comment'); 15 $record = Doctrine::getTable($this->getRequestParameter('model'))->findOneById($this->getRequestParameter('record_id')); 16 return $this->renderComponent('csComments', 'add_comment', array('record' => $record)); 16 17 } 17 18 plugins/sfCommentsPlugin/trunk/modules/csComments/lib/BasecsCommentsComponents.class.php
r20105 r20199 4 4 public function executeThread() 5 5 { 6 $treeMgr = Doctrine::getTable('Comment')->getTree(); 7 // $root = $treeMgr->findRoot($this->record->getId()); 8 9 // $this->comments = null; 10 11 // if( $root && $root->getId() ) 12 // { 13 $this->comments = $treeMgr->fetchBranch($this->record->getCommentRootId()); 14 // } 6 $this->comments = $this->record->getComments(); 15 7 } 8 16 9 public function executeAdd_comment() 17 10 { plugins/sfCommentsPlugin/trunk/modules/csComments/templates/_comments.php
r20105 r20199 3 3 <ul> 4 4 <?php foreach($comments AS $comment): ?> 5 <?php if( $comment->getLevel() == 0 ): ?>6 <?php continue ?>7 <?php endif ?>8 5 <?php include_partial('csComments/comment_row', array('comment' => $comment, 'record' => $record)) ?> 9 6 <?php endforeach ?>