insertCategory() public static method

Inserts a new category into the database
public static insertCategory ( array $item, array $meta = null ) : integer
$item array The data for the category to insert.
$meta array The metadata for the category to insert.
return integer
Exemplo n.º 1
0
 /**
  * Execute the action
  */
 public function execute()
 {
     parent::execute();
     // get parameters
     $categoryTitle = trim(\SpoonFilter::getPostValue('value', null, '', 'string'));
     // validate
     if ($categoryTitle === '') {
         $this->output(self::BAD_REQUEST, null, BL::err('TitleIsRequired'));
     } else {
         // get the data
         // build array
         $item['title'] = \SpoonFilter::htmlspecialchars($categoryTitle);
         $item['language'] = BL::getWorkingLanguage();
         $meta['keywords'] = $item['title'];
         $meta['keywords_overwrite'] = 'N';
         $meta['description'] = $item['title'];
         $meta['description_overwrite'] = 'N';
         $meta['title'] = $item['title'];
         $meta['title_overwrite'] = 'N';
         $meta['url'] = BackendBlogModel::getURLForCategory(\SpoonFilter::urlise($item['title']));
         // update
         $item['id'] = BackendBlogModel::insertCategory($item, $meta);
         // output
         $this->output(self::OK, $item, vsprintf(BL::msg('AddedCategory'), array($item['title'])));
     }
 }
Exemplo n.º 2
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // validate fields
         $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired'));
         // validate meta
         $this->meta->validate();
         // no errors?
         if ($this->frm->isCorrect()) {
             // build item
             $item['title'] = $this->frm->getField('title')->getValue();
             $item['language'] = BL::getWorkingLanguage();
             $item['meta_id'] = $this->meta->save();
             // insert the item
             $item['id'] = BackendBlogModel::insertCategory($item);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_add_category', array('item' => $item));
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('Categories') . '&report=added-category&var=' . rawurlencode($item['title']) . '&highlight=row-' . $item['id']);
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Handle the category of a post
  *
  * We'll check if the category exists in the fork blog module, and create it if it doesn't.
  *
  * @param string $category The post category
  * @return int
  */
 private function handleCategory($category = '')
 {
     // Does a category with this name exist?
     /* @var \SpoonDatabase $db */
     $db = BackendModel::getContainer()->get('database');
     $id = (int) $db->getVar('SELECT id FROM blog_categories WHERE title=? AND language=?', array($category, BL::getWorkingLanguage()));
     // We found an id!
     if ($id > 0) {
         return $id;
     }
     // Return default if we got an empty string
     if (trim($category) == '') {
         return 2;
     }
     // We should create a new category
     $cat = array();
     $cat['language'] = BL::getWorkingLanguage();
     $cat['title'] = $category;
     $meta = array();
     $meta['keywords'] = $category;
     $meta['description'] = $category;
     $meta['title'] = $category;
     $meta['url'] = $category;
     return Model::insertCategory($cat, $meta);
 }