Пример #1
0
 /**
  * Set the field's name
  *
  * @param string $name
  *
  * @return Field $this
  * @throws WireException
  *
  */
 public function setName($name)
 {
     $name = $this->fuel('sanitizer')->fieldName($name);
     if (Fields::isNativeName($name)) {
         throw new WireException("Field may not be named '{$name}' because it is a reserved word");
     }
     if ($this->fuel('fields') && ($f = $this->fuel('fields')->get($name)) && $f->id != $this->id) {
         throw new WireException("Field may not be named '{$name}' because it is already used by another field");
     }
     if (strpos($name, '__') !== false) {
         throw new WireException("Field name '{$name}' may not have double underscores because this usage is reserved by the core");
     }
     if ($this->settings['name'] != $name) {
         if ($this->settings['name'] && $this->settings['flags'] & Field::flagSystem) {
             throw new WireException("You may not change the name of field '{$this->settings['name']}' because it is a system field.");
         }
         $this->trackChange('name');
         if ($this->settings['name']) {
             $this->prevTable = $this->getTable();
         }
         // so that Fields can perform a table rename
     }
     $this->settings['name'] = $name;
     return $this;
 }
 /**
  * Encodes a sortfield from a fieldname to a signed integer (ID) representing a custom field, or native field name
  *
  * The returned value will be a negative value (or string preceded by a dash) if the sortfield is reversed. 
  *
  * @param string $sortfield
  * @return string|int
  *
  */
 public function encode($sortfield)
 {
     $reverse = false;
     if (substr($sortfield, 0, 1) == '-') {
         $reverse = true;
         $sortfield = substr($sortfield, 1);
     }
     if ($sortfield && !Fields::isNativeName($sortfield)) {
         if ($field = $this->fuel('fields')->get($sortfield)) {
             $sortfield = $field->id;
         } else {
             $sortfield = '';
         }
     }
     if ($sortfield) {
         if ($reverse) {
             $sortfield = "-{$sortfield}";
         }
     } else {
         $sortfield = 'sort';
     }
     return $sortfield;
 }