/** * Show the form to edit or create a category * @param int edit */ function m_categories_form($edit = 0) { global $mc, $xoopsModule, $db; MCHFunctions::toolbar(); RMTemplate::get()->assign('xoops_pagetitle', $edit ? __('Edit Category', 'match') : __('Add Category', 'match')); xoops_cp_location('<a href="./">' . $xoopsModule->name() . "</a> » \n <a href='categories.php'>" . __('Categories', 'match') . '</a> » ' . ($edit ? __('Edit Category', 'match') : __('Add Category', 'match'))); xoops_cp_header(); $id = rmc_server_var($_REQUEST, 'id', 0); if ($edit) { //Verificamos si la categoría es válida if ($id <= 0) { redirectMsg('./categories.php', __('Provide a category ID!', 'match'), 1); die; } //Verificamos si la categoría existe $cat = new MCHCategory($id); if ($cat->isNew()) { redirectMsg('./categories.php', __('Specified category was not found!', 'match'), 1); die; } } $form = new RMForm($edit ? __('Edit Category', 'match') : __('Add Category', 'match'), 'frmNew', 'categories.php'); $form->addElement(new RMFormText(__('Name', 'match'), 'name', 50, 150, $edit ? $cat->getVar('name') : ''), true); if ($edit) { $form->addElement(new RMFormText(__('Short name', 'match'), 'nameid', 50, 150, $cat->getVar('nameid')), true); } $form->addElement(new RMFormEditor(__('Description', 'match'), 'desc', '100%', '250px', $edit ? $cat->getVar('description', 'e') : '')); $sel_cats = new RMFormSelect(__('Parent category:', 'match'), 'parent', 0, $edit ? $cat->getVar('parent') : 0); $categories = array(); MCHFunctions::categories_tree($categories, 0, 0, true, $edit ? $cat->id() : 0, '`name` ASC'); $sel_cats->addOption(0, __('Select category...', 'match'), $edit ? $cat->getVar('parent') == 0 ? 1 : 0 : 1); foreach ($categories as $catego) { $sel_cats->addOption($catego['id'], str_repeat('—', $catego['indent']) . ' ' . $catego['name']); } $form->addElement($sel_cats); $form->addElement(new RMFormYesNo(__('Enable category', 'match'), 'active', $edit ? $cat->getVar('active') : 1)); $form->addElement(new RMFormHidden('action', $edit ? 'saveedit' : 'save')); if ($edit) { $form->addElement(new RMFormHidden('id', $cat->id())); } $ele = new RMFormButtonGroup(); $ele->addButton('sbt', $edit ? __('Save Changes!', 'match') : __('Add Now!', 'match'), 'submit'); $ele->addButton('cancel', _CANCEL, 'button', 'onclick="window.location=\'categos.php\';"'); $form->addElement($ele); $form = RMEvents::get()->run_event('match.form.categories', $form); $form->display(); xoops_cp_footer(); }
/** * Get categories tree * @param int Id of the category where search must start * @return array */ public function categories_tree(&$categories, $parent = 0, $indent = 0, $include_subs = true, $exclude = 0, $order = "id_cat DESC") { $db = XoopsDatabaseFactory::getDatabaseConnection(); $sql = "SELECT * FROM " . $db->prefix("mch_categories") . " WHERE parent='{$parent}' ORDER BY {$order}"; $result = $db->query($sql); while ($row = $db->fetchArray($result)) { if ($row['id_cat'] == $exclude) { continue; } $cat = new MCHCategory(); $cat->assignVars($row); $row['indent'] = $indent; $row['link'] = $cat->permalink(); $row['description'] = $cat->getVar('description'); $row['id'] = $cat->id(); $categories[$row['id_cat']] = $row; if ($include_subs) { MCHFunctions::categories_tree($categories, $row['id_cat'], $indent + 1, $include_subs, $exclude); } } }