Example #1
0
 /**
  * Creates a new Ad model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Ad();
     $imagesModel = new UploadImage();
     if ($model->load(Yii::$app->request->post())) {
         $imagesModel->image_files = UploadedFile::getInstances($imagesModel, 'image_files');
         if (empty($imagesModel->image_files) || ($model->uploadedImages = $imagesModel->upload(\app\models\AdImage::IMAGES_PATH))) {
             $model->save();
             Yii::$app->session->setFlash('success', "Ad <strong>{$model->title}</strong> created.");
         } else {
             Yii::$app->session->setFlash('danger', "Could not create new Ad.");
         }
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model, 'imagesModel' => $imagesModel]);
     }
 }
Example #2
0
 /**
  * Creates a new Ad model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Ad();
     $imagesModel = new UploadImage();
     $userModel;
     if (Yii::$app->user->isGuest) {
         $userModel = new User();
     } else {
         $userModel = Yii::$app->user->getIdentity();
     }
     if ($model->load(Yii::$app->request->post())) {
         $imagesModel->image_files = UploadedFile::getInstances($imagesModel, 'image_files');
         if (empty($imagesModel->image_files) || ($model->uploadedImages = $imagesModel->upload(\app\models\AdImage::IMAGES_PATH))) {
             $userData = Yii::$app->request->post('User');
             if (Yii::$app->user->isGuest) {
                 $user = User::findOne(['email' => $userData['email']]);
                 if (is_null($user)) {
                     $user = new User();
                     $user->load(Yii::$app->request->post());
                     $password = yii\helpers\BaseStringHelper::truncate($userData['first_name'], 18);
                     $user->password = $password;
                     $user->password_repeat = $password;
                     $user->validate();
                     $model->user_id = $user->id;
                 } else {
                     return $this->redirect(['user/login']);
                 }
                 exit;
             } else {
                 $model->user_id = Yii::$app->user->id;
             }
             $model->date_created = date('Y-m-d H:i');
             $model->date_expired = date('Y-m-d H:i');
             if ($model->save()) {
                 Yii::$app->session->setFlash('success', "Ad <strong>{$model->title}</strong> created.");
             } else {
                 Yii::$app->session->setFlash('danger', "Could not create new Ad.");
             }
         } else {
             Yii::$app->session->setFlash('danger', "Could not create new Ad.");
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'imagesModel' => $imagesModel, 'userModel' => $userModel]);
     }
 }
Example #3
0
 /**
  * Retrieve emails
  * @return 
  */
 public function get()
 {
     set_time_limit($this->timeLimit);
     /**
      * Perform search if it was not performed
      */
     if ($this->isDefaultSeach) {
         $this->search();
     }
     $count = 1;
     $emailsFound = array();
     // Get the latest email we retrieved
     $latestAd = (int) Ad::max('message_number');
     // If the latest email retrieve is same as the latest email
     // at the emap server at the moment, then stop by here
     rsort($this->emails);
     if ($latestAd == reset($this->emails)) {
         $this->close();
         return [];
     }
     // Change back to the old order
     rsort($this->emails);
     /* for every email... */
     foreach ($this->emails as $email_number) {
         $ad = new Ad();
         // Get headers
         $email = $this->getHeaders($email_number);
         try {
             $ad->owner = trim(str_replace('[kigalilife]', '', $email->from[0]->personal));
             $ad->subject = trim(str_replace('[kigalilife]', '', $email->subject));
             $ad->message_id = $email->message_id;
             $ad->message_number = $email_number;
             $ad->sent_date = $email->date;
             $ad->recieved_date = $email->maildate;
             $ad->sender_address = $email->senderaddress;
             $ad->from_address = trim(substr($email->fromaddress, 1, strpos($email->fromaddress, '[kigalilife]') - 1));
             $ad->size = $email->size;
             $ad->udate = $email->udate;
             // Get mail number
             $email->message_number = $email_number;
             // Get body of the current message
             $body_data = imap_fetchbody($this->connection, $email_number, "");
             // Get clean body
             $ad->body = trim($this->getBody($body_data));
             // Get attachments if they are inline
             $email->attachments = $this->getAttachments($body_data, $email_number);
             // Add more attachments if any attachment that is not inline exist
             $otherAttachemnts = $this->fetchAttachments($email_number);
             if (!empty($otherAttachemnts)) {
                 $email->attachments[] = $otherAttachemnts;
             }
             // Save all this emails information
             $emailsFound[$email_number] = $email;
             $ad->attachments = json_encode($email->attachments);
             $ad->save();
         } catch (\Exception $e) {
             print $e->getMessage();
         }
         if ($count++ >= $this->max_emails) {
             break;
         }
     }
     $this->close();
     return $emailsFound;
 }