Beispiel #1
0
 /**
  * @inheritdoc
  */
 public function beforeSave($insert)
 {
     $attributes = array_keys($this->getAttributes());
     // ID
     if ($this->isNewRecord and empty($this->_id)) {
         $this->_id = uniqid();
     }
     // SEO
     if (in_array('title', $attributes) and in_array('seo_url', $attributes) and empty($this->seo_url)) {
         $this->seo_url = StringHelper::asUrl($this->title);
     }
     if (in_array('title', $attributes) and in_array('seo_title', $attributes) and empty($this->seo_title)) {
         $this->seo_title = $this->title;
     }
     if (in_array('description', $attributes) and in_array('seo_desc', $attributes) and empty($this->seo_desc)) {
         $this->seo_desc = $this->description;
     }
     // Upload image
     if (in_array('image', $attributes)) {
         $image = \yii\web\UploadedFile::getInstance($this, 'image');
         if (!empty($image)) {
             $this->image = \yii\web\UploadedFile::getInstance($this, 'image');
             $ext = FileHelper::getExtention($this->image);
             if (!empty($ext)) {
                 $fileDir = Yii::$app->controller->module->id . '/' . date('Y/m/d/');
                 if (property_exists($this, 'title')) {
                     $title = $this->title;
                 } elseif (property_exists($this, 'name')) {
                     $title = $this->name;
                 } else {
                     $title = uniqid();
                 }
                 $fileName = StringHelper::asUrl($title) . '.' . $ext;
                 $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir;
                 FileHelper::createDirectory($folder);
                 $this->image->saveAs($folder . $fileName);
                 $this->image = $fileDir . $fileName;
             }
         } else {
             $this->image = $this->image_old;
         }
     }
     // creator, editor and time
     $now = new MongoDate();
     if (in_array('update_time', $attributes) and empty($this->update_time)) {
         $this->update_time = $now;
     }
     if (in_array('editor', $attributes) and !ClientHelper::isCommandLine()) {
         $this->editor = Yii::$app->user->id;
     }
     if ($this->isNewRecord) {
         if (in_array('creator', $attributes) and !ClientHelper::isCommandLine()) {
             $this->creator = Yii::$app->user->id;
         }
         if (in_array('create_time', $attributes) and $this->create_time == null) {
             $this->create_time = $now;
         }
     }
     return parent::beforeSave($insert);
 }
Beispiel #2
0
 public function beforeSave($insert)
 {
     $attributes = array_keys($this->getAttributes());
     // ID
     if ($this->isNewRecord and empty($this->_id)) {
         $this->_id = uniqid();
     }
     // SEO
     if (in_array('title', $attributes) and in_array('seo_url', $attributes) and empty($this->seo_url)) {
         $this->seo_url = StringHelper::asUrl($this->title);
     }
     if (in_array('title', $attributes) and in_array('seo_title', $attributes) and empty($this->seo_title)) {
         $this->seo_title = $this->title;
     }
     if (in_array('description', $attributes) and in_array('seo_desc', $attributes) and empty($this->seo_desc)) {
         $this->seo_desc = $this->description;
     }
     // Upload image
     $image = \yii\web\UploadedFile::getInstance($this, 'image');
     if (!empty($image)) {
         $this->image = \yii\web\UploadedFile::getInstance($this, 'image');
         $ext = FileHelper::getExtention($this->image);
         if (!empty($ext)) {
             $fileDir = LetHelper::getAvatarDir($this->primaryKey) . '/';
             $fileName = $this->primaryKey . '.jpg';
             //$fileName = $this->primaryKey . '.' . $ext;
             $folder = Yii::$app->params['uploadPath'] . '/' . Yii::$app->params['uploadDir'] . '/' . $fileDir;
             FileHelper::createDirectory($folder);
             $this->image->saveAs($folder . $fileName);
             $this->image = $fileDir . $fileName;
         }
     } else {
         $this->image = $this->image_old;
     }
     // creator, editor and time
     $now = new \MongoDate();
     if (in_array('update_time', $attributes) and empty($this->update_time)) {
         $this->update_time = $now;
     }
     if (in_array('editor', $attributes) and !ClientHelper::isCommandLine()) {
         $this->editor = Yii::$app->user->id;
     }
     if ($this->isNewRecord) {
         if (in_array('creator', $attributes) and !ClientHelper::isCommandLine()) {
             $this->creator = Yii::$app->user->id;
         }
         if (in_array('create_time', $attributes) and $this->create_time == null) {
             $this->create_time = $now;
         }
     }
     if (!empty($this->password)) {
         $this->password_hash = Yii::$app->getSecurity()->generatePasswordHash($this->password);
     }
     if ($this->isNewRecord) {
         $this->auth_key = bin2hex(Yii::$app->getSecurity()->generateRandomKey());
         $this->auth_key = substr($this->auth_key, 0, 32);
     }
     return true;
 }
Beispiel #3
0
 private static function fetchJSON($apiUrl)
 {
     ClientHelper::msgToConsole(' - fetchJSON:');
     $ch = curl_init();
     $timeout = 5;
     curl_setopt($ch, CURLOPT_URL, $apiUrl);
     // curl_setopt($ch, CURLOPT_POST, TRUE); // Use POST method
     // curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=1&var2=2&var3=3"); // Define POST values
     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
     $json = curl_exec($ch);
     $curl_errno = curl_errno($ch);
     $curl_error = curl_error($ch);
     curl_close($ch);
     // return $json;
     // Errors?
     if ($curl_errno > 0) {
         $data['error']['message'] = 'cURL Error ' . $curl_errno . ': ' . $curl_error;
         ClientHelper::msgToConsole('FAIL');
     } else {
         // Decode the JSON response
         $data = json_decode($json, TRUE);
         ClientHelper::msgToConsole('Success');
     }
     return $data;
 }