Esempio n. 1
0
 public function actionCreate()
 {
     $this->pageTitle = 'Jobstar - Create Reward';
     error_reporting(E_ALL & ~E_NOTICE);
     $model = new Reward();
     $rewardImagesModel = new RewardImages();
     $Tags = new Tags();
     if (isset($_POST['Reward']) && !empty($_POST['Reward'])) {
         $model->attributes = $_POST['Reward'];
         $model->category_id = $_POST['Reward']['category_id'];
         //echo "<pre>";
         //print_r($model->attributes);
         if ($model->save()) {
             //echo "----";
             $useridFolder = $model->id;
             if (isset($_POST['tags'])) {
                 $getTags = $_POST['tags'];
                 $mystring = $getTags;
                 $findme = ',';
                 $pos = strpos($mystring, $findme);
                 if ($pos === false) {
                     //echo "The string '$findme' was not found in the string '$mystring'";
                     //if that tag name already exist, skip that tag
                     $chkTagExist = Tags::model()->find('name = :name', array(':name' => $getTags));
                     if (empty($chkTagExist)) {
                         // echo "Tag not exist";
                         $insertMultiDoc = Yii::app()->db->createCommand()->insert('tags', array('reward_id' => $model->id, 'name' => $getTags));
                     } else {
                         //echo "dont save the record";
                     }
                 } else {
                     //echo "The string '$findme' was found in the string '$mystring'";
                     //echo " and exists at position $pos";
                     //explode with comma
                     $getTagArray = explode(',', $getTags);
                     //insert into tags table with reward model id.
                     foreach ($getTagArray as $tagVal) {
                         //if that tag name already exist, skip that tag
                         $chkTagExist = Tags::model()->find('name = :name', array(':name' => $tagVal));
                         if (empty($chkTagExist)) {
                             // echo "Tag not exist";
                             $insertMultiDoc = Yii::app()->db->createCommand()->insert('tags', array('reward_id' => $model->id, 'name' => $tagVal));
                         } else {
                             //echo "dont save the record";
                         }
                     }
                 }
             }
             //end tags
             $images = CUploadedFile::getInstancesByName('images');
             // proceed if the images have been set
             if (isset($images) && count($images) > 0) {
                 //upload multiple images, first create folder with reward id and store all related images in that folder.
                 // create folder
                 if ($_SERVER['DOCUMENT_ROOT'] == '/var/www/html') {
                     $setDir = $_SERVER['DOCUMENT_ROOT'] . "/images/reward/" . $useridFolder;
                 } else {
                     $setDir = $_SERVER['DOCUMENT_ROOT'] . "/jobstar/images/reward/" . $useridFolder;
                 }
                 if (is_dir($setDir) == false) {
                     mkdir("{$setDir}", 0777, true);
                     chmod("{$setDir}", 0777);
                 }
                 //end
                 //echo  $setDir;
                 //die;
                 // go through each uploaded image
                 $i = 0;
                 foreach ($images as $image => $pic) {
                     if ($i == '0') {
                         //first image will be default
                         $setDefaultStatus = '1';
                     } else {
                         $setDefaultStatus = '0';
                     }
                     //echo $pic->name.'<br />';
                     $finalName = $setDir . "/" . $pic->name;
                     //echo "<br>";
                     if ($pic->saveAs($finalName)) {
                         // add it to the main model now
                         //$img_add = new Picture();
                         //$img_add->filename = $pic->name; //it might be $img_add->name for you, filename is just what I chose to call it in my model
                         //$img_add->topic_id = $model->id; // this links your picture model to the main model (like your user, or profile model)
                         $insertMultiDoc = Yii::app()->db->createCommand()->insert('rewardimages', array('reward_id' => $model->id, 'image' => $pic->name, 'defaultImage' => $setDefaultStatus));
                         //$img_add->save(); // DONE
                     } else {
                         // handle the errors here, if you want
                     }
                     $i++;
                 }
                 //end foreach
             }
             // if image is uploaded by admin
         }
         //die('end');
         $this->redirect(array('admin', 'id' => $model->id));
     }
     //end post method
     $this->render('create', array('model' => $model));
 }
Esempio n. 2
0
 public function actionCreate()
 {
     $this->pageTitle = 'Jobstar - Create Reward';
     error_reporting(E_ALL & ~E_NOTICE);
     $model = new Reward();
     $rewardImagesModel = new RewardImages();
     $Tags = new Tags();
     if (isset($_POST['Reward']) && !empty($_POST['Reward'])) {
         //check product name duplicacy
         $chkTitle = $_POST['Reward']['title'];
         $chkPrice = $_POST['Reward']['price'];
         $chkCategory = $_POST['Reward']['category_id'];
         $chkRewardExist = Reward::model()->find('title = :title', array(':title' => $chkTitle));
         if (!empty($chkRewardExist)) {
             Yii::app()->user->setFlash('errDupTitle', 'Reward name exist in database, please enter another.');
             $this->redirect(array('create', 'titleSet' => $chkTitle, 'priceSet' => $chkPrice, 'categorySet' => $chkCategory));
         }
         //end checking
         //check image is uploaded or not
         $chkImageUploaded = CUploadedFile::getInstancesByName('images');
         if (isset($chkImageUploaded) && count($chkImageUploaded) > 0) {
             //do nothing.... this is done only for validation
         } else {
             //please select image
             Yii::app()->user->setFlash('imageErrMsg', 'Please upload image.');
             $model->attributes = $_POST['Reward'];
             //$this->redirect(array('create'));
             $this->redirect(array('create', 'titleSet' => $chkTitle, 'priceSet' => $chkPrice, 'categorySet' => $chkCategory));
         }
         // end validation
         $model->attributes = $_POST['Reward'];
         $model->category_id = $_POST['Reward']['category_id'];
         $model->status = '1';
         $model->description = $_POST['Reward']['description'];
         //echo "<pre>";
         //print_r($model->attributes);
         if ($model->save()) {
             //echo "----";
             $useridFolder = $model->id;
             if (isset($_POST['tag']) && $_POST['tag'] != '') {
                 $getTags = $_POST['tag'];
                 $mystring = $getTags;
                 $findme = ',';
                 $pos = strpos($mystring, $findme);
                 if ($pos === false) {
                     //echo "The string '$findme' was not found in the string '$mystring'";
                     //if that tag name already exist, skip that tag
                     $chkTagExist = Tags::model()->find('name = :name', array(':name' => $getTags));
                     if (empty($chkTagExist)) {
                         // echo "Tag not exist";
                         $insertMultiDoc = Yii::app()->db->createCommand()->insert('tags', array('reward_id' => $model->id, 'name' => $getTags));
                     } else {
                         //echo "dont save the record";
                     }
                 } else {
                     //echo "The string '$findme' was found in the string '$mystring'";
                     //echo " and exists at position $pos";
                     //explode with comma
                     $getTagArray = explode(',', $getTags);
                     //insert into tags table with reward model id.
                     foreach ($getTagArray as $tagVal) {
                         //if that tag name already exist, skip that tag
                         $chkTagExist = Tags::model()->find('name = :name', array(':name' => $tagVal));
                         if (empty($chkTagExist)) {
                             // echo "Tag not exist";
                             $insertMultiDoc = Yii::app()->db->createCommand()->insert('tags', array('reward_id' => $model->id, 'name' => $tagVal));
                         } else {
                             //echo "dont save the record";
                         }
                     }
                 }
             }
             //end tags
             $images = CUploadedFile::getInstancesByName('images');
             //echo "<pre>";
             //print_r($images);
             // proceed if the images have been set
             if (isset($images) && count($images) > 0) {
                 //upload multiple images, first create folder with reward id and store all related images in that folder.
                 // create folder
                 if ($_SERVER['DOCUMENT_ROOT'] == '/var/www/html') {
                     $setDir = $_SERVER['DOCUMENT_ROOT'] . "/images/reward/" . $useridFolder;
                 } else {
                     $setDir = $_SERVER['DOCUMENT_ROOT'] . "/jobstar/images/reward/" . $useridFolder;
                 }
                 if (is_dir($setDir) == false) {
                     mkdir("{$setDir}", 0777, true);
                     chmod("{$setDir}", 0777);
                 } else {
                     chmod("{$setDir}", 0777);
                 }
                 // go through each uploaded image
                 $i = 0;
                 foreach ($images as $thefile) {
                     if ($i == '0') {
                         //first image will be default
                         $setDefaultStatus = '1';
                     } else {
                         $setDefaultStatus = '0';
                     }
                     //echo $pic->name.'<br />'  error, size;
                     //$finalName = $setDir."/".$pic->name;
                     //echo "<br>";
                     $imagetime = time();
                     $image = $thefile;
                     //$type      = $pic->type;
                     $type = $thefile->extensionName;
                     $destination = $_SERVER['DOCUMENT_ROOT'] . "/jobstar/images/reward/" . $useridFolder;
                     $finalName = $setDir . "/" . $imagetime . "_" . $i;
                     $thumbnailImage = $setDir . "/" . $imagetime . "_" . $i;
                     if ($image->saveAs($finalName . '.' . $type)) {
                         $dbFileName = $imagetime . "_" . $i . '.' . $type;
                         $chkThumb = $this->createimage(270, 200, $thumbnailImage, 'm', $type);
                         if ($chkThumb == 0) {
                             // add it to the main model now
                             $insertMultiDoc = Yii::app()->db->createCommand()->insert('rewardimages', array('reward_id' => $model->id, 'image' => $dbFileName, 'defaultImage' => $setDefaultStatus));
                             $chkThumb = $this->createimage(270, 200, $thumbnailImage, 'm', $type);
                         } else {
                             $this->redirect(array('create'));
                         }
                     } else {
                         // handle the errors here, if you want
                     }
                     $i++;
                 }
                 //end foreach
             } else {
                 //please select image
                 Yii::app()->user->setFlash('imageErrMsg', 'Please upload image.');
             }
         }
         //die('end');
         $this->redirect(array('admin', 'id' => $model->id));
     }
     //end post method
     $this->render('create', array('model' => $model));
 }