Development

Changeset 20199

You must first sign up to be able to contribute.

Changeset 20199

Show
Ignore:
Timestamp:
07/16/09 04:30:13 (4 years ago)
Author:
bshaffer
Message:

first major step in porting the csComment plugin... This plugin is still not stable

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfCommentsPlugin/trunk/config/doctrine/comment.yml

    r20153 r20199  
    1111      primary: true 
    1212      autoincrement: true 
    13     subject: 
    14       type:     string(255) 
    1513    body: 
    1614      type:     clob 
     15    approved: 
     16      type:     boolean 
     17      default:  false 
     18    approved_at: 
     19      type:     timestamp 
    1720    user_id: 
    1821      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 ?> 
     1Commenter: 
     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  
    1010class DefaultCommentForm extends BaseCommentForm 
    1111{ 
    12  public function configure() 
     12  public function configure() 
    1313  { 
    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      )); 
    1723  } 
    1824} 
  • plugins/sfCommentsPlugin/trunk/lib/generator/CommentFormGenerator.class.php

    r20153 r20199  
    1414    return array('NewsComment'); 
    1515  }     
     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  } 
    1630 
    1731  /** 
  • plugins/sfCommentsPlugin/trunk/lib/helper/CommentHelper.php

    r20105 r20199  
    3535 
    3636 
    37 function get_doctrine_comments($record) 
     37function get_comments($record) 
    3838{ 
    3939  sfContext::getInstance()->getResponse()->addStylesheet('/sfDoctrineCommentsPlugin/css/comments.css'); 
  • plugins/sfCommentsPlugin/trunk/lib/model/doctrine/PluginComment.class.php

    r20153 r20199  
    66abstract class PluginComment extends BaseComment 
    77{ 
    8  protected $_commentables = array(); 
     8  // Temporary function, commenter relationships not yet implemented 
     9  public function hasCommenter() 
     10  { 
     11    return false; 
     12  } 
    913 
    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    { 
    2063     $this->hasMany($class, array('refClass' => $class.'Comment', 
    2164                                                'local' => 'comment_id', 
     
    2467     $this->hasOne($class.'Comment', array('local' => 'id', 
    2568                                  '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  } 
    4673} 
  • plugins/sfCommentsPlugin/trunk/lib/model/doctrine/PluginCommentTable.class.php

    r20153 r20199  
    77{ 
    88 
     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 
    946} 
  • plugins/sfCommentsPlugin/trunk/lib/model/doctrine/PluginCommenter.class.php

    r20105 r20199  
    44 * This class has been auto-generated by the Doctrine ORM Framework 
    55 */ 
    6 // abstract class PluginCommenter extends BaseCommenter 
    7 //
    8 // public function getName() 
    9 //
    10 //    return $this['username']; 
    11 //
    12 //
     6abstract 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  
    33class Doctrine_Commentable extends Doctrine_Record_Generator 
    44{ 
    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(); 
    1616 
    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    
     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 
    3131 
    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 
    3636 
    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); 
     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); 
    4848 
    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); 
     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); 
    5555 
    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  } 
    8158 
    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  } 
    8768} 
  • plugins/sfCommentsPlugin/trunk/lib/template/Commentable.php

    r20153 r20199  
    99   */   
    1010  protected $_options = array( 
    11                           'Comment' => array( 
    12                               'form_class' => 'DefaultCommentForm' 
    13                               ),  
    1411                          'Commenter' => array( 
    1512                              'enabled'       =>   true, 
    1613                              'model'         =>  'Commenter', 
    1714                              'table_method'  =>  'addCommenter', 
    18                               ), 
    19                           'relations' => array( 
    20                               'refClass' =>   false, 
    21   )); 
     15                              ) 
     16  ); 
    2217 
    2318  public function __construct(array $options = array()) 
     
    2722  } 
    2823 
    29   public function setUp() 
     24  public function getCommentUserFormClass() 
    3025  { 
    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'; 
    4027  } 
    41 } 
    42  
    43  
    44 //  
    45 //  Commentable.php 
    46 //  csActAsCommentablePlugin 
    47 //   
    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_Template 
    53 {     
    5428   
    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) 
    5635  { 
    5736    $object = $this->getInvoker(); 
    58     $comment->setObjectClass(get_class($object)); 
     37 
    5938    if(!$object['id']) 
    6039    { 
    6140      $object->save(); 
    6241    } 
    63     $comment->setObjectId($object->getId()); 
    6442 
    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; 
    6850  } 
    6951   
    70   public function getCommentThread(
     52  public function getCommentThread($root_id
    7153  { 
    72     return Doctrine::getTable('Comment')->getTree()->fetchBranch($this->getInvoker()->getCommentRootId());   
     54    return Doctrine::getTable('Comment')->getTree()->fetchBranch($root_id);   
    7355  } 
    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 
    10057  public function addCommentFromArray($commentArr) 
    10158  { 
     
    10663    } 
    10764  } 
    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() 
    13267  { 
    13368    $object = $this->getInvoker(); 
    134     return $object->getTable()->getComments($object->getId()); 
     69    return $object['Comments']->count(); 
    13570  } 
    13671 
    137   // Hacks to mimic Doctrine Relationship   
    138   public function setComments($comments) 
     72  public function setUp() 
    13973  { 
    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); 
    18375  } 
    18476} 
  • plugins/sfCommentsPlugin/trunk/modules/csComments/lib/BasecsCommentsActions.class.php

    r20105 r20199  
    1313  public function executeAdd() 
    1414  { 
    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)); 
    1617  } 
    1718 
  • plugins/sfCommentsPlugin/trunk/modules/csComments/lib/BasecsCommentsComponents.class.php

    r20105 r20199  
    44  public function executeThread() 
    55  { 
    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(); 
    157  } 
     8   
    169  public function executeAdd_comment() 
    1710  { 
  • plugins/sfCommentsPlugin/trunk/modules/csComments/templates/_comments.php

    r20105 r20199  
    33  <ul> 
    44    <?php foreach($comments AS $comment): ?> 
    5      <?php if( $comment->getLevel() == 0 ): ?> 
    6        <?php continue ?> 
    7      <?php endif ?> 
    85     <?php include_partial('csComments/comment_row', array('comment' => $comment, 'record' => $record)) ?> 
    96    <?php endforeach ?>