Advertisement
kidzen

Untitled

Oct 12th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.39 KB | None | 0 0
  1. class Contract extends BaseContract
  2. {
  3.     public $created_date;
  4.     public $last_update;
  5.     /**
  6.      * @return \yii\db\ActiveQuery
  7.      */
  8.  
  9.     public function accessToAlter() {
  10.         if(Yii::$app->user->isAdmin) {
  11.             return true;
  12.         }
  13.         if($this->status == \common\models\DraftStatusLookup::max()) {
  14.             return false;
  15.         }
  16.         return  true;
  17.     }
  18.     public function beforeSave($insert) {
  19.         if (!parent::beforeSave($insert)) {
  20.             return false;
  21.         }
  22.         var_dump(expression)
  23.         if(!$this->accessToAlter()) {
  24.             return false;
  25.         }
  26.  
  27.         return true;
  28.     }
  29.     public function afterSave($insert, $changedAttributes) {
  30.         parent::afterSave($insert, $changedAttributes);
  31.         // var_dump($changedAttributes);die;
  32.  
  33.         if(!$insert && isset($changedAttributes['status'])) {
  34.         // case : no active doc, status remain same : save normally
  35.             if(!$this->activeDocument && ($changedAttributes['status'] == $this->oldAttributes['status'])){
  36.                 return true;
  37.             }
  38.         // case : status changed : do something
  39.             if(!($changedAttributes['status'] == $this->oldAttributes['status'])){
  40.             // create new draft status
  41.                 $draftStatus = new DraftStatus();
  42.                 $draftStatus->status_id = $changedAttributes['status'];
  43.  
  44.             // case : active doc : renew draft_status_id
  45.                 if($this->activeDocument) {
  46.                     $draftStatus->draft_id = $this->activeDocument->id;
  47.                     $draftStatus->status_id = $this->status;
  48.                 } else {
  49.                     throw new \Exception(' Tiada draf perjanjian aktif');
  50.  
  51.                 }
  52.                 if(!$draftStatus->save()){
  53.                     throw new \Exception("fail save DS", 1);
  54.  
  55.                 }
  56.             // your code here like $changedAttributes['myField'];
  57.             }
  58.         } else {
  59.             unset($this->status);
  60.         }
  61.     }
  62.     //rename to access
  63.     public function can($operation) {
  64.         switch ($operation) {
  65.             case 'view':
  66.                 return true;
  67.             break;
  68.             case 'edit':
  69.                 // if(Yii::$app->user->isAdmin || (!($this->can('bypass') || !$this->can('requireHighLevelBypassAuthority')))) {
  70.                 //     return false;
  71.                 // }
  72.                 if($this->can('bypass') || !$this->can('requireHighLevelBypassAuthority')) {
  73.                     return true;
  74.                 }
  75.             break;
  76.             case 'delete':
  77.                 $isCreator = $this['created_by'] == Yii::$app->user->username;
  78.                 $permisionToDelete = ($isCreator && $this->can('bypass') && !$this->can('requireHighLevelBypassAuthority'));
  79.                 if(Yii::$app->user->isAdmin || $permisionToDelete) {
  80.                     return true;
  81.                 }
  82.             break;
  83.             case 'bypass':
  84.                 return (Yii::$app->user->isJuu || Yii::$app->user->isAdmin);
  85.             break;
  86.             case 'requireHighLevelBypassAuthority':
  87.                 return  ($this['status'] >= Yii::$app->params['settings']['departmentAlterLimit']);
  88.             break;
  89.             default:
  90.                 throw new \yii\web\NotSupportedException("Sila tentukan kebenaran operasi.");
  91.             break;
  92.         }
  93.         return false;
  94.  
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement