Пример #1
0
 /**
  *### .init()
  *
  * initialization of widget
  *
  */
 public function init()
 {
     if (!$this->model) {
         throw new CException('Parameter "model" should be provided for EditableField');
     }
     if (!$this->attribute) {
         throw new CException('Parameter "attribute" should be provided for EditableField');
     }
     // set name
     $this->name = $this->attribute;
     parent::init();
     // set value
     $this->value = $this->model->{$this->attribute};
     /**
      * set data-pk only for existing records
      */
     if (!$this->model->isNewRecord) {
         $this->pk = is_array($this->model->primaryKey) ? CJSON::encode($this->model->primaryKey) : $this->model->primaryKey;
     }
     $originalText = strlen($this->text) ? $this->text : CHtml::value($this->model, $this->attribute);
     /**
      * if apply set manually to false --> just render text, no js plugin applied
      */
     if ($this->apply === false) {
         $this->text = $originalText;
         return;
     } else {
         $this->apply = true;
     }
     /**
      * resolve model and attribute for related model
      */
     $resolved = self::resolveModel($this->model, $this->attribute);
     if ($resolved === false) {
         //cannot resolve related model (maybe no related models for this record)
         $this->apply = false;
         $this->text = $originalText;
         return;
     } else {
         list($this->model, $this->attribute) = $resolved;
     }
     /**
      * for security reason only safe attributes can be editable (e.g. defined in rules of model)
      * just print text (see 'run' method)
      */
     if (!$this->model->isAttributeSafe($this->attribute)) {
         $this->apply = false;
         $this->text = $originalText;
         return;
     }
     /**
      * try to detect type from metadata if not set
      */
     if ($this->type === null) {
         $this->type = 'text';
         if (array_key_exists($this->attribute, $this->model->tableSchema->columns)) {
             $dbType = $this->model->tableSchema->columns[$this->attribute]->dbType;
             if ($dbType == 'date' || $dbType == 'datetime') {
                 $this->type = 'date';
             }
             if (stripos($dbType, 'text') !== false) {
                 $this->type = 'textarea';
             }
         }
     }
     /**
      * If text not defined, generate it from model attribute for types except lists ('select', 'checklist' etc)
      * For lists keep it empty to apply autotext
      */
     if (!strlen($this->text) && !$this->_prepareToAutoText) {
         $this->text = $originalText;
     }
 }
Пример #2
0
 /**
  * initialization of widget
  *
  */
 public function init()
 {
     if (!$this->model) {
         throw new CException('Parameter "model" should be provided for TbEditableField');
     }
     if (!$this->attribute) {
         throw new CException('Parameter "attribute" should be provided for TbEditableField');
     }
     $originalModel = $this->model;
     $originalAttribute = $this->attribute;
     $originalText = strlen($this->text) ? $this->text : CHtml::value($this->model, $this->attribute);
     //if apply set manually to false --> just render text, no js plugin applied
     if ($this->apply === false) {
         $this->text = $originalText;
     } else {
         $this->apply = true;
     }
     //try to resolve related model (if attribute contains '.')
     $resolved = $this->resolveModels($this->model, $this->attribute);
     $this->model = $resolved['model'];
     $this->attribute = $resolved['attribute'];
     $this->staticModel = $resolved['staticModel'];
     $staticModel = $this->staticModel;
     $isMongo = $resolved['isMongo'];
     $isFormModel = $this->model instanceof CFormModel;
     //if real (related) model not exists --> just print text
     if (!$this->model) {
         $this->apply = false;
         $this->text = $originalText;
     }
     //for security reason only safe attributes can be editable (e.g. defined in rules of model)
     //just print text (see 'run' method)
     if (!$staticModel->isAttributeSafe($this->attribute)) {
         $this->apply = false;
         $this->text = $originalText;
     }
     /*
      try to detect type from metadata if not set
     */
     if ($this->type === null) {
         $this->type = 'text';
         if (!$isMongo && !$isFormModel && array_key_exists($this->attribute, $staticModel->tableSchema->columns)) {
             $dbType = $staticModel->tableSchema->columns[$this->attribute]->dbType;
             if ($dbType == 'date') {
                 $this->type = 'date';
             }
             if ($dbType == 'datetime') {
                 $this->type = 'datetime';
             }
             if (stripos($dbType, 'text') !== false) {
                 $this->type = 'textarea';
             }
         }
     }
     //name
     if (empty($this->name)) {
         $this->name = $isMongo ? $originalAttribute : $this->attribute;
     }
     //pk (for mongo takes pk from parent!)
     $pkModel = $isMongo ? $originalModel : $this->model;
     if (!$isFormModel) {
         if ($pkModel && !$pkModel->isNewRecord) {
             $this->pk = $pkModel->primaryKey;
         }
     } else {
         //formModel does not have pk, so set `send` option to `always` (send without pk)
         if (empty($this->send) && empty($this->options['send'])) {
             $this->send = 'always';
         }
     }
     parent::init();
     /*
      If text not defined, generate it from model attribute for types except lists ('select', 'checklist' etc)
      For lists keep it empty to apply autotext.
      $this->_prepareToAutotext calculated in parent class TbEditable.php
     */
     if (!strlen($this->text) && !$this->_prepareToAutotext) {
         $this->text = $originalText;
     }
     //set value directly for autotext generation
     if ($this->model && $this->_prepareToAutotext) {
         $this->value = CHtml::value($this->model, $this->attribute);
     }
     //generate title from attribute label
     if ($this->title === null) {
         $titles = array('Select' => array('select', 'date'), 'Check' => array('checklist'));
         $title = Yii::t('TbEditableField.editable', 'Enter');
         foreach ($titles as $t => $types) {
             if (in_array($this->type, $types)) {
                 $title = Yii::t('TbEditableField.editable', $t);
             }
         }
         $this->title = $title . ' ' . $staticModel->getAttributeLabel($this->attribute);
     } else {
         $this->title = strtr($this->title, array('{label}' => $staticModel->getAttributeLabel($this->attribute)));
     }
     //scenario
     if ($pkModel && !isset($this->params['scenario'])) {
         $this->params['scenario'] = $pkModel->getScenario();
     }
 }