Phundament App Class Reference
  • Package
  • Class
  • Tree

Packages

  • bootstrap
    • widgets
      • input
  • Image
  • None
  • p3admin
  • p3extensions
    • behaviors
    • commands
    • components
    • helpers
    • widgets
  • p3media
    • actions
    • controllers
    • models
  • p3pages
    • models
  • p3widgets
    • components
    • models
  • PHP
  • system
    • db
      • ar
    • gii
    • web
      • auth
      • helpers
      • widgets
  • yiiext
    • widgets
      • fancybox
      • lipsum
  • zii
    • widgets
      • grid

Classes

  • ActivationController
  • AdminController
  • AssignmentController
  • AssignmentForm
  • AuthChildForm
  • AuthItemController
  • AuthItemForm
  • BaseP3Widget
  • BaseP3WidgetMeta
  • BaseP3WidgetTranslation
  • Bootstrap
  • CImageComponent
  • CKEditor
  • CodeProvider
  • DefaultController
  • EChosen
  • EDbMigration
  • EditableField
  • EditableSaver
  • EMigrateCommand
  • ESelect2
  • EUserFlash
  • FullCrudFieldProvider
  • GenerateForm
  • IdentificationColumnValidator
  • InstallController
  • JSONEditorView
  • JuiJSONEditorInput
  • LoginController
  • LogoutController
  • Metadata
  • P3CrudFieldProvider
  • P3MediaController
  • P3MediaMetaController
  • P3MediaSelect
  • P3PageController
  • P3PageMetaController
  • P3PageTranslationController
  • P3Widget
  • P3WidgetController
  • P3WidgetMetaController
  • P3WidgetTranslation
  • P3WidgetTranslationController
  • ProfileController
  • ProfileFieldController
  • RAuthItemBehavior
  • RAuthItemChildDataProvider
  • RAuthItemDataProvider
  • RAuthItemParentDataProvider
  • RAuthorizer
  • RController
  • RDbAuthManager
  • RecoveryController
  • RegistrationController
  • RegistrationForm
  • RGenerator
  • Rights
  • RightsFilter
  • RightsModule
  • RInstaller
  • RPermissionDataProvider
  • RUserBehavior
  • RWebUser
  • UActiveForm
  • UploadHandler
  • UserChangePassword
  • UserController
  • UserIdentity
  • UserLogin
  • UserModule
  • UserRecoveryForm
  • UWdropDownDep
  • UWfile
  • UWjuiAutoComplete
  • UWjuidate
  • UWrelBelongsTo
  • WebUserBehavior
  1 <?php
  2 /**
  3  * EditableSaver class file.
  4  * 
  5  * This component is servar-side part for editable widgets. It performs update of one model attribute.
  6  * 
  7  * @author Vitaliy Potapov <noginsk@rambler.ru>
  8  * @link https://github.com/vitalets/yii-bootstrap-editable
  9  * @copyright Copyright &copy; Vitaliy Potapov 2012
 10  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 11  * @version 1.0.0
 12  */
 13  
 14 class EditableSaver extends CComponent
 15 {
 16     /**
 17      * scenarion used in model for update
 18      *
 19      * @var mixed
 20      */
 21     public $scenario = 'editable';
 22 
 23     /**
 24      * name of model
 25      *
 26      * @var mixed
 27      */
 28     public $modelClass;
 29     /**
 30      * primaryKey value
 31      *
 32      * @var mixed
 33      */
 34     public $primaryKey;
 35     /**
 36      * name of attribute to be updated
 37      *
 38      * @var mixed
 39      */
 40     public $attribute;
 41     /**
 42      * model instance
 43      *
 44      * @var CActiveRecord
 45      */
 46     public $model;
 47 
 48     /**
 49      * http status code ruterned for errors
 50     */
 51     public $errorHttpCode = 400;
 52 
 53     /**
 54     * name of changed attributes. Used when saving model
 55     * 
 56     * @var mixed
 57     */
 58     protected $changedAttributes = array();
 59     
 60     /**
 61      * Constructor
 62      *
 63      * @param mixed $modelName
 64      * @return EditableBackend
 65      */
 66     public function __construct($modelClass)
 67     {
 68         if (empty($modelClass)) {
 69             throw new CException(Yii::t('editable', 'You should provide modelClass in constructor of EditableSaver.'));
 70         }
 71         $this->modelClass = ucfirst($modelClass);
 72     }
 73 
 74     /**
 75      * main function called to update column in database
 76      *
 77      */
 78     public function update()
 79     {
 80         //set params from request
 81         $this->primaryKey = yii::app()->request->getParam('pk');
 82         $this->attribute = yii::app()->request->getParam('name');
 83         $value = yii::app()->request->getParam('value');
 84 
 85         //checking params
 86         if (empty($this->attribute)) {
 87             throw new CException(Yii::t('editable','Property "attribute" should be defined.'));
 88         }
 89         if (empty($this->primaryKey)) {
 90             throw new CException(Yii::t('editable','Property "primaryKey" should be defined.'));
 91         }
 92 
 93         //loading model
 94         $this->model = CActiveRecord::model($this->modelClass)->findByPk($this->primaryKey);
 95         if (!$this->model) {
 96             throw new CException(Yii::t('editable', 'Model {class} not found by primary key "{pk}"', array(
 97                '{class}'=>get_class($this->model), '{pk}'=>$this->primaryKey)));
 98         }
 99         $this->model->setScenario($this->scenario);
100         
101         //is attribute exists
102         if (!$this->model->hasAttribute($this->attribute)) {
103             throw new CException(Yii::t('editable', 'Model {class} does not have attribute "{attr}"', array(
104               '{class}'=>get_class($this->model), '{attr}'=>$this->attribute)));            
105         }
106 
107         //is attribute safe
108         if (!$this->model->isAttributeSafe($this->attribute)) {
109             throw new CException(Yii::t('editable', 'Model {class} rules do not allow to update attribute "{attr}"', array(
110               '{class}'=>get_class($this->model), '{attr}'=>$this->attribute))); 
111         }
112 
113         //setting new value
114         $this->setAttribute($this->attribute, $value);
115 
116         //validate
117         $this->model->validate(array($this->attribute));
118         if ($this->model->hasErrors()) {
119             $this->error($this->model->getError($this->attribute));
120         }
121 
122         //save
123         if ($this->beforeUpdate()) {
124             //saving (only chnaged attributes)
125             if ($this->model->save(false, $this->changedAttributes)) {
126                 $this->afterUpdate();
127             } else {
128                 $this->error(Yii::t('editable', 'Error while saving record!')); 
129             }
130         } else {
131             $firstError = reset($this->model->getErrors());
132             $this->error($firstError[0]);
133         }
134     }
135 
136     /**
137      * This event is raised before the update is performed.
138      * @param CModelEvent $event the event parameter
139      */
140     public function onBeforeUpdate($event)
141     {
142         $this->raiseEvent('onBeforeUpdate', $event);
143     }
144 
145     /**
146      * This event is raised after the update is performed.
147      * @param CEvent $event the event parameter
148      */
149     public function onAfterUpdate($event)
150     {
151         $this->raiseEvent('onAfterUpdate', $event);
152     }
153 
154     /**
155      * errors  as CHttpException
156      * @param $msg
157      * @throws CHttpException
158      */
159     protected function error($msg)
160     {
161         throw new CHttpException($this->errorHttpCode, $msg);
162     }
163 
164     /**
165      * beforeUpdate
166      *
167      */
168     protected function beforeUpdate()
169     {
170         $this->onBeforeUpdate(new CEvent($this));
171         return !$this->model->hasErrors();
172     }
173 
174     /**
175      * afterUpdate
176      *
177      */
178     protected function afterUpdate()
179     {
180         $this->onAfterUpdate(new CEvent($this));
181     }
182     
183     /**
184     * setting new value of attribute.
185     * Attrubute name also stored in array to save only changed attributes
186     * 
187     * @param mixed $name
188     * @param mixed $value
189     */
190     public function setAttribute($name, $value)
191     {
192          $this->model->$name = $value;
193          $this->changedAttributes[] = $name;
194          $this->changedAttributes = array_unique($this->changedAttributes);
195     }
196 }
197 
Phundament App Class Reference API documentation generated by ApiGen 2.8.0