update() public static method

Update a certain question
public static update ( array $item )
$item array
Example #1
0
 /**
  * Execute the action
  */
 public function execute()
 {
     parent::execute();
     $questionId = \SpoonFilter::getPostValue('questionId', null, '', 'int');
     $fromCategoryId = \SpoonFilter::getPostValue('fromCategoryId', null, '', 'int');
     $toCategoryId = \SpoonFilter::getPostValue('toCategoryId', null, '', 'int');
     $fromCategorySequence = \SpoonFilter::getPostValue('fromCategorySequence', null, '', 'string');
     $toCategorySequence = \SpoonFilter::getPostValue('toCategorySequence', null, '', 'string');
     // invalid question id
     if (!BackendFaqModel::exists($questionId)) {
         $this->output(self::BAD_REQUEST, null, 'question does not exist');
     } else {
         // list ids
         $fromCategorySequence = (array) explode(',', ltrim($fromCategorySequence, ','));
         $toCategorySequence = (array) explode(',', ltrim($toCategorySequence, ','));
         // is the question moved to a new category?
         if ($fromCategoryId != $toCategoryId) {
             $item['id'] = $questionId;
             $item['category_id'] = $toCategoryId;
             BackendFaqModel::update($item);
             // loop id's and set new sequence
             foreach ($toCategorySequence as $i => $id) {
                 $item = array();
                 $item['id'] = (int) $id;
                 $item['sequence'] = $i + 1;
                 // update sequence if the item exists
                 if (BackendFaqModel::exists($item['id'])) {
                     BackendFaqModel::update($item);
                 }
             }
         }
         // loop id's and set new sequence
         foreach ($fromCategorySequence as $i => $id) {
             $item['id'] = (int) $id;
             $item['sequence'] = $i + 1;
             // update sequence if the item exists
             if (BackendFaqModel::exists($item['id'])) {
                 BackendFaqModel::update($item);
             }
         }
         // success output
         $this->output(self::OK, null, 'sequence updated');
     }
 }
Example #2
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     if ($this->frm->isSubmitted()) {
         $this->meta->setUrlCallback('Backend\\Modules\\Faq\\Engine\\Model', 'getURL', array($this->record['id']));
         $this->frm->cleanupFields();
         // validate fields
         $this->frm->getField('title')->isFilled(BL::err('QuestionIsRequired'));
         $this->frm->getField('answer')->isFilled(BL::err('AnswerIsRequired'));
         $this->frm->getField('category_id')->isFilled(BL::err('CategoryIsRequired'));
         $this->meta->validate();
         if ($this->frm->isCorrect()) {
             // build item
             $item['id'] = $this->id;
             $item['meta_id'] = $this->meta->save(true);
             $item['category_id'] = $this->frm->getField('category_id')->getValue();
             $item['language'] = $this->record['language'];
             $item['question'] = $this->frm->getField('title')->getValue();
             $item['answer'] = $this->frm->getField('answer')->getValue(true);
             $item['hidden'] = $this->frm->getField('hidden')->getValue();
             // update the item
             BackendFaqModel::update($item);
             BackendTagsModel::saveTags($item['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule());
             BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $item));
             // edit search index
             BackendSearchModel::saveIndex($this->getModule(), $item['id'], array('title' => $item['question'], 'text' => $item['answer']));
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('Index') . '&report=saved&var=' . urlencode($item['question']) . '&highlight=' . $item['id']);
         }
     }
 }