public function afterSave($event) { parent::afterSave($event); // do not auto-create meta data information for meta data table itself (recursion). if ($this->metaDataRelation == '_self_') { return true; } // create new meta data record or just update modifiedBy/At columns if ($this->resolveMetaDataModel() === null) { $metaClassName = $this->owner->getActiveRelation($this->metaDataRelation)->className; $metaModel = new $metaClassName(); $metaModel->id = $this->owner->id; $metaModel->status = self::STATUS_ACTIVE; $metaModel->language = Yii::app()->language; $metaModel->owner = Yii::app()->user->id; $primaryRole = key(Yii::app()->authManager->getRoles(Yii::app()->user->id)); $metaModel->checkAccessDelete = $primaryRole; $metaModel->createdAt = date('Y-m-d H:i:s'); $metaModel->createdBy = Yii::app()->user->id; $metaModel->model = get_class($this->owner); } else { $metaModel = $this->resolveMetaDataModel(); $metaModel->modifiedAt = date('Y-m-d H:i:s'); $metaModel->modifiedBy = Yii::app()->user->id; } $metaModel->save(); return true; }
public function afterSave($event) { $relations = $this->getRelations(); foreach ($relations as $relation) { if ($relation['type'] == CActiveRecord::MANY_MANY) { $forAdd = array_diff($relation['value'], $relation['oldValue']); foreach ($forAdd as $id) { if ($id) { $sql = 'INSERT INTO `' . $relation['m2mTable'] . '` (`' . $relation['m2mThisField'] . '`, `' . $relation['m2mForeignField'] . '`) VALUES (:this_field, :foreign_field)'; Yii::app()->getDb()->createCommand($sql)->bindValues(array('this_field' => $this->getOwner()->id, ':foreign_field' => $id))->execute(); } } $forRemove = array_diff($relation['oldValue'], $relation['value']); foreach ($forRemove as $id) { if ($id) { $sql = 'DELETE IGNORE FROM `' . $relation['m2mTable'] . '` WHERE `' . $relation['m2mThisField'] . '` = :this_field AND `' . $relation['m2mForeignField'] . '` = :foreign_field'; Yii::app()->getDb()->createCommand($sql)->bindValues(array('this_field' => $this->getOwner()->id, ':foreign_field' => $id))->execute(); } } } } //Yii::app()->end(); parent::afterSave($event); return true; }
public function afterSave($event) { if (Yii::app() instanceof CWebApplication) { if (Yii::app()->user->getState(UserIdentity::STATE_AUDIT_TRAIL, true)) { $isNewRecord = $this->getOwner()->getIsNewRecord(); UserActivityLog::model()->addRecord(array('user_id' => Yii::app()->user->id, 'ip' => Common::getIp(), 'action' => $isNewRecord ? 'create' : 'update', 'activity' => $this->prepareString($isNewRecord ? $this->createTemplate : $this->updateTemplate))); } } return parent::afterSave($event); }
public function afterSave($event) { return parent::afterSave($event); if ($this->owner->isNewRecord) { if (array_key_exists(get_class($this->owner), self::$handlers)) { $method = self::$handlers[get_class($this->owner)]['create']; $this->{$method}(); } } }
public function afterSave($event) { parent::afterSave($event); if (!$this->owner->isNewRecord) { $videoCache = Yii::app()->cache->get('Video::' . $this->owner->youtubeId); $videoCache['description'] = $this->owner->description; $videoCache['title'] = $this->owner->title; Yii::app()->cache->set('Video::' . $this->owner->youtubeId, $videoCache); } }
public function afterSave($event) { $model = $this->getOwner(); if ($model->isNewRecord) { foreach ($this->findAllAttaches() as $attach) { $attach->object_id = $model->id; $attach->save(); } } return parent::afterSave($event); }
/** * @param CEvent $event */ public function afterSave($event) { // restore values of attributes saved in _convertAttributesToDB() if (count($this->_backupAttributes)) { foreach ($this->_backupAttributes as $name => $value) { $this->getOwner()->{$name} = $value; } $this->_backupAttributes = []; } parent::afterSave($event); }
public function afterSave($event) { $allowedFields = $this->allowed; $ignoredFields = $this->ignored; $ignoredClasses = $this->ignored_class; $newattributes = $this->getOwner()->getAttributes(); $oldattributes = $this->getOldAttributes(); // Lets check if the whole class should be ignored if (sizeof($ignoredClasses) > 0) { if (array_search(get_class($this->getOwner()), $ignoredClasses) !== false) { return; } } // Lets unset fields which are not allowed if (sizeof($allowedFields) > 0) { foreach ($newattributes as $f => $v) { if (array_search($f, $allowedFields) === false) { unset($newattributes[$f]); } } foreach ($oldattributes as $f => $v) { if (array_search($f, $allowedFields) === false) { unset($oldattributes[$f]); } } } // Lets unset fields which are ignored if (sizeof($ignoredFields) > 0) { foreach ($newattributes as $f => $v) { if (array_search($f, $ignoredFields) !== false) { unset($newattributes[$f]); } } foreach ($oldattributes as $f => $v) { if (array_search($f, $ignoredFields) !== false) { unset($oldattributes[$f]); } } } // If no difference then WHY? // There is some kind of problem here that means "0" and 1 do not diff for array_diff so beware: stackoverflow.com/questions/12004231/php-array-diff-weirdness :S if (count(array_diff_assoc($newattributes, $oldattributes)) <= 0) { return; } // If this is a new record lets add a CREATE notification if ($this->getOwner()->getIsNewRecord()) { $this->leaveTrail('CREATE'); } // Now lets actually write the attributes $this->auditAttributes($newattributes, $oldattributes); // Reset old attributes to handle the case with the same model instance updated multiple times $this->setOldAttributes($this->getOwner()->getAttributes()); return parent::afterSave($event); }
public function afterSave($event) { $owner = $this->getOwner(); if ($owner->getIsNewRecord() && $owner->send_email) { $this->accountDetailsEmail(); } if ($owner->getScenario() === Users::SCENARIO_RESET_PASSWORD && $owner->send_email) { $this->newPasswordEmail(); } if ($owner->getIsNewRecord() && $owner->getScenario() === Users::SCENARIO_SIGNUP) { $this->sendAccActivationEmail(); } return parent::afterSave($event); }
public function afterSave($event) { $attributes = Yii::app()->request->getParam('MetaTag'); if ($attributes) { $meta_tag = MetaTag::model()->findByAttributes(array('object_id' => $this->owner->id, 'model_id' => get_class($this->owner))); if (!$meta_tag) { $meta_tag = new MetaTag(); } $attributes['object_id'] = $this->owner->id; $attributes['model_id'] = get_class($this->owner); $meta_tag->attributes = $attributes; $meta_tag->save(); } return parent::afterSave($event); }
public function afterSave($event) { parent::afterSave($event); // Save all languages updated via setTranslatedFields(). if ($this->autoSave) { foreach ($this->updates as $language => $callback) { $callback[0]->model_id = $this->owner->id; if (!call_user_func($callback)) { throw new \Exception("Model callback failed."); } } if (isset($this->_current)) { $this->_current->save(); } } $this->backupValues(); if (isset($this->_current)) { $this->setLanguage($this->_current->language); } }
public function afterSave($event) { parent::afterSave($event); $this->processAttributes($event->sender, self::DIRECTION_CLIENT); }
/** * Provides saving related HAS_ONE records. * * @param CEvent $event event object * * @see CActiveRecordBehavior::afterSave() * * @throws Exception */ public function afterSave($event) { $owner = $this->getOwner(); foreach ($owner->relations() as $key => $relation) { $isHasOne = $relation['0'] == CActiveRecord::HAS_ONE; if (!$isHasOne) { continue; } if (isset($this->relations[$key]) || in_array($key, $this->relations)) { $config = $this->_getRelationConfig($key); $owner->{$key}->{$relation}[2] = $owner->primaryKey; if ($config['createRelated'] && !$owner->{$key}) { $related = new $relation[1](); $related->{$relation}[2] = $owner->primaryKey; } else { $related = $owner->{$key}; } if ($config['saveRelated'] && !$related->save()) { $message = 'Can not save related record. ' . CHtml::errorSummary($related); throw new CException($message); } } } parent::afterSave($event); }
/** * Сохраняем историю */ public function afterSave($event) { parent::afterSave($event); if ($this->saveHistory && $this->waitForSave) { $table = $this->historyTable ? $this->historyTable : $this->owner->tableName() . "_events_history"; $builder = Yii::app()->db->schema->commandBuilder; $builder->createMultipleInsertCommand($table, $this->waitForSave)->execute(); } }
/** * Update model translations */ public function afterSave($event) { Tag::model()->updateFrequency($this->_oldTags, $this->getOwner()->tags); return parent::afterSave($event); }
public function afterSave($event) { parent::afterSave($event); $newNames = array(); $i = 0; foreach ($this->fields as $field) { if ($this->owner->{$field} instanceof CUploadedFile) { //generating the new filename and subtituting it in the entry $fileName = $newNames[$field] = preg_replace('/\\*([a-zA-Z_]+[a-zA-Z0-9_]*)\\*/e', '\\$this->owner->$1', $this->nameMask) . '.jpg'; if ($this->prependFileName) { $fileName = $field . '_' . $fileName; } $cropper = new ImageCropper(); //creating the original image if ($this->resize) { $cropper->resize_and_crop($this->owner->{$field}->tempName, self::getImagePath($fileName), $this->resizeTo[$i][0], $this->resizeTo[$i][1], $this->fileQuality, true); } else { $this->owner->{$field}->saveAs(self::getImagePath($fileName)); } //and now, the thumbnail if ($this->hasThumb) { $thumbName = $this->generateThumbName($fileName); $cropper->resize_and_crop(self::getImagePath($fileName), self::getImagePath($thumbName), $this->thumbSize[$i][0], $this->thumbSize[$i][1], $this->thumbQuality, true); } } ++$i; } //now we need to update the entry with the new filename if (sizeof($newNames) > 0) { $reg = $this->owner->findByPk($this->owner->primaryKey); foreach ($newNames as $field => $newName) { $reg->{$field} = $newName; } $reg->save(false); } }
public function afterSave($event) { $oldAttributes = $this->owner->getOldAttributes(); $linkFields = Fields::model()->findAllByAttributes(array('modelName' => get_class($this->owner), 'type' => 'link')); foreach ($linkFields as $field) { $nameAndId = Fields::nameAndId($this->owner->getAttribute($field->fieldName)); $oldNameAndId = Fields::nameAndId(isset($oldAttributes[$field->fieldName]) ? $oldAttributes[$field->fieldName] : ''); if (!empty($oldNameAndId[1]) && $nameAndId[1] !== $oldNameAndId[1]) { $oldTarget = X2Model::model($field->linkType)->findByPk($oldNameAndId[1]); $this->owner->deleteRelationship($oldTarget); } $newTarget = X2Model::model($field->linkType)->findByPk($nameAndId[1]); $this->owner->createRelationship($newTarget); } parent::afterSave($event); }
public function afterSave($event) { if (!is_array($this->ignoreRelations)) { throw new CException('ignoreRelations of CAdvancedArBehavior needs to be an array'); } $this->writeManyManyTables(); parent::afterSave($event); return true; }
public function afterSave($event) { parent::afterSave($event); $this->createFullHistory(); }
public function afterSave($event) { Yii::log("go on loggable behaviors", CLogger::LEVEL_ERROR); try { $username = Yii::app()->user->nom; $userid = Yii::app()->user->name; } catch (Exception $e) { //If we have no user object, this must be a command line program $username = '******'; $userid = null; } if (empty($username)) { $username = '******'; } if (empty($userid)) { $userid = null; } $newattributes = $this->Owner->getAttributes(); $oldattributes = $this->getOldAttributes(); if (!$this->Owner->isNewRecord) { // compare old and new foreach ($newattributes as $name => $value) { if (!empty($oldattributes) && isset($oldattributes[$name])) { $old = $oldattributes[$name]; } elseif (!empty($oldattributes) && !isset($oldattributes[$name])) { $old = 'Undefined : new attribute'; } else { $old = ''; } if ($value != $old) { // if (is_string($value) && ($value != $old)) { $log = new AuditTrail(); $log->old_value = $old; if (is_string($value)) { $log->new_value = $value; } else { $log->new_value = json_decode(json_encode($value)); } $log->action = 'CHANGE'; $log->model = get_class($this->Owner); $log->model_id = $this->Owner->getPrimaryKey(); $log->field = $name; $log->stamp = date('Y-m-d H:i:s'); $log->user_id = $userid; $log->save(); } } } else { $log = new AuditTrail(); $log->old_value = ''; $log->new_value = ''; $log->action = 'CREATE'; $log->model = get_class($this->Owner); $log->model_id = $this->Owner->_id; $log->field = 'N/A'; $log->stamp = date('Y-m-d H:i:s'); $log->user_id = $userid; $log->save(); foreach ($newattributes as $name => $value) { $log = new AuditTrail(); $log->old_value = ''; if (is_string($value)) { $log->new_value = $value; } else { $log->new_value = json_decode(json_encode($value)); } $log->action = 'SET'; $log->model = get_class($this->Owner); $log->model_id = $this->Owner->getPrimaryKey(); $log->field = $name; $log->stamp = date('Y-m-d H:i:s'); $log->user_id = $userid; $log->save(); } } return parent::afterSave($event); }
public function afterSave($event) { $this->fixRelations(); $this->writeManyManyTables(); return parent::afterSave($event); }
/** * Saves model tags on model save. * @param CModelEvent $event * @throw Exception */ public function afterSave($event) { if ($this->needToSave()) { $builder = $this->getConnection()->getCommandBuilder(); if (!$this->createTagsAutomatically) { // checking if all of the tags are existing ones foreach ($this->tags as $tag) { $findCriteria = new CDbCriteria(array('select' => "t." . $this->tagTablePk, 'params' => array(':tag' => $tag))); if (!$this->tagTableCondition instanceof CDbExpression) { $findCriteria->addCondition("t.{$this->tagTableName} = :tag "); } else { $findCriteria->addCondition($this->tagTableCondition->__toString()); } if ($this->getScopeCriteria()) { $findCriteria->mergeWith($this->getScopeCriteria()); } $tagId = $builder->createFindCommand($this->tagTable, $findCriteria)->queryScalar(); if (!$tagId) { throw new Exception("Tag \"{$tag}\" does not exist. Please add it before assigning or enable createTagsAutomatically."); } } } if (!$this->getOwner()->getIsNewRecord()) { // delete all present tag bindings if record is existing one $this->deleteTags(); } // add new tag bindings and tags if there are any if (!empty($this->tags)) { foreach ($this->tags as $tag) { if (empty($tag)) { return; } // try to get existing tag $findCriteria = new CDbCriteria(array('select' => "t." . $this->tagTablePk, 'params' => array(':tag' => $tag))); if (!$this->tagTableCondition instanceof CDbExpression) { $findCriteria->addCondition("t.{$this->tagTableName} = :tag "); } else { $findCriteria->addCondition($this->tagTableCondition->__toString()); } if ($this->getScopeCriteria()) { $findCriteria->mergeWith($this->getScopeCriteria()); } $tagId = $builder->createFindCommand($this->tagTable, $findCriteria)->queryScalar(); // if there is no existing tag, create one if (!$tagId) { $this->createTag($tag); // reset all tags cache $this->resetAllTagsCache(); $this->resetAllTagsWithModelsCountCache(); $tagId = $this->getConnection()->getLastInsertID(); } // bind tag to it's model $builder->createInsertCommand($this->getTagBindingTableName(), array($this->getModelTableFkName() => $this->getOwner()->primaryKey, $this->tagBindingTableTagId => $tagId))->execute(); } $this->updateCount(+1); } $this->cache->set($this->getCacheKey(), $this->tags); } parent::afterSave($event); }
/** * @param CEvent * @return void */ public function afterSave($event) { // TODO afterSave не срабатывает если модель не была изменена // Save changed attributes. if ($this->changedAttributes->count > 0) { $this->saveEavAttributes($this->changedAttributes->toArray()); } // Call parent method for convenience. parent::afterSave($event); }
public function afterSave($event) { $allowedFields = $this->allowed; $ignoredFields = $this->ignored; $ignoredClasses = $this->ignored_class; $newattributes = $this->getOwner()->getAttributes(); $oldattributes = $this->_oldAttributes; // Lets check if the whole class should be ignored if (sizeof($ignoredClasses) > 0) { if (array_search(get_class($this->getOwner()), $ignoredClasses) !== false) { return; } } // Lets unset fields which are not allowed if (sizeof($allowedFields) > 0) { foreach ($newattributes as $f => $v) { if (array_search($f, $allowedFields) === false) { unset($newattributes[$f]); } } foreach ($oldattributes as $f => $v) { if (array_search($f, $allowedFields) === false) { unset($oldattributes[$f]); } } } // Lets unset fields which are ignored if (sizeof($ignoredFields) > 0) { foreach ($newattributes as $f => $v) { if (array_search($f, $ignoredFields) !== false) { unset($newattributes[$f]); } } foreach ($oldattributes as $f => $v) { if (array_search($f, $ignoredFields) !== false) { unset($oldattributes[$f]); } } } // If no difference then WHY? // There is some kind of problem here that means "0" and 1 do not diff for array_diff so beware: stackoverflow.com/questions/12004231/php-array-diff-weirdness :S if (count(array_diff_assoc($newattributes, $oldattributes)) <= 0) { return parent::afterSave($event); } // If this is a new record lets add a CREATE notification if ($this->getOwner()->getIsNewRecord()) { $this->leaveTrail('CREATE'); } // Now lets actually write the attributes $this->auditAttributes($newattributes, $oldattributes); // Reset old attributes to handle the case with the same model instance updated multiple times $this->_oldAttributes = $this->getOwner()->getAttributes(); // get additional data for save logs $model = get_class($this->getOwner()); // Gets a plain text version of the model name $model_id = $this->getNormalizedPk(); $user_id = $this->getUserId(); // Lets get the $stamp = $this->storeTimestamp ? time() : date($this->dateFormat); // If we are storing a timestamp lets get one else lets get the date //save logs as bulk $log = new AuditTrail(); $log->saveBulk($this->log_records, $model, $user_id, $model_id, $stamp); $this->log_records = []; return parent::afterSave($event); }
public function afterSave($event) { parent::afterSave($event); $this->writeManyManyTables(); return true; }
public function afterSave($event) { try { $username = Yii::app()->user->Name; $userid = Yii::app()->user->id; } catch (Exception $e) { //If we have no user object, this must be a command line program $username = "******"; $userid = null; } if (empty($username)) { $username = "******"; } if (empty($userid)) { $userid = null; } $newattributes = $this->Owner->getAttributes(); $oldattributes = $this->getOldAttributes(); if (!$this->Owner->isNewRecord) { // compare old and new foreach ($newattributes as $name => $value) { if (!empty($oldattributes)) { $old = $oldattributes[$name]; } else { $old = ''; } if ($value != $old) { $log = new AuditTrail(); $log->old_value = $old; $log->new_value = $value; $log->action = 'CHANGE'; $log->model = get_class($this->Owner); $log->model_id = $this->Owner->getPrimaryKey(); $log->field = $name; $log->stamp = date('Y-m-d H:i:s'); $log->user_id = $userid; $log->save(); } } } else { $log = new AuditTrail(); $log->old_value = ''; $log->new_value = ''; $log->action = 'CREATE'; $log->model = get_class($this->Owner); $log->model_id = $this->Owner->getPrimaryKey(); $log->field = 'N/A'; $log->stamp = date('Y-m-d H:i:s'); $log->user_id = $userid; $log->save(); foreach ($newattributes as $name => $value) { $log = new AuditTrail(); $log->old_value = ''; $log->new_value = $value; $log->action = 'SET'; $log->model = get_class($this->Owner); $log->model_id = $this->Owner->getPrimaryKey(); $log->field = $name; $log->stamp = date('Y-m-d H:i:s'); $log->user_id = $userid; $log->save(); } } return parent::afterSave($event); }
/** * Find changes to the model and save them as AuditField records * Do not call this method directly, it will be called after the model is saved. * @param CModelEvent $event */ public function afterSave($event) { if (!$this->enableAuditField) { parent::afterSave($event); return; } $date = time(); $newAttributes = $this->owner->attributes; $oldAttributes = $this->_dbAttributes; $auditModels = $this->getAuditModels(); $auditRequestId = $this->getAuditRequestId(); $auditFields = array(); $userId = Yii::app()->user && Yii::app()->user->id ? Yii::app()->user->id : 0; // insert if ($this->owner->isNewRecord) { foreach ($newAttributes as $name => $new) { if (in_array($name, $this->ignoreFields['insert'])) { continue; } // prepare the values $new = trim($new); if (!$new) { continue; } // prepare the logs foreach ($auditModels as $auditModel) { if (isset($auditModel['ignoreFields']) && in_array($name, $auditModel['ignoreFields'])) { continue; } $auditFields[] = array('old_value' => '', 'new_value' => $new, 'action' => 'INSERT', 'model_name' => $auditModel['model_name'], 'model_id' => $auditModel['model_id'], 'field' => $auditModel['prefix'] . $name, 'created' => $date, 'user_id' => $userId, 'audit_request_id' => $auditRequestId); } } } else { // compare old and new foreach ($newAttributes as $name => $new) { if (in_array($name, $this->ignoreFields['update'])) { continue; } // prepare the values $old = !empty($oldAttributes) ? trim($oldAttributes[$name]) : ''; $new = trim($new); if (in_array($old, $this->ignoreValues)) { $old = ''; } if (in_array($new, $this->ignoreValues)) { $new = ''; } if ($new == $old) { continue; } // prepare the logs foreach ($auditModels as $auditModel) { if (isset($auditModel['ignoreFields']) && in_array($name, $auditModel['ignoreFields'])) { continue; } $auditFields[] = array('old_value' => $old, 'new_value' => $new, 'action' => 'UPDATE', 'model_name' => $auditModel['model_name'], 'model_id' => $auditModel['model_id'], 'field' => $auditModel['prefix'] . $name, 'created' => $date, 'user_id' => $userId, 'audit_request_id' => $auditRequestId); } } } // insert the audit_field records $this->addAuditFields($auditFields); // set the dbAttributes to the new values $this->_dbAttributes = $this->owner->attributes; parent::afterSave($event); }
public function afterSave($event) { parent::afterSave($event); $this->handleCollection(); }
public function afterSave($event) { $imageId = $this->getImageId(); if ($this->_imageId != $imageId) { foreach ($this->versions as $version => $config) { $oldPath = $this->getFilePath($version, $this->_imageId); $newPath = $this->getFilePath($version, $imageId); if (file_exists($oldPath)) { rename($oldPath, $newPath); } } } parent::afterSave($event); }
/** * @param CEvent $event */ public function afterSave($event) { if (empty($this->owner->elastic_update_after_save) || $this->owner->elastic_update_after_save) { $this->elasticDelete(); $this->indexElasticDocument(); } parent::afterSave($event); }