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(); }
function DeleteCategory($category_id, $category = null) { global $DB, $ROOT_CATEGORY; if ($category == null) { $category = $DB->Row('SELECT * FROM lx_categories WHERE category_id=?', array($category_id)); } // Remove cache files ClearCategoryCache($category['category_id'], $category['path']); ClearCategoryCache($category['parent_id']); // Delete all sub-categories $result = $DB->Query('SELECT * FROM lx_categories WHERE parent_id=?', array($category_id)); while ($subcategory = $DB->NextRow($result)) { DeleteCategory($subcategory['category_id'], $subcategory); } $DB->Free($result); // Delete all links in this category $result = $DB->Query('SELECT * FROM lx_links JOIN lx_link_cats USING (link_id) WHERE category_id=?', array($category_id)); while ($link = $DB->NextRow($result)) { DeleteLink($link['link_id'], FALSE, $link); } $DB->Free($result); // Delete this category $DB->Update('DELETE FROM lx_categories WHERE category_id=?', array($category_id)); // Update subcategory count for parent category if ($category['parent_id'] != $ROOT_CATEGORY['parent_id']) { UpdateSubcategoryCount($category['parent_id']); } }
// 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. define('LINKX', TRUE); require_once '../includes/common.php'; require_once "{$GLOBALS['BASE_DIR']}/includes/mysql.class.php"; require_once "{$GLOBALS['BASE_DIR']}/admin/includes/functions.php"; $DB = new DB($C['db_hostname'], $C['db_username'], $C['db_password'], $C['db_name']); $DB->Connect(); echo "Re-counting sub-categories and links per category..."; flush(); $result = $DB->Query('SELECT * FROM lx_categories'); while ($category = $DB->NextRow($result)) { UpdateSubcategoryCount($category['category_id']); UpdateLinkCount($category['category_id']); } $DB->Free($result); echo "done<br />"; flush(); echo "Re-counting comments..."; flush(); $result = $DB->Query('SELECT * FROM lx_links'); while ($link = $DB->NextRow($result)) { $comments = $DB->Count('SELECT COUNT(*) FROM lx_link_comments WHERE link_id=?', array($link['link_id'])); $DB->Update('UPDATE lx_links SET comments=? WHERE link_id=?', array($comments, $link['link_id'])); } $DB->Free($result); echo "done<br />"; flush();