public function validateIsCategory($attribute, $message = 'attribute is not a valid category') { $category = categories::model()->getByPK($this->attributes[$attribute]); if (!$category) { return str_replace('attribute', $attribute, $message); } else { return false; } }
/** * Prepares a list of products and renders accordingly * * If the request comes from an AJAX request containing pagination data, the resultant table will be * paginated appropriately. Otherwise, a default pagination state (no sorting, no filtering, page 1) * will be created. */ public function actionIndex() { $statuses = statuses::model()->getAll(); $vendors = vendors::model()->getAll(); $tags = tags::model()->getAll(); $showColumns = Auth::User()->getColumns(); if (count($showColumns) == 0) { $showColumns = array(1, 2, 3); } $columnHeaders = headers::model()->getAll(); $allHeaders = array(); foreach ($columnHeaders as $header) { $allHeaders[$header->id] = $header; } $headers = array('show' => $showColumns, 'headers' => $allHeaders); $pagination = array('limit' => array(0, 10), 'filter' => 'All categories', 'sortAttribute' => null, 'sortDirection' => null); if (!empty($_GET['ajax'])) { //handle ajax requests //get the pagination data from the query string if (isset($_GET['headers'])) { $headers['show'] = explode(',', $_GET['headers']); Auth::User()->setColumns($_GET['headers']); } $sortHeader = headers::model()->getbyPK($_GET['sortAttribute']); $pagination['sortAttribute'] = $sortHeader->sortName; $pagination['sortDirection'] = $_GET['sortDirection']; $pagination['limit'] = array($_GET['paginationPageNumber'], $_GET['paginationPerPage']); $pagination['filter'] = $_GET['filter']; $conditions = null; $params = null; //if there is a filter in place, create the conditions and parameters needed if (!empty($pagination['filter']) && $pagination['filter'] != 'All categories') { $conditions = "category = :category"; $category = categories::model()->getByAttribute('name', $pagination['filter']); $params = array('category' => $category->id); } $models = items::model(true)->getAll($conditions, $params, $pagination); $count = items::model()->getCount($conditions, $params); $pagination['count'] = $count; $pagination['sortAttribute'] = $_GET['sortAttribute']; $this->renderPartial('table', array('data' => $models, 'statuses' => $statuses, 'pagination' => $pagination, 'vendors' => $vendors, 'tags' => $tags, 'headers' => $headers)); } else { //handle default requests $models = items::model(true)->getAll(null, null, $pagination); $count = items::model()->getCount(null); $pagination['count'] = $count; $this->render('index', array('models' => $models, 'statuses' => $statuses, 'vendors' => $vendors, 'tags' => $tags, 'pagination' => $pagination, 'headers' => $headers)); } }
public function actionUpdateCategory() { $errors = array(); if (!isset($_GET['value'])) { $errors[] = 'A category ID must be supplied'; } else { if (!is_numeric($_GET['value'])) { $errors[] = "Provided category ID must be an integer"; } else { if ($category = categories::model()->getbyPK($_GET['value'])) { $updateAttributes = array('name' => null, 'description' => null); foreach ($updateAttributes as $updateAttribute => $value) { if (isset($_GET[$updateAttribute])) { $updateAttributes[$updateAttribute] = $_GET[$updateAttribute]; } else { unset($updateAttributes[$updateAttribute]); } } $category->setAttributes($updateAttributes); if ($category->save()) { $attributes = array('id', 'name', 'created', 'modified', 'description'); $data = array($this->returnValues($category, $attributes)); } else { $errors[] = 'The category could not be updated'; } } else { $errors[] = 'Could not find category with ID of ' . $_GET['value']; } } } if (count($errors) > 0) { $this->renderApi(false, null, $errors); } else { $this->renderApi(true, $data); } }
/** * Deletes a category * * If no category ID is passed or if it does not match a valid product ID in the database, an error * message is generated. Otherwise the category is deleted and a success message is generated. In either * case the user is redirected to the categories index. */ public function actionDelete() { testProject::setAlert('There was a problem with your request. Please try again.', 'error'); if (isset($_GET['value'])) { $model = categories::model()->getByPK($_GET['value']); $name = $model->name; if ($model->delete()) { testProject::setAlert('The item ' . $name . ' was deleted.', 'info'); } } $this->redirect('categories/index'); }
/** * 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 = products::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'); } else { testProject::setAlert('We\'re sorry but your changes were not saved. Please try again.', 'danger'); } //redirect to product view $this->redirect('products/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 = products::model()->getByPK($_GET['value']); $categories = categories::model()->getAll(); $this->render('edit', array('model' => $model, 'categories' => $categories)); } else { //if no record found, redirect to product index $this->redirect('products/index'); } } }