/**
  * @param CActiveRecord $model
  * @param string        $default
  * @return array|mixed|null|string
  */
 public static function guessObjectName(CActiveRecord $model, $default = '?')
 {
     if (isset($model->name)) {
         return $model->name;
     }
     if (isset($model->title)) {
         return $model->title;
     }
     $pk = $model->getPrimaryKey();
     if (is_array($pk)) {
         // Clé composite, on laisse tomber pour le moment
         return $default;
     }
     if (is_null($pk)) {
         return $default;
     }
     return $pk;
 }
Example #2
0
 /**
  * Compares current active record with another one.
  * The comparison is made by comparing table name and the primary key values of the two active records.
  * @param CActiveRecord $record record to compare to
  * @return boolean whether the two active records refer to the same row in the database table.
  */
 public function equals($record)
 {
     return $this->tableName() === $record->tableName() && $this->getPrimaryKey() === $record->getPrimaryKey();
 }
 /**
  * Является ли текущий объект потомком для модели из параметров
  * @param DaActiveRecord $model
  */
 public function isDescendant(CActiveRecord $model, $checkSelf = false)
 {
     if ($checkSelf && $this->owner->getPrimaryKey() == $model->getPrimaryKey()) {
         return true;
     }
     return $this->getParentById($model->getPrimaryKey()) !== null;
 }
Example #4
0
 /**
  *### .getPrimaryKey()
  *
  * Helper function to return the primary key of the $data
  * IMPORTANT: composite keys on CActiveDataProviders will return the keys joined by comma
  *
  * @param CActiveRecord $data
  *
  * @return null|string
  */
 protected function getPrimaryKey($data)
 {
     if ($this->dataProvider instanceof CActiveDataProvider) {
         $key = $this->dataProvider->keyAttribute === null ? $data->getPrimaryKey() : $data->{$this->keyAttribute};
         return is_array($key) ? implode(',', $key) : $key;
     }
     if ($this->dataProvider instanceof CArrayDataProvider || $this->dataProvider instanceof CSqlDataProvider) {
         return is_object($data) ? $data->{$this->dataProvider->keyField} : $data[$this->dataProvider->keyField];
     }
     return null;
 }
Example #5
0
 /**
  * Переопределенная функция сохранения пользователя.
  * Помимо сохранения данных пользователя, присваиваются выбранные права доступа
  * 
  * @param type $runValidation
  * @param type $attributes
  * @throws CHttpException
  */
 public function Save($runValidation = true, $attributes = null)
 {
     /* $selectedRows - массив выбранных ролей пользователя */
     $selectedRows = (string) filter_input(INPUT_POST, 'items') !== '' ? json_decode((string) filter_input(INPUT_POST, 'items'), true) : [];
     /* $_POST['oper'] может быть "edit" или "add", редактирование или добавление новой записи */
     if ((string) filter_input(INPUT_POST, 'oper') === '') {
         throw new CHttpException(500, 'Отсутствует POST переменная "oper"');
     }
     /* ИД пользователя, который редактируется, если необходим */
     $editid = (string) filter_input(INPUT_POST, 'editid');
     if ((string) filter_input(INPUT_POST, 'oper') === 'edit' && $editid === '') {
         throw new CHttpException(500, 'Отсутствует POST переменная "editid"');
     }
     /* Имя сценария, может быть "NewUser" - проверяет введеный пароль и подтверждение на совпадение.
      * Используется при добавлении нового пользотваеля, или смене пароля */
     $scenario = (string) filter_input(INPUT_POST, 'scenario');
     if ($scenario === 'NewUser') {
         $this->password = CPasswordHelper::hashPassword($this->password);
     }
     /* Хэшируем новый пароль */
     /* Сохраняем модель пользователя и присваиваем роли */
     if (parent::save($runValidation, $attributes) !== false) {
         $auth = Yii::app()->authManager;
         /* Удаляем все роли пользователя при сохранении изменений профиля пользователя */
         if ($editid !== '' && $scenario === '') {
             $assigned_roles = Yii::app()->authManager->getAuthAssignments($editid);
             foreach (array_keys($assigned_roles) as $AuthItem) {
                 $auth->revoke($AuthItem, $editid);
             }
         }
         /* Присваиваем выбранные роли пользователю */
         if (count((array) $selectedRows) > 0) {
             foreach (array_keys($selectedRows) as $AuthItem) {
                 $auth->assign($AuthItem, parent::getPrimaryKey());
             }
             Yii::app()->authManager->save();
         }
     }
 }
 /**
  * @param CActiveRecord $model
  * @param $type
  */
 protected function deleteRecord(CActiveRecord $model, $type)
 {
     Yii::app()->db->createCommand()->delete('{{exchange1c}}', 'object_id=:object_id AND object_type=:object_type', array(':object_id' => $model->getPrimaryKey(), ':object_type' => $type));
 }
Example #7
0
 public function afterSave()
 {
     $this->_originalKey = parent::getPrimaryKey();
     $result = parent::afterSave();
     return $result;
 }
Example #8
0
 /**
  * Переопределенная функция сохранения группы LDAP.
  * Помимо сохранения данных группы LDAP, присваиваются выбранные права доступа
  * 
  * @param type $runValidation
  * @param type $attributes
  * @throws CHttpException
  */
 public function Save($runValidation = true, $attributes = null)
 {
     /* $selectedRows - массив выбранных ролей группы LDAP */
     $selectedRows = (string) filter_input(INPUT_POST, 'items') !== '' ? json_decode((string) filter_input(INPUT_POST, 'items'), true) : [];
     /* $_POST['oper'] может быть "edit" или "add", редактирование или добавление новой записи */
     if ((string) filter_input(INPUT_POST, 'oper') === '') {
         throw new CHttpException(500, 'Отсутствует POST переменная "oper"');
     }
     /* ИД группы LDAP, который редактируется, если необходим */
     $editid = (string) filter_input(INPUT_POST, 'editid');
     $oper = (string) filter_input(INPUT_POST, 'oper');
     if ($oper === 'edit' && $editid === '') {
         throw new CHttpException(500, 'Отсутствует POST переменная "editid"');
     }
     /* Сохраняем модель группы LDAP и присваиваем роли */
     if (parent::save($runValidation, $attributes) !== false) {
         /*  $auth = Yii::app()->authManager; */
         /* Удаляем все роли группы LDAP при сохранении изменений профиля групп LDAP */
         if ($editid !== '') {
             AuthAssignmentLdap::model()->deleteAll('groupid = :groupid', array(':groupid' => $editid));
         }
         /* Присваиваем выбранные роли группе LDAP */
         if ($editid === '' && $oper === 'add') {
             $editid = parent::getPrimaryKey();
         }
         if (count((array) $selectedRows) > 0) {
             foreach (array_keys($selectedRows) as $AuthItem) {
                 $model = new AuthAssignmentLdap();
                 $model->itemname = $AuthItem;
                 $model->groupid = $editid;
                 $model->save();
             }
         }
     }
 }
 /**
  * Resolves primary key attributes (name => value).
  * 
  * @param CActiveRecord $record record to resolve primary key
  * 
  * @return array primary key attributes
  */
 private static function _resolvePrimaryAttributes($record)
 {
     $key = $record->getMetaData()->tableSchema->primaryKey;
     $primaryAttributes = $record->getPrimaryKey();
     if (is_array($key)) {
         return $primaryAttributes;
     } else {
         return array($key => $primaryAttributes);
     }
 }