Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Contract extends BaseContract
- {
- public $created_date;
- public $last_update;
- /**
- * @return \yii\db\ActiveQuery
- */
- public function accessToAlter() {
- if(Yii::$app->user->isAdmin) {
- return true;
- }
- if($this->status == \common\models\DraftStatusLookup::max()) {
- return false;
- }
- return true;
- }
- public function beforeSave($insert) {
- if (!parent::beforeSave($insert)) {
- return false;
- }
- var_dump(expression)
- if(!$this->accessToAlter()) {
- return false;
- }
- return true;
- }
- public function afterSave($insert, $changedAttributes) {
- parent::afterSave($insert, $changedAttributes);
- // var_dump($changedAttributes);die;
- if(!$insert && isset($changedAttributes['status'])) {
- // case : no active doc, status remain same : save normally
- if(!$this->activeDocument && ($changedAttributes['status'] == $this->oldAttributes['status'])){
- return true;
- }
- // case : status changed : do something
- if(!($changedAttributes['status'] == $this->oldAttributes['status'])){
- // create new draft status
- $draftStatus = new DraftStatus();
- $draftStatus->status_id = $changedAttributes['status'];
- // case : active doc : renew draft_status_id
- if($this->activeDocument) {
- $draftStatus->draft_id = $this->activeDocument->id;
- $draftStatus->status_id = $this->status;
- } else {
- throw new \Exception(' Tiada draf perjanjian aktif');
- }
- if(!$draftStatus->save()){
- throw new \Exception("fail save DS", 1);
- }
- // your code here like $changedAttributes['myField'];
- }
- } else {
- unset($this->status);
- }
- }
- //rename to access
- public function can($operation) {
- switch ($operation) {
- case 'view':
- return true;
- break;
- case 'edit':
- // if(Yii::$app->user->isAdmin || (!($this->can('bypass') || !$this->can('requireHighLevelBypassAuthority')))) {
- // return false;
- // }
- if($this->can('bypass') || !$this->can('requireHighLevelBypassAuthority')) {
- return true;
- }
- break;
- case 'delete':
- $isCreator = $this['created_by'] == Yii::$app->user->username;
- $permisionToDelete = ($isCreator && $this->can('bypass') && !$this->can('requireHighLevelBypassAuthority'));
- if(Yii::$app->user->isAdmin || $permisionToDelete) {
- return true;
- }
- break;
- case 'bypass':
- return (Yii::$app->user->isJuu || Yii::$app->user->isAdmin);
- break;
- case 'requireHighLevelBypassAuthority':
- return ($this['status'] >= Yii::$app->params['settings']['departmentAlterLimit']);
- break;
- default:
- throw new \yii\web\NotSupportedException("Sila tentukan kebenaran operasi.");
- break;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement