function UpdateChildPaths($parent) { global $DB; $result = $DB->Query('SELECT * FROM lx_categories WHERE parent_id=?', array($parent['category_id'])); while ($subcategory = $DB->NextRow($result)) { $path = GeneratePathData($subcategory, $parent); $DB->Update('UPDATE lx_categories SET ' . 'path=?, ' . 'path_parts=?, ' . 'path_hash=? ' . 'WHERE category_id=?', array($path['path'], $path['serialized'], $path['hash'], $subcategory['category_id'])); $subcategory['path'] = $path['path']; $subcategory['path_parts'] = $path['serialized']; $subcategory['hash'] = $path['hash']; ClearCategoryCache($subcategory['category_id'], $path['path']); UpdateChildPaths($subcategory); } $DB->Free($result); }
function lxEditCategory() { global $DB, $C; VerifyPrivileges(P_CATEGORY_MODIFY); // Count number of subcategories and links in this category $subcategories = $DB->Count('SELECT COUNT(*) FROM lx_categories WHERE parent_id=?', array($_REQUEST['category_id'])); $links = $DB->Count('SELECT COUNT(*) FROM lx_link_cats WHERE category_id=?', array($_REQUEST['category_id'])); $validator = new Validator(); $validator->Register($_REQUEST['name'], V_EMPTY, 'The category name must be filled in'); $validator->Register($_REQUEST['name'], V_REGEX, 'The category name cannot contain a / or _ character', '/^[^\\/_]*$/'); $validator->Register($_REQUEST['name'], V_NOT_REGEX, 'The category name cannot contain a :: character sequence', '/::/'); $validator->Register($_REQUEST['parent_id'], V_EMPTY, 'You must select a parent category'); $validator->Register(is_numeric($_REQUEST['name']), V_FALSE, 'The category name cannot be all numeric'); if ($_REQUEST['template']) { $validator->Register($_REQUEST['template'], V_REGEX, 'The template name can only contain letters, numbers, periods and underscores', '/^[a-z0-9\\.\\-_]+$/'); $validator->Register($_REQUEST['template'], V_REGEX, 'The template must have a .tpl file extension', '/\\.tpl$/'); } if ($_REQUEST['parent_id'] == $_REQUEST['category_id']) { $validator->SetError('A category cannot be it\'s own parent'); } if ($_REQUEST['crosslink_id'] == $_REQUEST['category_id']) { $validator->SetError('A crosslink category cannot point to itself'); } if (!empty($_REQUEST['crosslink_id']) && ($subcategories > 0 || $links > 0)) { $validator->SetError('This category cannot be set to be a crosslink category because it contains sub-categories and/or links'); } if (!$validator->Validate()) { $GLOBALS['errstr'] = join('<br />', $validator->GetErrors()); lxShEditCategory(); return; } $parent = $_REQUEST['parent_id'] == 0 ? $GLOBALS['ROOT_CATEGORY'] : $DB->Row('SELECT * FROM lx_categories WHERE category_id=?', array($_REQUEST['parent_id'])); $_REQUEST['name'] = trim($_REQUEST['name']); $_REQUEST['url_name'] = trim($_REQUEST['url_name']); if (empty($_REQUEST['crosslink_id'])) { $_REQUEST['crosslink_id'] = null; } $old_category = $DB->Row('SELECT * FROM lx_categories WHERE category_id=?', array($_REQUEST['category_id'])); $DB->Update('UPDATE lx_categories SET ' . 'name=?, ' . 'url_name=?, ' . 'description=?, ' . 'meta_description=?, ' . 'meta_keywords=?, ' . 'parent_id=?, ' . 'path=?, ' . 'path_parts=?, ' . 'path_hash=?, ' . 'template=?, ' . 'crosslink_id=?, ' . 'related_ids=?, ' . 'status=?, ' . 'hidden=? ' . 'WHERE category_id=?', array($_REQUEST['name'], $_REQUEST['url_name'], $_REQUEST['description'], $_REQUEST['meta_description'], $_REQUEST['meta_keywords'], $_REQUEST['parent_id'], '', '', '', $_REQUEST['template'], $_REQUEST['crosslink_id'], $_REQUEST['related_ids'], $_REQUEST['status'], intval($_REQUEST['hidden']), $_REQUEST['category_id'])); $category = $DB->Row('SELECT * FROM lx_categories WHERE category_id=?', array($_REQUEST['category_id'])); $path = GeneratePathData($category, $parent); $DB->Update('UPDATE lx_categories SET ' . 'path=?, ' . 'path_parts=?, ' . 'path_hash=? ' . 'WHERE category_id=?', array($path['path'], $path['serialized'], $path['hash'], $_REQUEST['category_id'])); UpdateSubcategoryCount($parent['category_id']); // Update path data for child categories if necessary if ($old_category['parent_id'] != $category['parent_id'] || $old_category['name'] != $category['name'] || $old_category['url_name'] != $category['url_name']) { $category['path'] = $path['path']; $category['path_parts'] = $path['serialized']; $category['hash'] = $path['hash']; UpdateChildPaths($category); } $GLOBALS['message'] = 'Category successfully updated'; $GLOBALS['added'] = true; // Ask user to create custom template file if it does not exist if ($_REQUEST['template'] && !file_exists("{$GLOBALS['BASE_DIR']}/templates/{$_REQUEST['template']}")) { $GLOBALS['message'] .= "<br />Please create the template file {$_REQUEST['template']} in the templates directory and set it's permissions to 666"; } // Clear cache for this category and it's parent ClearCategoryCache($category['category_id'], $path['path']); ClearCategoryCache($category['parent_id']); lxShEditCategory(); }