public function setup() { parent::setup(); $user = sfContext::getInstance()->getUser(); unset($this['type'], $this['page_id'], $this['created_at'], $this['updated_at'], $this['slug_saved']); $q = Doctrine::getTable($this->getModelName())->addCategoriesForUser($user->getGuardUser(), $user->hasCredential('admin')); $this->setWidget('categories_list', new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => $this->getModelName(), 'query' => $q))); $this->setValidator('categories_list', new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => $this->getModelName(), 'query' => $q, 'required' => false))); if ($user->hasCredential('admin')) { $this->setWidget('categories_list_add', new sfWidgetFormInputHidden()); //TODO: Make this validator better, should check for duplicate categories, etc. $this->setValidator('categories_list_add', new sfValidatorPass(array('required' => false))); } if (!$user->hasCredential('admin')) { unset($this['author_id']); } $templates = sfConfig::get('app_' . $this->engine . '_templates'); $templateChoices = array(); foreach ($templates as $key => $template) { $templateChoices[$key] = $template['name']; } $this->setWidget('template', new sfWidgetFormChoice(array('multiple' => false, 'choices' => $templateChoices))); $this->setValidator('template', new sfValidatorChoice(array('required' => true, 'multiple' => false, 'choices' => array_flip($templateChoices)))); if (count($templateChoices) <= 1) { unset($this['template']); } if (!sfConfig::get('app_aBlog_comments', false)) { unset($this['allow_comments']); } // The candidates to edit pages are candidates to author blogs $candidateGroup = sfConfig::get('app_a_edit_candidate_group', false); $sufficientGroup = sfConfig::get('app_a_edit_sufficient_group', false); if ($user->hasCredential('admin') || $user->getGuardUser()->getId() == $this->getObject()->getAuthorId()) { $q = Doctrine::getTable('sfGuardUser')->createQuery(); $q->addWhere('sfGuardUser.id != ?', $user->getGuardUser()->getId()); if ($candidateGroup) { $q->innerJoin('sfGuardUser.groups g')->addWhere('g.name = ?', array($candidateGroup)); } $this->setWidget('editors_list', new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'query' => $q))); $this->setValidator('editors_list', new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'query' => $q, 'required' => false))); } else { unset($this['editors_list']); } $q = Doctrine::getTable('sfGuardUser')->createQuery('u'); if ($candidateGroup && $sufficientGroup) { $q->leftJoin('u.groups g')->addWhere('(g.name IN (?, ?)) OR (u.is_super_admin IS TRUE)', array($candidateGroup, $sufficientGroup)); } $this->setWidget('author_id', new sfWidgetFormDoctrineChoice(array('model' => 'sfGuardUser', 'query' => $q))); $this->setValidator('author_id', new sfValidatorDoctrineChoice(array('model' => 'sfGuardUser', 'query' => $q, 'required' => false))); $this->setWidget('published_at', new sfWidgetFormJQueryDateTime(array('date' => array('image' => '/apostrophePlugin/images/a-icon-datepicker.png')))); $this->getWidgetSchema()->setDefault('published_at', date('Y-m-d H:i:s')); $this->widgetSchema['tags'] = new sfWidgetFormInput(array('default' => implode(', ', $this->getObject()->getTags())), array('class' => 'tag-input', 'autocomplete' => 'off')); $this->validatorSchema['tags'] = new sfValidatorString(array('required' => false)); $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator')))); }
public function setup() { parent::setup(); $user = sfContext::getInstance()->getUser(); // We implement our own sluggability so we have to take responsibility for // booting that field out of the form classes unset($this['type'], $this['page_id'], $this['created_at'], $this['updated_at'], $this['slug'], $this['slug_saved'], $this['tags'], $this['title'], $this['status']); $q = Doctrine::getTable($this->getModelName())->addCategoriesForUser($user->getGuardUser(), $user->hasCredential('admin')); $this->setWidget('categories_list', new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => $this->getModelName(), 'query' => $q))); $this->setValidator('categories_list', new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => $this->getModelName(), 'query' => $q, 'required' => false))); if ($user->hasCredential('admin')) { // Don't use a hidden field, an actual field will be output in that case and conflict // with the DHTML-generated checkboxes when PHP goes to parse the result $this->setWidget('categories_list_add', new sfWidgetFormInputText()); //TODO: Make this validator better, should check for duplicate categories, etc. $this->setValidator('categories_list_add', new sfValidatorPass(array('required' => false))); } $templates = sfConfig::get('app_' . $this->engine . '_templates', $this->getObject()->getTemplateDefaults()); $templateChoices = array(); foreach ($templates as $key => $template) { $templateChoices[$key] = $template['name']; } $this->setWidget('template', new sfWidgetFormChoice(array('multiple' => false, 'choices' => $templateChoices))); $this->setValidator('template', new sfValidatorChoice(array('required' => true, 'multiple' => false, 'choices' => array_flip($templateChoices)))); if (count($templateChoices) <= 1) { unset($this['template']); } if (!sfConfig::get('app_aBlog_comments', false)) { unset($this['allow_comments']); } // The candidates to edit pages are candidates to author blogs $candidateGroup = sfConfig::get('app_a_edit_candidate_group', false); $sufficientGroup = sfConfig::get('app_a_edit_sufficient_group', false); if ($user->hasCredential('admin') || $user->getGuardUser()->getId() == $this->getObject()->getAuthorId()) { $q = Doctrine::getTable('sfGuardUser')->createQuery(); $q->addWhere('sfGuardUser.id != ?', $user->getGuardUser()->getId()); if ($candidateGroup) { $q->innerJoin('sfGuardUser.Groups g')->addWhere('g.name = ?', array($candidateGroup)); } $this->setWidget('editors_list', new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'query' => $q))); $this->setValidator('editors_list', new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'query' => $q, 'required' => false))); } else { unset($this['editors_list']); } if ($user->hasCredential('admin')) { // Blog author candidates include blog editor candidates, plus admins - if they aren't listed here they // wind up losing authorship to the first person in the dropdown due to the validator! if ($candidateGroup && $sufficientGroup) { $q = Doctrine::getTable('sfGuardUser')->createQuery('u')->leftJoin('u.Permissions p WITH p.name = "cms_admin"')->leftJoin('u.Groups g')->leftJoin('g.Permissions gp WITH gp.name = "cms_admin"')->addWhere('(g.name IN (?, ?)) OR (u.is_super_admin IS TRUE) OR gp.name = "cms_admin" OR p.name = "cms_admin"', array($candidateGroup, $sufficientGroup)); } else { $q = Doctrine::getTable('sfGuardUser')->createQuery('u')->leftJoin('u.Permissions p WITH p.name = "cms_admin"')->leftJoin('u.Groups g')->leftJoin('g.Permissions gp WITH gp.name = "cms_admin"')->addWhere('(u.is_super_admin IS TRUE) OR gp.name = "cms_admin" OR p.name = "cms_admin"', array($candidateGroup, $sufficientGroup)); } $this->setWidget('author_id', new sfWidgetFormDoctrineChoice(array('model' => 'sfGuardUser', 'query' => $q))); $this->setValidator('author_id', new sfValidatorDoctrineChoice(array('model' => 'sfGuardUser', 'query' => $q, 'required' => false))); } else { unset($this['author_id']); } $this->setWidget('published_at', new aWidgetFormJQueryDateTime(array('date' => array('image' => '/apostrophePlugin/images/a-icon-datepicker.png')), array('time' => array('twenty-four-hour' => false, 'minutes-increment' => 30)))); // DON'T set a default for the date/time widget. If you do, for some reason it wins even though // the object already exists. A reasonable default is already in the object anyway by now $tagstring = implode(', ', $this->getObject()->getTags()); // added a space after the comma for readability // class tag-input enabled for typeahead support $this->widgetSchema['tags'] = new sfWidgetFormInput(array('default' => $tagstring), array('class' => 'tags-input', 'autocomplete' => 'off', 'id' => 'a-blog-post-tags-input')); $this->validatorSchema['tags'] = new sfValidatorString(array('required' => false)); $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator')))); $this->configurePublication(); }