Advertisement
Guest User

Model (example)

a guest
Dec 30th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.62 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5. use Yii;
  6. use yii\web\UploadedFile;
  7.  
  8. /**
  9.  * This is the model class for table "contactperson".
  10.  *
  11.  * @property int $id
  12.  * @property int $account_id
  13.  * @property int|null $salutation
  14.  * @property string|null $name
  15.  * @property string|null $role
  16.  * @property string|null $image_filename
  17.  * @property string|null $phone
  18.  * @property string|null $phone_mobile
  19.  * @property string|null $email
  20.  *
  21.  * @property Account $account
  22.  * @property Group $group
  23.  */
  24. class Contactperson extends \yii\db\ActiveRecord
  25. {
  26.     /* @var UploadedFile */
  27.     public $image;
  28.     /* @var bool */
  29.     public $uploaded = false;
  30.  
  31.     const SCENARIO_UPLOAD_ONLY = "upload";
  32.     const SCENARIO_SAVE = "save";
  33.  
  34.     public function scenarios()
  35.     {
  36.         $scenarios = parent::scenarios();
  37.         $scenarios[self::SCENARIO_UPLOAD_ONLY] = ['image', 'image_filename'];
  38.         $scenarios[self::SCENARIO_SAVE] = ['account_id', 'salutation', 'name', 'role', 'image_filename', 'phone', 'phone_mobile', 'email'];
  39.         return $scenarios;
  40.     }
  41.  
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public static function tableName()
  46.     {
  47.         return 'contactperson';
  48.     }
  49.  
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function rules()
  54.     {
  55.         return [
  56.             ['image', 'image', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg'],
  57.  
  58.             [['account_id', 'name', 'role', 'email'], 'required', 'on' => self::SCENARIO_SAVE],
  59.             [['account_id', 'salutation'], 'integer'],
  60.             [['name', 'role'], 'string', 'max' => 128],
  61.             [['image_filename'], 'string', 'max' => 255],
  62.             [['phone', 'phone_mobile'], 'string', 'max' => 32],
  63.             [['email'], 'string', 'max' => 96],
  64.             ['email', 'email'],
  65.             [['account_id'], 'exist', 'skipOnError' => false, 'targetClass' => Account::class, 'targetAttribute' => ['account_id' => 'id']],
  66.             //['account_id', 'default', 'value' => Yii::$app->account->data->id],
  67.         ];
  68.     }
  69.  
  70.  
  71.     /**
  72.      * @return \yii\db\ActiveQuery
  73.      */
  74.     public function getAccount()
  75.     {
  76.         return $this->hasOne(Account::class, ['id' => 'account_id']);
  77.     }
  78.  
  79.     /**
  80.      * @return \yii\db\ActiveQuery
  81.      * @throws \yii\base\InvalidConfigException
  82.      */
  83.     public function getGroup()
  84.     {
  85.         return $this->hasOne(Group::class, ['id' => 'group_id'])->viaTable('group_contactperson', ['contactperson_id'=>'id']);
  86.     }
  87.  
  88.     public function upload(){
  89.         if( $this->validate() ){
  90.             $newFilename = $this->image->baseName.'.'.$this->image->extension;
  91.             //delete old image if it has not the same filename
  92.             if( !empty($this->image_filename) ){
  93.                 if( $this->image_filename !== $newFilename ){
  94.                     unlink( Yii::getAlias('@webroot') .'/'.$this->account_id.'/uploads/'.$this->image_filename );
  95.                 }
  96.             }
  97.  
  98.             $this->image->saveAs(Yii::getAlias('@webroot') .'/'.$this->account_id.'/uploads/'.$newFilename);
  99.             //process image with IMAGINE to fit sizes
  100.             //generate a small image
  101.             $this->image_filename = $newFilename;
  102.             $this->uploaded = true;
  103.         }else{
  104.             $this->uploaded = false;
  105.         }
  106.     }
  107.  
  108.     public function getImagePath(){
  109.         if (!empty($this->image_filename)) {
  110.             $profileImagePath = Yii::getAlias('@web') . '/' . $this->account_id . '/uploads/' . $this->image_filename;
  111.         } else {
  112.             $profileImagePath = '/img/default_contact_profileimage.png';
  113.         }
  114.         return $profileImagePath;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement