public function configure()
 {
     $this->useFields(array('general_categ_id', 'company_categ_name', 'company_categ_description'));
     //Narrow down the valid options for some field validators
     $companyCategs = CompanyCategoryTable::getInstance()->getCompanyCategoriesByCompanyIdQuery($this->getOption('company_user_id'), $this->getOption('current_category'), $this->getOption('current_category_lft'), $this->getOption('current_category_rgt'));
     $this->widgetSchema['parent_category'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getModelName(), 'add_empty' => false, 'query' => $companyCategs));
     $this->widgetSchema['general_categ_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('GeneralCategory'), 'add_empty' => true, 'query' => GeneralCategoryTable::getInstance()->getGeneralCategoriesByLftQuery()));
     $this->widgetSchema['company_categ_description'] = new sfWidgetFormTextarea();
     $this->validatorSchema['parent_category'] = new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'required' => true, 'query' => $companyCategs));
     $this->validatorSchema['general_categ_id'] = new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('GeneralCategory'), 'required' => false, 'query' => GeneralCategoryTable::getInstance()->getGeneralCategoriesByLftQuery()));
     $this->widgetSchema->setLabels(array('parent_category' => 'Parent Company Category'));
     $this->widgetSchema->setLabels(array('general_categ_id' => 'General Category'));
     $this->widgetSchema->setLabels(array('company_categ_name' => 'Company Category Name'));
     $this->widgetSchema->setLabels(array('company_categ_description' => 'Company Category Description'));
     //i18n (Internationalization)
     $this->widgetSchema->getFormFormatter()->setTranslationCatalogue('company_category_form');
 }
Exemplo n.º 2
0
 public function executeChoose($request)
 {
     //Get user Id
     $userId = $this->getUser()->getGuardUser()->getId();
     //Get data from user. Empty array by default.
     $checked = $request->getParameter('checked', array());
     $uniqChecked = array_unique($checked, SORT_NUMERIC);
     //We retrieve a Doctrine_Collection
     $userBaskets = UserBasketTable::getInstance()->findByUserId($userId);
     //Using Doctrine_Collection_Iterator
     $iterator = $userBaskets->getIterator();
     while ($userBasket = $iterator->current()) {
         foreach ($uniqChecked as $index => $value) {
             if ($userBasket->getGeneralCategId() == $value) {
                 unset($uniqChecked[$index]);
                 $iterator->next();
                 //I know this sucks, but I am in a hurry.
                 continue 2;
             }
         }
         $userBaskets->remove($iterator->key());
         $iterator->next();
     }
     if (!empty($uniqChecked)) {
         foreach ($uniqChecked as $index => $value) {
             //Never trust in data coming from users... Performance vs security.
             $generalCategory = GeneralCategoryTable::getInstance()->findOneById($value);
             //TODO: some evil person could send the data without using my nice JavaScript code
             //Here I should check if the node has child nodes and add them always, even if the user
             //did not send them because she/he is not using my nice JavaScript code. My JavaScript code
             //always checks the child nodes' checkbox following the hierarchy structure.
             if ($generalCategory != null) {
                 $userBasket = new UserBasket();
                 $userBasket->general_categ_id = $generalCategory->getId();
                 $userBasket->user_id = $userId;
                 $userBaskets->add($userBasket);
             }
         }
     }
     //The Doctrine_Collection should just insert/remove in the database in this point (never before)
     //This feature is really nice (if it works as intended) I have no time for checking out its behaviour...
     $userBaskets->save();
     //set content type HTTP field  with the right value (we are going to use a JSON response)
     $this->getResponse()->setContentType('application/json');
     //Bypass completely the view layer and set the response code directly from this action.
     //In this way the user may know if the data were updated
     return $this->renderText(json_encode($uniqChecked));
 }