Backend\Modules\Locale\Engine\Model::existsByName PHP Method

existsByName() public static method

Does a locale exists by its name.
public static existsByName ( string $name, string $type, string $module, string $language, string $application, integer $id = null ) : boolean
$name string The name of the locale.
$type string The type of the locale.
$module string The module wherein will be searched.
$language string The language to use.
$application string The application wherein will be searched.
$id integer The id to exclude in the check.
return boolean
    public static function existsByName($name, $type, $module, $language, $application, $id = null)
    {
        $name = (string) $name;
        $type = (string) $type;
        $module = (string) $module;
        $language = (string) $language;
        $application = (string) $application;
        $id = $id !== null ? (int) $id : null;
        // get db
        $db = BackendModel::getContainer()->get('database');
        // return
        if ($id !== null) {
            return (bool) $db->getVar('SELECT 1
                 FROM locale
                 WHERE name = ? AND type = ? AND module = ? AND language = ? AND application = ? AND id != ?
                 LIMIT 1', array($name, $type, $module, $language, $application, $id));
        }
        return (bool) BackendModel::getContainer()->get('database')->getVar('SELECT 1
             FROM locale
             WHERE name = ? AND type = ? AND module = ? AND language = ? AND application = ?
             LIMIT 1', array($name, $type, $module, $language, $application));
    }

Usage Example

Example #1
0
 /**
  * Execute the action
  */
 public function execute()
 {
     parent::execute();
     $isGod = BackendAuthentication::getUser()->isGod();
     // get possible languages
     if ($isGod) {
         $possibleLanguages = array_unique(array_merge(BL::getWorkingLanguages(), BL::getInterfaceLanguages()));
     } else {
         $possibleLanguages = BL::getWorkingLanguages();
     }
     // get parameters
     $language = \SpoonFilter::getPostValue('language', array_keys($possibleLanguages), null, 'string');
     $module = \SpoonFilter::getPostValue('module', BackendModel::getModules(), null, 'string');
     $name = \SpoonFilter::getPostValue('name', null, null, 'string');
     $type = \SpoonFilter::getPostValue('type', BackendModel::getContainer()->get('database')->getEnumValues('locale', 'type'), null, 'string');
     $application = \SpoonFilter::getPostValue('application', array('Backend', 'Frontend'), null, 'string');
     $value = \SpoonFilter::getPostValue('value', null, null, 'string');
     // validate values
     if (trim($value) == '' || $language == '' || $module == '' || $type == '' || $application == '' || $application == 'Frontend' && $module != 'Core') {
         $error = BL::err('InvalidValue');
     }
     // in case this is a 'act' type, there are special rules concerning possible values
     if ($type == 'act' && !isset($error)) {
         if (urlencode($value) != CommonUri::getUrl($value)) {
             $error = BL::err('InvalidActionValue', $this->getModule());
         }
     }
     // no error?
     if (!isset($error)) {
         // build item
         $item['language'] = $language;
         $item['module'] = $module;
         $item['name'] = $name;
         $item['type'] = $type;
         $item['application'] = $application;
         $item['value'] = $value;
         $item['edited_on'] = BackendModel::getUTCDate();
         $item['user_id'] = BackendAuthentication::getUser()->getUserId();
         // does the translation exist?
         if (BackendLocaleModel::existsByName($name, $type, $module, $language, $application)) {
             // add the id to the item
             $item['id'] = (int) BackendLocaleModel::getByName($name, $type, $module, $language, $application);
             // update in db
             BackendLocaleModel::update($item);
         } else {
             // insert in db
             BackendLocaleModel::insert($item);
         }
         // output OK
         $this->output(self::OK);
     } else {
         $this->output(self::ERROR, null, $error);
     }
 }
All Usage Examples Of Backend\Modules\Locale\Engine\Model::existsByName