<?php
/*
* Copyright 2008 Blandware (http://www.blandware.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Actions for categories.
*
* @package AtleapLite
* @author Roman Puchkovskiy
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
*/
require_once 'commodity.php';
// ~ DAO functions
/**
* Loads categories from DB.
*
* @param array $queryInfo optional query info
* @see loadObjects()
* @return array objects
*/
function loadCategories($queryInfo = array()) {
$params = $queryInfo['parameters'];
if (!empty($params['parentId'])) {
$queryInfo['fields']['parent_id'] = $params['parentId'] == 'null' ? null : $params['parentId'];
}
return loadObjects('category', getCategoryDescriptor(), 'categoriesGrid', $queryInfo);
}
/**
* Creates or updates a category.
*
* @param int $id ID of category to update (ignored when creating)
* @param int|null $parentId ID of parent category (null if no parent)
* @param object $form form object
* @param bool $create if true, category is created, else it's updated
* @return bool|object false if failed or object
*/
function addOrUpdateCategory($id, $parentId, &$form, $create)
{
if ($create) {
$dao =& getDao('category');
} else {
$dao =& staticGet('category', $id);
if (!$dao) {
// no such object
return false;
}
}
formToDao($form, $dao, getCategoryDescriptor());
if ($parentId === null || $parentId === '') {
$dao->parent_id = 'null';
} else {
$dao->parent_id = $parentId;
}
if ($create) {
$dao->pos = getFreeCategoryPos($parentId);
$dao->insert();
} else {
$dao->update();
}
return $dao;
}
/**
* Returns position number which is not assigned to any category with given
* parent.
*
* @param int|null $parentId ID of parent (if null, categories with no parent
* are considered)
*/
function getFreeCategoryPos($parentId) {
$dao =& getDao('category');
return $dao->getFreePos($parentId);
}
// ~ Actions
/**
* Shows a page with list of categories.
*/
function listCategories()
{
global $smarty, $perPage, $pagerDelta, $bottomLinks;
$parentId = fromGetOrSession('parentId');
processGridParams('categoriesGrid');
$queryInfo = buildDefaultQueryInfo();
$queryInfo['orderBy'] = 'pos';
$queryInfo['parameters']['parentId'] = empty($parentId) ? 'null' : $parentId;
$partial = loadCategories($queryInfo);
$total = $partial['total'];
$categories = $partial['rows'];
$pager = getPager(array('totalItems' => $total));
exportPagerData($pager, $smarty, $categories);
assignSortDirs('categoriesGrid', $smarty, getCategoryDescriptor());
assignFilters($smarty, 'categoriesGrid');
$smarty->assign('template', 'category/list.tpl');
$smarty->assign('title', getMessage('category.list.title'));
$smarty->assign('allowedViewCategory', allowed('viewCategory'));
$smarty->assign('allowedUpdateCategory', allowed('updateCategory'));
$smarty->assign('allowedDeleteCategory', allowed('deleteCategory'));
$smarty->assign('deletePermId', 'deleteCategory');
$smarty->assign('offset', $queryInfo['offset']);
$smarty->assign('isLastPage', $pager->isLastPage());
assignCategoryAncestors($smarty, $parentId);
$smarty->initGrid('categoriesGrid', getCategoryDescriptor(), $_GET['action']);
setSystemMenuItemBold('manageCatalogue');
$bottomLinks[] = array('link' => buildUrl('callCreateCategory'),
'text' => getMessage('common.button.create'),
'permId' => 'createCategory',
'button' => true);
if ($parentId !== null && $parentId !== '') {
$bottomLinks[] = array('link' => buildUrl('viewCategory', array('categoryId' => $parentId)),
'text' => getMessage('category.form.commodities'),
'permId' => 'viewCategory',
'button' => true);
}
}
/**
* Allows to view category properties.
*/
function viewCategory()
{
global $smarty, $bottomLinks;
setSystemMenuItemBold('manageCatalogue');
$bottomLinks[] = array('link' => getCategoryListUrl('pos'),
'text' => getMessage('category.form.backToList'),
'permId' => 'listCategories',
'button' => true,
'class' => 'form_button_long');
$categoryId = fromGetOrSession('categoryId');
$dao =& staticGet('category', $categoryId);
$smarty->assign('viewed', $dao);
$categoryId = fromGetOrSession('categoryId');
if (!isset($categoryId)) {
redirect(getCategoryListUrl('pos'));
}
processGridParams('commoditiesGrid');
$queryInfo = buildDefaultQueryInfo();
$queryInfo['orderBy'] = 'pos';
$queryInfo['parameters']['categoryId'] = $categoryId;
$partial = loadCommodities($queryInfo);
$total = $partial['total'];
$commodities = $partial['rows'];
$pager = getPager(array('totalItems' => $total));
exportPagerData($pager, $smarty, $commodities);
assignSortDirs('commoditiesGrid', $smarty, getCommodityDescriptor());
assignFilters($smarty, 'commoditiesGrid');
$smarty->assign('allowedListCommodities', allowed('listCommodities'));
$smarty->assign('allowedCreateCommodity', allowed('createCommodity'));
$smarty->assign('allowedViewCommodity', allowed('viewCommodity'));
$smarty->assign('allowedUpdateCommodity', allowed('updateCommodity'));
$smarty->assign('allowedDeleteCommodity', allowed('deleteCommodity'));
$smarty->assign('deletePermId', 'deleteCommodity');
$smarty->assign('offset', $queryInfo['offset']);
$smarty->assign('isLastPage', $pager->isLastPage());
$smarty->initGrid('commoditiesGrid', getCommodityDescriptor(), $_GET['action']);
$smarty->assign('template', 'category/view.tpl');
$smarty->assign('title', getMessage('category.view.title'));
}
/**
* Displays a category to user.
*/
function showCategory() {
global $smarty, $bottomLinks, $maxImageWidth, $maxImageHeight;
if (isset($_GET['id'])) {
$id = $_GET['id'];
$category =& staticGet('category', $id);
} else {
$id = null;
$category = null;
}
if ($category != null) {
$subCategories = $category->getSubCategories();
$commodities = $category->getCommodities();
} else {
$dao =& getDao('category');
$subCategories = $dao->getFreeCategories();
}
$smarty->assign('category', $category);
$smarty->assign('subCategories', $subCategories);
$smarty->assign('subCategoriesAreEmpty', count($subCategories) == 0);
$smarty->assign('commodities', $commodities);
$smarty->assign('commoditiesAreEmpty', count($commodities) == 0);
assignCategoryAncestors($smarty, $id);
$smarty->assign('maxImageWidth', $maxImageWidth);
$smarty->assign('maxImageHeight', $maxImageHeight);
$smarty->assign('template', 'category/show.tpl');
$smarty->assign('title', $category == null
? getMessage('category.show.title')
: htmlspecialchars($category->title));
$smarty->setAdminLayout(false);
}
/**
* Creates a category form.
*
* @param string $name form name
* @param string $method HTTP method
* @param string $action action
* @param bool $create whether this is for for creation, not for updating
* @param bool $init whether form needs to be initialized from dao
* @param int $id optional category ID (for updating)
* @return object created form
*/
function &createCategoryForm($name, $method, $action, $create, $init,
$id = null) {
$params = array();
if (!$create) {
$params['id'] = $id;
}
$parentId = fromGetOrSession('parentId');
$params['parentId'] = $parentId;
$form = new FormBase($name, $method, buildUrl($action, $params));
$form->addTextElement('title', getMessage('category.form.title'));
$form->addRicheditElement('description', getMessage('category.form.description'));
$form->addProceedElement($create ? getMessage('common.button.create') : getMessage('common.button.update'));
$form->addCancelElement(getMessage('common.button.cancel'));
if (!$create && $init) {
$dao =& staticGet('category', $id);
daoToForm($dao, $form, getCategoryDescriptor());
}
$form->addRequiredRule('title', getMessage('category.error.title.required'));
$form->addFormRule($create ? 'validateCreateCategoryForm' : 'validateUpdateCategoryForm');
return $form;
}
/**
* Shows a page with form to create a category.
*/
function callCreateCategory()
{
global $smarty, $bottomLinks;
setSystemMenuItemBold('manageCatalogue');
$form =& createCategoryForm('createCategory', 'POST', 'createCategory', true, false);
showForm($smarty, $form, 'category/createUpdate.tpl', getMessage('category.create.title'));
}
/**
* Creates a category.
*/
function createCategory()
{
global $smarty, $bottomLinks;
if (isCancelled()) {
redirect(getCategoryListUrl('pos'));
}
$parentId = fromGetOrSession('parentId');
setSystemMenuItemBold('manageCatalogue');
$form =& createCategoryForm('createCategory', 'POST', 'createCategory', true, false);
if ($form->validate()) {
addOrUpdateCategory(null, $parentId, $form, true);
redirect(getCategoryListUrl());
} else {
showForm($smarty, $form, 'category/createUpdate.tpl', getMessage('category.create.title'));
}
}
/**
* Shows a page with form to update a category.
*/
function callUpdateCategory()
{
global $smarty, $bottomLinks;
setSystemMenuItemBold('manageCatalogue');
$form =& createCategoryForm('updateCategory', 'POST', 'updateCategory', false, true, $_GET['id']);
showForm($smarty, $form, 'category/createUpdate.tpl', getMessage('category.update.title'));
}
/**
* Updates a category.
*/
function updateCategory()
{
global $smarty, $bottomLinks;
if (isCancelled()) {
redirect(getCategoryListUrl('pos'));
}
$parentId = fromGetOrSession('parentId');
setSystemMenuItemBold('manageCategories');
$form =& createCategoryForm('updateCategory', 'POST', 'updateCategory', false, false, $_GET['id']);
if ($form->validate()) {
addOrUpdateCategory($_GET['id'], $parentId, $form, false);
redirect(getCategoryListUrl());
} else {
showForm($smarty, $form, 'category/createUpdate.tpl', getMessage('category.update.title'));
}
}
/**
* Validates a form which is used to create a category.
*
* @param array $fields assoc array from field names to values
* @return bool|array true if form is valid or assoc array with errors
*/
function validateCreateCategoryForm($fields) {
return validateCategoryForm($fields, true);
}
/**
* Validates a form which is used to update a category.
*
* @param array $fields assoc array from field names to values
* @return bool|array true if form is valid or assoc array with errors
*/
function validateUpdateCategoryForm($fields) {
return validateCategoryForm($fields, false);
}
/**
* Validates a category form.
*
* @param array $fields assoc array from field names to values
* @param bool $create whether this is creation form
* @return bool|array true if form is valid or assoc array with errors
*/
function validateCategoryForm($fields, $create)
{
if (categoryHasDuplicates($fields, $create, $_GET['parentId'])) {
return array('title' => getMessage('category.error.duplicate.title'));
}
return true;
}
/**
* Returns whether category has duplicates.
*
* @param array $fields assoc array from field names to values
* @param bool $create whether this is creation form being validated
* @param int|null $parentId ID of parent (may be null)
* @return bool true if there are duplicates
*/
function categoryHasDuplicates($fields, $create, $parentId) {
$id = $_GET['id'];
$title = $fields['title'];
$dao =& getDao('category');
$escapedTitle = $dao->escape($title);
$dao->selectAdd();
$dao->selectAdd('count(*) as _c');
$dao->whereAdd();
$dao->whereAdd("(title = '$escapedTitle')");
$dao->addParentCondition($parentId);
if (!$create) {
$dao->whereAdd("(id != $id)");
}
$dao->find(true);
return $dao->_c > 0;
}
/**
* Moves a category up one position.
*/
function liftCategory() {
$id = $_GET['id'];
$dao =& staticGet('category', $id);
if ($dao) {
$dao->tryLift();
}
redirect(getCategoryListUrl());
}
/**
* Moves a category down one position.
*/
function lowerCategory() {
$id = $_GET['id'];
$dao =& staticGet('category', $id);
if ($dao) {
$dao->tryLower();
}
redirect(getCategoryListUrl());
}
/**
* Deletes a category.
*/
function deleteCategory()
{
global $smarty, $bottomLinks;
setSystemMenuItemBold('manageCatalogue');
$dao =& staticGet('category', $_GET['id']);
if (tryDeleteCategory($dao)) {
redirect(getCategoryListUrl());
} else {
$smarty->assign('template', 'error.tpl');
$smarty->assign('reason', getMessage('category.error.cannotDelete'));
$smarty->assign('link', getCategoryListUrl());
}
}
/**
* Deletes several categories.
*/
function massDeleteCategories()
{
if (isset($_GET['checked'])) {
foreach ($_GET['checked'] as $id => $on) {
$dao =& staticGet('category', $id);
tryDeleteCategory($dao);
}
}
redirect(getCategoryListUrl());
}
/**
* Tryies to delete a category.
*
* @param object $dao object to delete
* @return true if deletion was successful
*/
function tryDeleteCategory($dao) {
$result = $dao->deleteSubObjects();
if ($result) {
$result = $dao->delete();
}
return $result;
}
/**
* Returns URL to list of categories.
*
* @param string $colon optional colon by which to sort
* @return URL
*/
function getCategoryListUrl($colon = null) {
if (empty($colon)) {
$colon = $_SESSION['sortColon'];
}
return buildUrl('listCategories'/*, array('sortColon' => $colon)*/);
}
/**
* Obtains category ancestors and assigns them to asarty instance.
*
* @param object $smarty smarty instance
* @param int $id category ID
*/
function assignCategoryAncestors(&$smarty, $id) {
if ($id === null || $id === '') {
$ancestors = array();
} else {
$dao =& staticGet('category', $id);
$ancestors =& $dao->getAncestorsAndThis();
}
$smarty->assign('ancestors', $ancestors);
$smarty->assign('ancestorsNumber', count($ancestors));
}
?>