Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\models;
- use Yii;
- use yii\web\UploadedFile;
- /**
- * This is the model class for table "contactperson".
- *
- * @property int $id
- * @property int $account_id
- * @property int|null $salutation
- * @property string|null $name
- * @property string|null $role
- * @property string|null $image_filename
- * @property string|null $phone
- * @property string|null $phone_mobile
- * @property string|null $email
- *
- * @property Account $account
- * @property Group $group
- */
- class Contactperson extends \yii\db\ActiveRecord
- {
- /* @var UploadedFile */
- public $image;
- /* @var bool */
- public $uploaded = false;
- const SCENARIO_UPLOAD_ONLY = "upload";
- const SCENARIO_SAVE = "save";
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- $scenarios[self::SCENARIO_UPLOAD_ONLY] = ['image', 'image_filename'];
- $scenarios[self::SCENARIO_SAVE] = ['account_id', 'salutation', 'name', 'role', 'image_filename', 'phone', 'phone_mobile', 'email'];
- return $scenarios;
- }
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'contactperson';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- ['image', 'image', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg'],
- [['account_id', 'name', 'role', 'email'], 'required', 'on' => self::SCENARIO_SAVE],
- [['account_id', 'salutation'], 'integer'],
- [['name', 'role'], 'string', 'max' => 128],
- [['image_filename'], 'string', 'max' => 255],
- [['phone', 'phone_mobile'], 'string', 'max' => 32],
- [['email'], 'string', 'max' => 96],
- ['email', 'email'],
- [['account_id'], 'exist', 'skipOnError' => false, 'targetClass' => Account::class, 'targetAttribute' => ['account_id' => 'id']],
- //['account_id', 'default', 'value' => Yii::$app->account->data->id],
- ];
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getAccount()
- {
- return $this->hasOne(Account::class, ['id' => 'account_id']);
- }
- /**
- * @return \yii\db\ActiveQuery
- * @throws \yii\base\InvalidConfigException
- */
- public function getGroup()
- {
- return $this->hasOne(Group::class, ['id' => 'group_id'])->viaTable('group_contactperson', ['contactperson_id'=>'id']);
- }
- public function upload(){
- if( $this->validate() ){
- $newFilename = $this->image->baseName.'.'.$this->image->extension;
- //delete old image if it has not the same filename
- if( !empty($this->image_filename) ){
- if( $this->image_filename !== $newFilename ){
- unlink( Yii::getAlias('@webroot') .'/'.$this->account_id.'/uploads/'.$this->image_filename );
- }
- }
- $this->image->saveAs(Yii::getAlias('@webroot') .'/'.$this->account_id.'/uploads/'.$newFilename);
- //process image with IMAGINE to fit sizes
- //generate a small image
- $this->image_filename = $newFilename;
- $this->uploaded = true;
- }else{
- $this->uploaded = false;
- }
- }
- public function getImagePath(){
- if (!empty($this->image_filename)) {
- $profileImagePath = Yii::getAlias('@web') . '/' . $this->account_id . '/uploads/' . $this->image_filename;
- } else {
- $profileImagePath = '/img/default_contact_profileimage.png';
- }
- return $profileImagePath;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement