Backend\Modules\Blog\Engine\Model::getURLForCategory PHP Method

getURLForCategory() public static method

Retrieve the unique URL for a category
public static getURLForCategory ( string $url, integer $id = null ) : string
$url string The string whereon the URL will be based.
$id integer The id of the category to ignore.
return string
    public static function getURLForCategory($url, $id = null)
    {
        // redefine URL
        $url = (string) $url;
        // get db
        $db = BackendModel::getContainer()->get('database');
        // new category
        if ($id === null) {
            // already exists
            if ((bool) $db->getVar('SELECT 1
                 FROM blog_categories AS i
                 INNER JOIN meta AS m ON i.meta_id = m.id
                 WHERE i.language = ? AND m.url = ?
                 LIMIT 1', array(BL::getWorkingLanguage(), $url))) {
                $url = BackendModel::addNumber($url);
                return self::getURLForCategory($url);
            }
        } else {
            // current category should be excluded
            if ((bool) $db->getVar('SELECT 1
                 FROM blog_categories AS i
                 INNER JOIN meta AS m ON i.meta_id = m.id
                 WHERE i.language = ? AND m.url = ? AND i.id != ?
                 LIMIT 1', array(BL::getWorkingLanguage(), $url, $id))) {
                $url = BackendModel::addNumber($url);
                return self::getURLForCategory($url, $id);
            }
        }
        return $url;
    }

Usage Example

Ejemplo 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'])));
     }
 }