Backend\Modules\Tags\Engine\Model::getURL PHP Method

getURL() public static method

Get a unique URL for a tag
public static getURL ( string $url, integer $id = null ) : string
$url string The URL to use as a base.
$id integer The ID to ignore.
return string
    public static function getURL($url, $id = null)
    {
        $url = CommonUri::getUrl((string) $url);
        $language = BL::getWorkingLanguage();
        // get db
        $db = BackendModel::getContainer()->get('database');
        // no specific id
        if ($id === null) {
            // get number of tags with the specified url
            $number = (int) $db->getVar('SELECT 1
                 FROM tags AS i
                 WHERE i.url = ? AND i.language = ?
                 LIMIT 1', array($url, $language));
            // there are items so, call this method again.
            if ($number != 0) {
                // add a number
                $url = BackendModel::addNumber($url);
                // recall this method, but with a new url
                $url = self::getURL($url, $id);
            }
        } else {
            // specific id given
            // get number of tags with the specified url
            $number = (int) $db->getVar('SELECT 1
                 FROM tags AS i
                 WHERE i.url = ? AND i.language = ? AND i.id != ?
                 LIMIT 1', array($url, $language, $id));
            // there are items so, call this method again.
            if ($number != 0) {
                // add a number
                $url = BackendModel::addNumber($url);
                // recall this method, but with a new url
                $url = self::getURL($url, $id);
            }
        }
        return $url;
    }

Usage Example

Example #1
0
 /**
  * Execute the action
  */
 public function execute()
 {
     parent::execute();
     // get parameters
     $id = \SpoonFilter::getPostValue('id', null, 0, 'int');
     $tag = trim(\SpoonFilter::getPostValue('value', null, '', 'string'));
     // validate id
     if ($id === 0) {
         $this->output(self::BAD_REQUEST, null, 'no id provided');
     } else {
         // validate tag name
         if ($tag === '') {
             $this->output(self::BAD_REQUEST, null, BL::err('NameIsRequired'));
         } else {
             // check if tag exists
             if (BackendTagsModel::existsTag($tag)) {
                 $this->output(self::BAD_REQUEST, null, BL::err('TagAlreadyExists'));
             } else {
                 $item['id'] = $id;
                 $item['tag'] = \SpoonFilter::htmlspecialchars($tag);
                 $item['url'] = BackendTagsModel::getURL(CommonUri::getUrl(\SpoonFilter::htmlspecialcharsDecode($item['tag'])), $id);
                 BackendTagsModel::update($item);
                 $this->output(self::OK, $item, vsprintf(BL::msg('Edited'), array($item['tag'])));
             }
         }
     }
 }
All Usage Examples Of Backend\Modules\Tags\Engine\Model::getURL