Example #1
0
 /**
  * Checks if the product is new, and sets the created and modified times appropriately,
  * prior to saving.
  * @see testProjectModel::beforeSave()
  */
 public function beforeSave()
 {
     if ($this->isNew) {
         $this->created = date("Y-m-d H:i:s");
         $this->modified = $this->created;
         $this->user = Auth::User()->id;
     } else {
         $this->modified = date("Y-m-d H:i:s");
     }
     $tags = isset($this->attributes['tags']) ? $this->attributes['tags'] : array();
     foreach ($tags as $i => $tag) {
         if (!is_numeric($tag)) {
             $tagModel = new tags(array('name' => $tag));
             $tagModel->save();
             $tags[$i] = $tagModel->id;
         } else {
             $tags[$i] = intval($tag);
         }
     }
     $this->tagIds = $tags;
     unset($this->attributes['tags']);
     if (!is_numeric($this->vendor)) {
         $vendor = new vendors(array('name' => $this->vendor));
         $vendor->save();
         $this->vendor = $vendor->id;
     }
     $this->cleanDates(array('purchaseDate', 'saleDate', 'soldDate'));
     return true;
 }
 /**
  * Allows the user to make changes to an existing vendor
  * 
  * If data has been posted, an attempt will be made to save the changes to the database.  The user
  * will then be redirected to the vendor view with a notification whether the changes were saved
  * or not.  If no data has been posted, the method will attempt to render a form with the editable
  * fields for the vendor indicated via ID in the query string.  If no ID is given in the query string
  * or if a vendor matching that ID cannot be found in the database, the user will be redirected to the
  * vendor index.
  */
 public function actionEdit()
 {
     if (isset($_POST['model'])) {
         $model = vendors::model()->getByPK($_POST['model']['id']);
         $model->setAttributes($_POST['model']);
         if ($model->save()) {
             testProject::setAlert('The changes were saved successfully.', 'success');
         } else {
             testProject::setAlert('We\'re sorry but your changes were not saved.  Please try again.', 'danger');
         }
         $this->redirect('vendors/view/' . $model->id);
     } else {
         if (isset($_GET['value'])) {
             $model = vendors::model()->getByPK($_GET['value']);
             $this->render('edit', array('model' => $model));
         } else {
             $this->redirect('vendors/index');
         }
     }
 }
Example #3
0
 /**
  * Allows the user to make changes to an existing product
  * 
  * If data has been posted, an attempt will be made to save the changes to the database.  The user
  * will then be redirected to the product view with a notification whether the changes were saved
  * or not.  If no data has been posted, the method will attempt to render a form with the editable
  * fields for the product indicated via ID in the query string.  If no ID is given in the query string
  * or if a product matching that ID cannot be found in the database, the user will be redirected to the
  * product index.
  */
 public function actionEdit()
 {
     if (isset($_POST['model'])) {
         //check that data has been posted
         $model = items::model()->getByPK($_POST['model']['id']);
         $model->setAttributes($_POST['model']);
         if ($model->save()) {
             //set the alert based on success of the save
             testProject::setAlert('The changes were saved successfully.', 'success');
             $this->redirect("items/view/{$model->id}");
         } else {
             testProject::setAlert('We\'re sorry but your changes were not saved.  Please try again.', 'danger');
             $this->redirect("items/view/{$model->id}");
         }
         //redirect to product view
         $this->redirect('items/view/' . $model->id);
     } else {
         //if no data is posted
         if (isset($_GET['value'])) {
             //attempt to load record based on ID and render edit form
             $model = items::model()->getByPK($_GET['value']);
             $statuses = statuses::model()->getAll();
             $vendors = vendors::model()->getAll();
             $tags = tags::model()->getAll();
             $this->render('edit', array('model' => $model, 'statuses' => $statuses, 'vendors' => $vendors, 'tags' => $tags));
         } else {
             //if no record found, redirect to product index
             $this->redirect('items');
         }
     }
 }