function form_save() { if (isset($_POST["save_component_tree"])) { $save["id"] = $_POST["id"]; $save["name"] = form_input_validate($_POST["name"], "name", "", false, 3); $save["sort_type"] = form_input_validate($_POST["sort_type"], "sort_type", "", true, 3); if (!is_error_message()) { $tree_id = sql_save($save, "graph_tree"); if ($tree_id) { raise_message(1); /* sort the tree using the algorithm chosen by the user */ sort_tree(SORT_TYPE_TREE, $tree_id, $_POST["sort_type"]); } else { raise_message(2); } } if (is_error_message() || empty($_POST["id"])) { header("Location: tree.php?action=edit&id=" . (empty($tree_id) ? $_POST["id"] : $tree_id)); } else { header("Location: tree.php"); } } elseif (isset($_POST["save_component_tree_item"])) { $tree_item_id = api_tree_item_save($_POST["id"], $_POST["graph_tree_id"], $_POST["type"], $_POST["parent_item_id"], isset($_POST["title"]) ? $_POST["title"] : "", isset($_POST["local_graph_id"]) ? $_POST["local_graph_id"] : "0", isset($_POST["rra_id"]) ? $_POST["rra_id"] : "0", isset($_POST["host_id"]) ? $_POST["host_id"] : "0", isset($_POST["host_grouping_type"]) ? $_POST["host_grouping_type"] : "1", isset($_POST["sort_children_type"]) ? $_POST["sort_children_type"] : "1", isset($_POST["propagate_changes"]) ? true : false); if (is_error_message()) { header("Location: tree.php?action=item_edit&tree_item_id=" . (empty($tree_item_id) ? $_POST["id"] : $tree_item_id) . "&tree_id=" . $_POST["graph_tree_id"] . "&parent_id=" . $_POST["parent_item_id"]); } else { header("Location: tree.php?action=edit&id=" . $_POST["graph_tree_id"]); } } }
function categories_meta_box ($Product) { $db =& DB::get(); $category_table = DatabaseObject::tablename(Category::$table); $categories = $db->query("SELECT id,name,parent FROM $category_table ORDER BY parent,name",AS_ARRAY); $categories = sort_tree($categories); if (empty($categories)) $categories = array(); $categories_menu = '<option value="0">'.__('Parent Category','Ecart').'…</option>'; foreach ($categories as $category) { $padding = str_repeat(" ",$category->depth*3); $categories_menu .= '<option value="'.$category->id.'">'.$padding.esc_html($category->name).'</option>'; } $selectedCategories = array(); foreach ($Product->categories as $category) $selectedCategories[] = $category->id; ?> <div id="category-menu" class="multiple-select short"> <ul> <?php $depth = 0; foreach ($categories as $category): if ($category->depth > $depth) echo "<li><ul>"; ?> <?php if ($category->depth < $depth): ?> <?php for ($i = $category->depth; $i < $depth; $i++): ?> </ul></li> <?php endfor; ?> <?php endif; ?> <li id="category-element-<?php echo $category->id; ?>"><input type="checkbox" name="categories[]" value="<?php echo $category->id; ?>" id="category-<?php echo $category->id; ?>" tabindex="3"<?php if (in_array($category->id,$selectedCategories)) echo ' checked="checked"'; ?> class="category-toggle" /><label for="category-<?php echo $category->id; ?>"><?php echo esc_html($category->name); ?></label></li> <?php $depth = $category->depth; endforeach; ?> <?php for ($i = 0; $i < $depth; $i++): ?> </ul></li> <?php endfor; ?> </ul> </div> <div> <div id="new-category" class="hidden"> <input type="text" name="new-category" value="" size="15" id="new-category-name" /><br /> <select name="new-category-parent"><?php echo $categories_menu; ?></select> <button id="add-new-category" type="button" class="button-secondary" tabindex="2"><small><?php _e('Add','Ecart'); ?></small></button> </div> <button id="new-category-button" type="button" class="button-secondary" style="margin-top:10px;" tabindex="2"><?php _e('Add New Category','Ecart'); ?></button> </div> <?php }
$groups[$group][$host]["snmp_ver"] = $version; $groups[$group][$host]["community"] = $community; } /* ----------------------------------------------------------------------------------------------------- */ /* Make sure we have a netdot tree */ $treeId = db_fetch_cell("SELECT id FROM graph_tree WHERE name = 'Netdot'"); if ($treeId) { debug("Netdot tree already exists - id: ({$treeId})"); } else { $treeOpts = array(); $treeOpts["id"] = 0; # Zero means create a new one rather than save over an existing one $treeOpts["name"] = "Netdot"; $treeOpts["sort_type"] = $sortMethods["alpha"]; $treeId = sql_save($treeOpts, "graph_tree"); sort_tree(SORT_TYPE_TREE, $treeId, $treeOpts["sort_type"]); echo "Created Netdot Tree - id: {$treeId}\n"; } /* Store all header and host nodes for faster lookups */ $hostNodes = array(); $headerNodes = array(); $treeNodes = db_fetch_assoc("SELECT id, title, host_id FROM graph_tree_items WHERE graph_tree_id={$treeId}"); foreach ($treeNodes as $row) { if ($row["host_id"] != 0) { $hostNodes[$row["host_id"]] = $row["id"]; } elseif ($row["title"] != "") { $headerNodes[$row["title"]] = $row["id"]; } } /* ----------------------------------------------------------------------------------------------------- */ foreach ($groups as $group => $hosts) {
/** * Load sub-categories * * @since 1.0 * @version 1.1 * * @param array $loading Query configuration array * @return boolean successfully loaded or not **/ function load_children($loading=array()) { if (isset($this->smart) || empty($this->id) || empty($this->uri)) return false; $db = DB::get(); $catalog_table = DatabaseObject::tablename(Catalog::$table); $defaults = array( 'columns' => 'cat.*,count(sc.product) as total', 'joins' => array("LEFT JOIN $catalog_table AS sc ON sc.parent=cat.id AND sc.type='category'"), 'where' => array("cat.uri like '%$this->uri%' AND cat.id <> $this->id"), 'orderby' => 'name', 'order' => 'ASC' ); $loading = array_merge($defaults,$loading); extract($loading); switch(strtolower($orderby)) { case "id": $orderby = "cat.id"; break; case "slug": $orderby = "cat.slug"; break; case "count": $orderby = "total"; break; default: $orderby = "cat.name"; } switch(strtoupper($order)) { case "DESC": $order = "DESC"; break; default: $order = "ASC"; } $joins = join(' ',$joins); $where = join(' AND ',$where); $name_order = ($orderby !== "name")?",name ASC":""; $query = "SELECT $columns FROM $this->_table AS cat $joins WHERE $where GROUP BY cat.id ORDER BY cat.parent DESC,$orderby $order$name_order"; $children = $db->query($query,AS_ARRAY); $children = sort_tree($children,$this->id); foreach ($children as &$child) { $this->children[$child->id] = new Category(); $this->children[$child->id]->populate($child); $this->children[$child->id]->depth = $child->depth; $this->children[$child->id]->total = $child->total; } return (!empty($this->children)); }
function sort_tree(&$result, $key) { $master_sort = sorter($result, $key); foreach ($master_sort as $data) { $id = $data[$key]; // remove child $newdata = $data; unset($data['children']); $current_array[$id] = $data; // fielded parents if (array_key_exists("children", $newdata)) { $result = $newdata['children']; $current_array[$id]['children'] = sort_tree($result, $key); } } return $current_array; }
/** * Interface processor for the product list manager * * @return void **/ function products ($workflow=false) { global $Ecart,$Products; $db = DB::get(); $Settings = &EcartSettings(); if ( !(is_ecart_userlevel() || current_user_can('ecart_products')) ) wp_die(__('You do not have sufficient permissions to access this page.')); $defaults = array( 'cat' => false, 'pagenum' => 1, 'per_page' => 20, 's' => '', 'sl' => '', 'matchcol' => '', 'f' => '' ); $args = array_merge($defaults,$_GET); extract($args,EXTR_SKIP); if (!$workflow) { if (empty($categories)) $categories = array(''); $category_table = DatabaseObject::tablename(Category::$table); $query = "SELECT id,name,parent FROM $category_table ORDER BY parent,name"; $categories = $db->query($query,AS_ARRAY); $categories = sort_tree($categories); if (empty($categories)) $categories = array(); $categories_menu = '<option value="">'.__('View all categories','Ecart').'</option>'; $categories_menu .= '<option value="-"'.($cat=='-'?' selected="selected"':'').'>'.__('Uncategorized','Ecart').'</option>'; foreach ($categories as $category) { $padding = str_repeat(" ",$category->depth*3); if ($cat == $category->id) $categories_menu .= '<option value="'.$category->id.'" selected="selected">'.$padding.esc_html($category->name).'</option>'; else $categories_menu .= '<option value="'.$category->id.'">'.$padding.esc_html($category->name).'</option>'; } $inventory_filters = array( 'all' => __('View all products','Ecart'), 'is' => __('In stock','Ecart'), 'ls' => __('Low stock','Ecart'), 'oos' => __('Out-of-stock','Ecart'), 'ns' => __('Not stocked','Ecart') ); $inventory_menu = menuoptions($inventory_filters,$sl,true); } $subfilters = array('f' => 'featured','p' => 'published','s' => 'onsale','i' => 'inventory'); $subs = array( 'all' => array('label' => __('All','Ecart'),'columns' => "count(distinct pd.id) AS total",'where'=>'true'), 'published' => array('label' => __('Published','Ecart'),'total' => 0,'columns' => "count(distinct pd.id) AS total",'where'=>"pd.status='publish'",'request' => 'p'), 'onsale' => array('label' => __('On Sale','Ecart'),'total' => 0,'columns' => "count(distinct pd.id) AS total",'where'=>"pt.sale='on'",'request' => 's'), 'featured' => array('label' => __('Featured','Ecart'),'total' => 0,'columns' => "count(distinct pd.id) AS total",'where'=>"pd.featured='on'",'request' => 'f'), 'inventory' => array('label' => __('Inventory','Ecart'),'total' => 0,'columns' => "count(distinct pt.id) AS total",'where'=>"pt.inventory='on' AND pt.type!='N/A'",'grouping'=>'pt.id','request' => 'i') ); if ('i' == $f) $per_page = 50; $pagenum = absint( $pagenum ); if ( empty($pagenum) ) $pagenum = 1; if( !$per_page || $per_page < 0 ) $per_page = 20; $start = ($per_page * ($pagenum-1)); $pd = DatabaseObject::tablename(Product::$table); $pt = DatabaseObject::tablename(Price::$table); $catt = DatabaseObject::tablename(Category::$table); $clog = DatabaseObject::tablename(Catalog::$table); $orderby = "pd.created DESC"; $where = "true"; $having = ""; if (!empty($s)) { $products = new SearchResults(array("search"=>$s)); $products->load_products(array("load"=>array())); $ids = array_keys($products->products); $where .= " AND pd.id IN (".join(',',$ids).")"; } // if (!empty($cat)) $where .= " AND cat.id='$cat' AND (clog.category != 0 OR clog.id IS NULL)"; if (!empty($cat)) { if ($cat == "-") { $having = "HAVING COUNT(cat.id) = 0"; } else { $matchcol .= ", GROUP_CONCAT(DISTINCT cat.id ORDER BY cat.id SEPARATOR ',') AS catids"; $where .= " AND cat.id IN (SELECT parent FROM $clog WHERE parent=$cat AND type='category')"; } } if (!empty($sl)) { switch($sl) { case "ns": $where .= " AND pt.inventory='off'"; break; case "oos": $where .= " AND (pt.inventory='on')"; $having .= (empty($having)?"HAVING ":" AND ")."SUM(pt.stock) = 0"; break; case "ls": $ls = $Settings->get('lowstock_level'); if (empty($ls)) $ls = '0'; $where .= " AND (pt.inventory='on' AND pt.stock <= $ls AND pt.stock > 0)"; break; case "is": $where .= " AND (pt.inventory='on' AND pt.stock > 0)"; } } if (!empty($f)) $where .= " AND ".$subs[$subfilters[$f]]['where']; $base = $Settings->get('base_operations'); if ($base['vat']) $taxrate = ecart_taxrate(); if (empty($taxrate)) $taxrate = 0; if ('i' == $f) { $columns = "SQL_CALC_FOUND_ROWS pt.id,pd.id as product,pd.name,pd.slug,pt.label,pt.optionkey,pt.stock,pt.sku"; // Load the products $query = "SELECT $columns $matchcol FROM $pt AS pt LEFT JOIN $pd AS pd ON pd.id=pt.product LEFT JOIN $clog AS clog ON pd.id=clog.product LEFT JOIN $catt AS cat ON cat.id=clog.parent AND clog.type='category' WHERE $where GROUP BY pt.id $having ORDER BY pd.id,pt.sortorder LIMIT $start,$per_page"; $Products = $db->query($query,AS_ARRAY); $productcount = $db->query("SELECT FOUND_ROWS() as total"); } else { $columns = "SQL_CALC_FOUND_ROWS pd.id,pd.name,pd.slug,pd.featured,pd.variations,GROUP_CONCAT(DISTINCT cat.name ORDER BY cat.name SEPARATOR ', ') AS categories, IF(pt.options=0,IF(pt.tax='off',pt.price,pt.price+(pt.price*$taxrate)),-1) AS mainprice, IF(MAX(pt.tax)='off',MAX(pt.price),MAX(pt.price+(pt.price*$taxrate))) AS maxprice, IF(MAX(pt.tax)='off',MIN(pt.price),MIN(pt.price+(pt.price*$taxrate))) AS minprice, IF(pt.inventory='on','on','off') AS inventory, ROUND(SUM(pt.stock)/IF(clog.id IS NULL,1,count(DISTINCT clog.id)),0) AS stock"; if ($workflow) $columns = "pd.id"; // Load the products $query = "SELECT $columns $matchcol FROM $pd AS pd LEFT JOIN $pt AS pt ON pd.id=pt.product AND pt.type != 'N/A' AND pt.context != 'addon' LEFT JOIN $clog AS clog ON pd.id=clog.product LEFT JOIN $catt AS cat ON cat.id=clog.parent AND clog.type='category' WHERE $where GROUP BY pd.id $having ORDER BY $orderby LIMIT $start,$per_page"; $Products = $db->query($query,AS_ARRAY); $productcount = $db->query("SELECT FOUND_ROWS() as total"); } foreach ($subs as $name => &$subquery) { if ($name == "all") { $subquery['total'] = (int)$productcount->total; continue; } $columns = $subquery['columns']; if (!empty($f)) $where = str_replace(" AND ".$subs[$subfilters[$f]]['where'],"",$where); $w = ($where == "true")?$subquery['where']:"$where AND ({$subquery['where']})"; $category_join = (strpos($w,"type='category'") !== false)?"LEFT JOIN $clog AS clog ON pd.id=clog.product LEFT JOIN $catt AS cat ON cat.id=clog.parent AND clog.type='category'":""; $grouping = "GROUP BY ".(isset($subquery['grouping'])?$subquery['grouping']:"pd.id"); $query = "SELECT $columns $matchcol FROM $pd AS pd LEFT JOIN $pt AS pt ON pd.id=pt.product AND pt.type != 'N/A' $category_join WHERE $w $grouping $having"; $db->query($query); $found = $db->query("SELECT FOUND_ROWS() as total"); if (isset($found->total)) $subquery['total'] = number_format((int)$found->total); } $num_pages = ceil($productcount->total / $per_page); $page_links = paginate_links( array( 'base' => add_query_arg(array("edit"=>null,'pagenum' => '%#%')), 'format' => '', 'total' => $num_pages, 'current' => $pagenum, )); if ('i' == $f) { include(ECART_ADMIN_PATH."/products/inventory.php"); return; } if ($workflow) return $Products; include(ECART_ADMIN_PATH."/products/products.php"); }
function form_save() { /* clear graph tree cache on save - affects current user only, other users should see changes in <5 minutes */ if (isset($_SESSION['dhtml_tree'])) { unset($_SESSION['dhtml_tree']); } if (isset($_POST["save_component_tree"])) { /* modify for multi user start */ if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) { if ($_POST["id"] != $_SESSION["private_tree_id"]) { access_denied(); } } /* modify for multi user end */ $save["id"] = $_POST["id"]; $save["name"] = form_input_validate($_POST["name"], "name", "", false, 3); $save["sort_type"] = form_input_validate($_POST["sort_type"], "sort_type", "", true, 3); if (!is_error_message()) { $tree_id = sql_save($save, "graph_tree"); if ($tree_id) { raise_message(1); /* sort the tree using the algorithm chosen by the user */ sort_tree(SORT_TYPE_TREE, $tree_id, $_POST["sort_type"]); } else { raise_message(2); } } header("Location: tree.php?action=edit&id=" . (empty($tree_id) ? $_POST["id"] : $tree_id)); } elseif (isset($_POST["save_component_tree_item"])) { /* modify for multi user start */ if (!empty($_GET["id"])) { if (!check_tree_item($_POST["id"])) { access_denied(); } } /* modify for multi user end */ $tree_item_id = api_tree_item_save($_POST["id"], $_POST["graph_tree_id"], $_POST["type"], $_POST["parent_item_id"], isset($_POST["title"]) ? $_POST["title"] : "", isset($_POST["local_graph_id"]) ? $_POST["local_graph_id"] : "0", isset($_POST["rra_id"]) ? $_POST["rra_id"] : "0", isset($_POST["host_id"]) ? $_POST["host_id"] : "0", isset($_POST["host_grouping_type"]) ? $_POST["host_grouping_type"] : "1", isset($_POST["sort_children_type"]) ? $_POST["sort_children_type"] : "1", isset($_POST["propagate_changes"]) ? true : false); if (is_error_message()) { header("Location: tree.php?action=item_edit&tree_item_id=" . (empty($tree_item_id) ? $_POST["id"] : $tree_item_id) . "&tree_id=" . $_POST["graph_tree_id"] . "&parent_id=" . $_POST["parent_item_id"]); } else { /* modify for multi user start */ if ($_SESSION["permission"] < ACCESS_ADMINISTRATOR) { if (isset($_POST["local_graph_id"]) && $_POST["graph_tree_id"] == $_SESSION["public_tree_id"]) { exec("php ./cli/add_perms.php --user-id=" . $_SESSION["sess_user_id"] . " --item-type=graph --item-id=" . $_POST["local_graph_id"]); } } /* modify for multi user end */ header("Location: tree.php?action=edit&id=" . $_POST["graph_tree_id"]); } } }
function api_tree_item_save($id, $tree_id, $type, $parent_tree_item_id, $title, $local_graph_id, $rra_id, $host_id, $host_grouping_type, $sort_children_type, $propagate_changes) { global $config; input_validate_input_number($tree_id); input_validate_input_number($parent_tree_item_id); include_once($config["library_path"] . "/tree.php"); $parent_order_key = db_fetch_cell("select order_key from graph_tree_items where id=$parent_tree_item_id"); /* fetch some cache variables */ if (empty($id)) { /* new/save - generate new order key */ $order_key = get_next_tree_id($parent_order_key, "graph_tree_items", "order_key", "graph_tree_id=$tree_id"); }else{ /* edit/save - use old order_key */ $order_key = db_fetch_cell("select order_key from graph_tree_items where id=$id"); } /* duplicate graph check */ $search_key = substr($parent_order_key, 0, (tree_tier($parent_order_key) * CHARS_PER_TIER)); if (($type == TREE_ITEM_TYPE_GRAPH) && (sizeof(db_fetch_assoc("select id from graph_tree_items where local_graph_id='$local_graph_id' and rra_id='$rra_id' and graph_tree_id='$tree_id' and order_key like '$search_key" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', (MAX_TREE_DEPTH * CHARS_PER_TIER) - (strlen($search_key) + CHARS_PER_TIER)) . "'")) > 0)) { return 0; } $save["id"] = $id; $save["graph_tree_id"] = $tree_id; $save["title"] = form_input_validate($title, "title", "", ($type == TREE_ITEM_TYPE_HEADER ? false : true), 3); $save["order_key"] = $order_key; $save["local_graph_id"] = form_input_validate($local_graph_id, "local_graph_id", "", true, 3); $save["rra_id"] = form_input_validate($rra_id, "rra_id", "", true, 3); $save["host_id"] = form_input_validate($host_id, "host_id", "", true, 3); $save["host_grouping_type"] = form_input_validate($host_grouping_type, "host_grouping_type", "", true, 3); $save["sort_children_type"] = form_input_validate($sort_children_type, "sort_children_type", "", true, 3); $tree_item_id = 0; if (!is_error_message()) { $tree_item_id = sql_save($save, "graph_tree_items"); if ($tree_item_id) { raise_message(1); /* re-parent the branch if the parent item has changed */ if ($parent_tree_item_id != $tree_item_id) { reparent_branch($parent_tree_item_id, $tree_item_id); } $tree_sort_type = db_fetch_cell("select sort_type from graph_tree where id='$tree_id'"); /* tree item ordering */ if ($tree_sort_type == TREE_ORDERING_NONE) { /* resort our parent */ $parent_sorting_type = db_fetch_cell("select sort_children_type from graph_tree_items where id=$parent_tree_item_id"); if ((!empty($parent_tree_item_id)) && ($parent_sorting_type != TREE_ORDERING_NONE)) { sort_tree(SORT_TYPE_TREE_ITEM, $parent_tree_item_id, $parent_sorting_type); } /* if this is a header, sort direct children */ if (($type == TREE_ITEM_TYPE_HEADER) && ($sort_children_type != TREE_ORDERING_NONE)) { sort_tree(SORT_TYPE_TREE_ITEM, $tree_item_id, $sort_children_type); } /* tree ordering */ }else{ /* potential speed savings for large trees */ if (tree_tier($save["order_key"]) == 1) { sort_tree(SORT_TYPE_TREE, $tree_id, $tree_sort_type); }else{ sort_tree(SORT_TYPE_TREE_ITEM, $parent_tree_item_id, $tree_sort_type); } } /* if the user checked the 'Propagate Changes' box */ if (($type == TREE_ITEM_TYPE_HEADER) && ($propagate_changes == true)) { $search_key = preg_replace("/0+$/", "", $order_key); $tree_items = db_fetch_assoc("select graph_tree_items.id from graph_tree_items where graph_tree_items.host_id = 0 and graph_tree_items.local_graph_id = 0 and graph_tree_items.title != '' and graph_tree_items.order_key like '$search_key%%' and graph_tree_items.graph_tree_id='$tree_id'"); if (sizeof($tree_items) > 0) { foreach ($tree_items as $item) { db_execute("update graph_tree_items set sort_children_type = '$sort_children_type' where id = '" . $item["id"] . "'"); if ($sort_children_type != TREE_ORDERING_NONE) { sort_tree(SORT_TYPE_TREE_ITEM, $item["id"], $sort_children_type); } } } } }else{ raise_message(2); } } return $tree_item_id; }
function load_children($loading = array()) { if (isset($this->smart) || empty($this->id) || empty($this->uri)) { return false; } $db = DB::get(); if (empty($loading['orderby'])) { $loading['orderby'] = "name"; } switch (strtolower($loading['orderby'])) { case "id": $orderby = "cat.id"; break; case "slug": $orderby = "cat.slug"; break; case "count": $orderby = "total"; break; default: $orderby = "cat.name"; } if (empty($loading['order'])) { $loading['order'] = "ASC"; } switch (strtoupper($loading['order'])) { case "DESC": $order = "DESC"; break; default: $order = "ASC"; } $catalog_table = DatabaseObject::tablename(Catalog::$table); $children = $db->query("SELECT cat.*,count(sc.product) AS total FROM {$this->_table} AS cat LEFT JOIN {$catalog_table} AS sc ON sc.category=cat.id WHERE cat.uri like '%{$this->uri}%' AND cat.id <> {$this->id} GROUP BY cat.id ORDER BY cat.parent DESC,{$orderby} {$order},name ASC", AS_ARRAY); $children = sort_tree($children, $this->id); foreach ($children as &$child) { $this->children[$child->id] = new Category(); $this->children[$child->id]->populate($child); $this->children[$child->id]->depth = $child->depth; $this->children[$child->id]->total = $child->total; } if (!empty($this->children)) { return true; } return false; }
function categories_meta_box($Product) { $db =& DB::get(); $category_table = DatabaseObject::tablename(Category::$table); $categories = $db->query("SELECT id,name,parent FROM {$category_table} ORDER BY parent,name", AS_ARRAY); $categories = sort_tree($categories); if (empty($categories)) { $categories = array(); } $categories_menu = '<option value="0" rel="-1,-1">' . __('Parent Category', 'Shopp') . '…</option>'; foreach ($categories as $category) { $padding = str_repeat(" ", $category->depth * 3); $categories_menu .= '<option value="' . $category->id . '" rel="' . $category->parent . ',' . $category->depth . '">' . $padding . $category->name . '</option>'; } $selectedCategories = array(); foreach ($Product->categories as $category) { $selectedCategories[] = $category->id; } ?> <div id="category-menu" class="multiple-select short"> <ul> <?php $depth = 0; foreach ($categories as $category) { if ($category->depth > $depth) { echo "<li><ul>"; } ?> <?php if ($category->depth < $depth) { ?> <?php for ($i = $category->depth; $i < $depth; $i++) { ?> </ul></li> <?php } ?> <?php } ?> <li id="category-element-<?php echo $category->id; ?> "><input type="checkbox" name="categories[]" value="<?php echo $category->id; ?> " id="category-<?php echo $category->id; ?> " tabindex="3"<?php if (in_array($category->id, $selectedCategories)) { echo ' checked="checked"'; } ?> class="category-toggle" /><label for="category-<?php echo $category->id; ?> "><?php echo $category->name; ?> </label></li> <?php $depth = $category->depth; } ?> <?php for ($i = 0; $i < $depth; $i++) { ?> </ul></li> <?php } ?> </ul> </div> <div id="new-category"> <input type="text" name="new-category" value="" size="15" id="new-category" /><br /> <select name="new-category-parent"><?php echo $categories_menu; ?> </select> <button id="add-new-category" type="button" class="button-secondary" tabindex="2"><small><?php _e('Add', 'Shopp'); ?> </small></button> </div> <?php }
function api_tree_item_save($id, $tree_id, $type, $parent_tree_item_id, $title, $local_graph_id, $rra_id, $host_id, $host_grouping_type, $sort_children_type, $propagate_changes) { global $config; input_validate_input_number($tree_id); input_validate_input_number($parent_tree_item_id); include_once $config["library_path"] . "/tree.php"; db_execute("LOCK TABLES graph_tree_items WRITE, graph_tree READ, graph_templates_graph READ, host READ"); $parent_order_key = db_fetch_cell("select order_key from graph_tree_items where id={$parent_tree_item_id}"); /* fetch some cache variables */ if (empty($id)) { /* new/save - generate new order key */ $order_key = get_next_tree_id($parent_order_key, "graph_tree_items", "order_key", "graph_tree_id={$tree_id}"); } else { /* edit/save - use old order_key */ $order_key = db_fetch_cell("select order_key from graph_tree_items where id={$id}"); } /* duplicate graph check */ $search_key = substr($parent_order_key, 0, tree_tier($parent_order_key) * CHARS_PER_TIER); if ($id == 0 && $type == TREE_ITEM_TYPE_GRAPH && sizeof(db_fetch_assoc("select id from graph_tree_items where local_graph_id='{$local_graph_id}' and graph_tree_id='{$tree_id}' and order_key like '{$search_key}" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', MAX_TREE_DEPTH * CHARS_PER_TIER - (strlen($search_key) + CHARS_PER_TIER)) . "'")) > 0) { $value = db_fetch_cell("select id from graph_tree_items where local_graph_id='{$local_graph_id}' and graph_tree_id='{$tree_id}' and order_key like '{$search_key}" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', MAX_TREE_DEPTH * CHARS_PER_TIER - (strlen($search_key) + CHARS_PER_TIER)) . "'"); db_execute("UNLOCK TABLES"); return $value; } /* Duplicate header check */ if ($id == 0 && $type == TREE_ITEM_TYPE_HEADER) { if (sizeof(db_fetch_assoc("SELECT id FROM graph_tree_items WHERE title='{$title}' AND graph_tree_id='{$tree_id}' AND order_key LIKE '{$search_key}" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', MAX_TREE_DEPTH * CHARS_PER_TIER - (strlen($search_key) + CHARS_PER_TIER)) . "' AND order_key NOT LIKE '{$search_key}" . str_repeat('0', MAX_TREE_DEPTH * CHARS_PER_TIER - strlen($search_key)) . "%'")) > 0) { $value = db_fetch_cell("select id from graph_tree_items where title='{$title}' and graph_tree_id='{$tree_id}' and order_key like '{$search_key}" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', MAX_TREE_DEPTH * CHARS_PER_TIER - (strlen($search_key) + CHARS_PER_TIER)) . "'"); db_execute("UNLOCK TABLES"); return $value; } } /* Duplicate host check */ if ($id == 0 && $type == TREE_ITEM_TYPE_HOST && sizeof(db_fetch_assoc("select id from graph_tree_items where host_id='{$host_id}' and local_graph_id='{$local_graph_id}' and graph_tree_id='{$tree_id}' and order_key like '{$search_key}" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', MAX_TREE_DEPTH * CHARS_PER_TIER - (strlen($search_key) + CHARS_PER_TIER)) . "'")) > 0) { $value = db_fetch_cell("select id from graph_tree_items where host_id='{$host_id}' and local_graph_id='{$local_graph_id}' and graph_tree_id='{$tree_id}' and order_key like '{$search_key}" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', MAX_TREE_DEPTH * CHARS_PER_TIER - (strlen($search_key) + CHARS_PER_TIER)) . "'"); db_execute("UNLOCK TABLES"); return $value; } $save["id"] = $id; $save["graph_tree_id"] = $tree_id; $save["title"] = form_input_validate($title, "title", "", $type == TREE_ITEM_TYPE_HEADER ? false : true, 3); $save["order_key"] = $order_key; $save["local_graph_id"] = form_input_validate($local_graph_id, "local_graph_id", "", true, 3); $save["rra_id"] = form_input_validate($rra_id, "rra_id", "", true, 3); $save["host_id"] = form_input_validate($host_id, "host_id", "", true, 3); $save["host_grouping_type"] = form_input_validate($host_grouping_type, "host_grouping_type", "", true, 3); $save["sort_children_type"] = form_input_validate($sort_children_type, "sort_children_type", "", true, 3); $tree_item_id = 0; if (!is_error_message()) { $tree_item_id = sql_save($save, "graph_tree_items"); if ($tree_item_id) { raise_message(1); /* re-parent the branch if the parent item has changed */ if ($parent_tree_item_id != $tree_item_id) { reparent_branch($parent_tree_item_id, $tree_item_id); } $tree_sort_type = db_fetch_cell("select sort_type from graph_tree where id='{$tree_id}'"); /* tree item ordering */ if ($tree_sort_type == TREE_ORDERING_NONE) { /* resort our parent */ $parent_sorting_type = db_fetch_cell("select sort_children_type from graph_tree_items where id={$parent_tree_item_id}"); if (!empty($parent_tree_item_id) && $parent_sorting_type != TREE_ORDERING_NONE) { sort_tree(SORT_TYPE_TREE_ITEM, $parent_tree_item_id, $parent_sorting_type); } /* if this is a header, sort direct children */ if ($type == TREE_ITEM_TYPE_HEADER && $sort_children_type != TREE_ORDERING_NONE) { sort_tree(SORT_TYPE_TREE_ITEM, $tree_item_id, $sort_children_type); } /* tree ordering */ } else { /* potential speed savings for large trees */ if (tree_tier($save["order_key"]) == 1) { sort_tree(SORT_TYPE_TREE, $tree_id, $tree_sort_type); } else { sort_tree(SORT_TYPE_TREE_ITEM, $parent_tree_item_id, $tree_sort_type); } } /* if the user checked the 'Propagate Changes' box */ if ($type == TREE_ITEM_TYPE_HEADER && $propagate_changes == true) { $search_key = preg_replace("/0+\$/", "", $order_key); $tree_items = db_fetch_assoc("select\n\t\t\t\t\tgraph_tree_items.id\n\t\t\t\t\tfrom graph_tree_items\n\t\t\t\t\twhere graph_tree_items.host_id = 0\n\t\t\t\t\tand graph_tree_items.local_graph_id = 0\n\t\t\t\t\tand graph_tree_items.title != ''\n\t\t\t\t\tand graph_tree_items.order_key like '{$search_key}%%'\n\t\t\t\t\tand graph_tree_items.graph_tree_id='{$tree_id}'"); if (sizeof($tree_items) > 0) { foreach ($tree_items as $item) { db_execute("update graph_tree_items set sort_children_type = '{$sort_children_type}' where id = '" . $item["id"] . "'"); if ($sort_children_type != TREE_ORDERING_NONE) { sort_tree(SORT_TYPE_TREE_ITEM, $item["id"], $sort_children_type); } } } } } else { raise_message(2); } } db_execute("UNLOCK TABLES"); return $tree_item_id; }
function form_save() { require_once(CACTI_BASE_PATH . "/include/graph_tree/graph_tree_constants.php"); if (isset($_POST["save_component_tree"])) { $save["id"] = $_POST["id"]; $save["name"] = form_input_validate($_POST["name"], "name", "", false, 3); $save["sort_type"] = form_input_validate($_POST["sort_type"], "sort_type", "", true, 3); if (!is_error_message()) { $tree_id = sql_save($save, "graph_tree"); if ($tree_id) { raise_message(1); /* sort the tree using the algorithm chosen by the user */ sort_tree(SORT_TYPE_TREE, $tree_id, get_request_var_post("sort_type")); }else{ raise_message(2); } } if ((is_error_message()) || (empty($_POST["id"]))) { header("Location: tree.php?action=edit&id=" . (empty($tree_id) ? $_POST["id"] : $tree_id)); }else{ header("Location: tree.php"); } exit; }elseif (isset($_POST["save_component_tree_item"])) { $tree_item_id = api_tree_item_save($_POST["id"], $_POST["graph_tree_id"], $_POST["type"], $_POST["parent_item_id"], (isset($_POST["title"]) ? $_POST["title"] : ""), (isset($_POST["local_graph_id"]) ? $_POST["local_graph_id"] : "0"), (isset($_POST["rra_id"]) ? $_POST["rra_id"] : "0"), (isset($_POST["device_id"]) ? $_POST["device_id"] : "0"), (isset($_POST["device_grouping_type"]) ? $_POST["device_grouping_type"] : "1"), (isset($_POST["sort_children_type"]) ? $_POST["sort_children_type"] : "1"), (isset($_POST["propagate_changes"]) ? true : false)); if (is_error_message()) { header("Location: tree.php?action=item_edit&tree_item_id=" . (empty($tree_item_id) ? $_POST["id"] : $tree_item_id) . "&tree_id=" . $_POST["graph_tree_id"] . "&parent_id=" . $_POST["parent_item_id"]); }else{ header("Location: tree.php?action=edit&id=" . $_POST["graph_tree_id"]); } exit; } }
display_help($me); exit(1); } $tree["id"] = 0; # create a new tree item unset($tree["sort_type_cli"]); # remove for save reset($tree); if ($debug) { print __("Save Tree Array:") . "\n"; print_r($tree); } else { $tree["id"] = sql_save($tree, "graph_tree"); if ($tree["id"] === 0) { echo __("Failed to create Tree") . "\n"; } else { sort_tree(SORT_TYPE_TREE, $tree["id"], $tree["sort_type"]); echo __("Tree Created - tree-id: (%d)", $tree["id"]) . "\n"; } } break; case (strtolower($tree_types[TREE_TYPE_NODE])): # name is used both for tree and tree_items if (isset($tree["name"])) $tree_item["title"] = $tree["name"]; if (isset($tree["sort_type_cli"])) $tree_item["sort_type_cli"] = $tree["sort_type_cli"]; if (isset($tree["id"])) $tree_item["graph_tree_id"] = $tree["id"]; # at least one matching criteria for tree item has to be defined if (!sizeof($tree_item)) {
function form_save() { /* clear graph tree cache on save - affects current user only, other users should see changes in <5 minutes */ if (isset($_SESSION['dhtml_tree'])) { unset($_SESSION['dhtml_tree']); } if (isset($_POST["save_component_tree"])) { /* ================= input validation ================= */ input_validate_input_number(get_request_var_post("id")); /* ==================================================== */ $save["id"] = $_POST["id"]; $save["name"] = form_input_validate($_POST["name"], "name", "", false, 3); $save["sort_type"] = form_input_validate($_POST["sort_type"], "sort_type", "", true, 3); if (!is_error_message()) { $tree_id = sql_save($save, "graph_tree"); if ($tree_id) { raise_message(1); /* sort the tree using the algorithm chosen by the user */ sort_tree(SORT_TYPE_TREE, $tree_id, $_POST["sort_type"]); } else { raise_message(2); } } header("Location: tree.php?action=edit&id=" . (empty($tree_id) ? $_POST["id"] : $tree_id)); } elseif (isset($_POST["save_component_tree_item"])) { /* ================= input validation ================= */ input_validate_input_number(get_request_var_post("id")); input_validate_input_number(get_request_var_post("graph_tree_id")); input_validate_input_number(get_request_var_post("type")); input_validate_input_number(get_request_var_post("parent_item_id")); input_validate_input_number(get_request_var_post("local_graph_id")); input_validate_input_number(get_request_var_post("host_id")); input_validate_input_number(get_request_var_post("rra_id")); input_validate_input_number(get_request_var_post("host_group_type")); /* ==================================================== */ $tree_item_id = api_tree_item_save($_POST["id"], $_POST["graph_tree_id"], $_POST["type"], $_POST["parent_item_id"], isset($_POST["title"]) ? $_POST["title"] : "", isset($_POST["local_graph_id"]) ? $_POST["local_graph_id"] : "0", isset($_POST["rra_id"]) ? $_POST["rra_id"] : "0", isset($_POST["host_id"]) ? $_POST["host_id"] : "0", isset($_POST["host_grouping_type"]) ? $_POST["host_grouping_type"] : "1", isset($_POST["sort_children_type"]) ? $_POST["sort_children_type"] : "1", isset($_POST["propagate_changes"]) ? true : false); if (is_error_message()) { header("Location: tree.php?action=item_edit&tree_item_id=" . (empty($tree_item_id) ? $_POST["id"] : $tree_item_id) . "&tree_id=" . $_POST["graph_tree_id"] . "&parent_id=" . $_POST["parent_item_id"]); } else { header("Location: tree.php?action=edit&id=" . $_POST["graph_tree_id"]); } } }
function load_categories($filtering = false, $showsmart = false, $results = false) { $db = DB::get(); if (empty($filtering['columns'])) { $filtering['columns'] = "cat.id,cat.parent,cat.name,cat.description,cat.uri,cat.slug,count(DISTINCT pd.id) AS total,IF(SUM(IF(pt.inventory='off',1,0) OR pt.inventory IS NULL)>0,'off','on') AS inventory, SUM(pt.stock) AS stock"; } if (!empty($filtering['limit'])) { $filtering['limit'] = "LIMIT " . $filtering['limit']; } else { $filtering['limit'] = ""; } // if (!$this->outofstock) $filtering['where'] .= (empty($filtering['where'])?"":" AND ")."(pt.inventory='off' OR (pt.inventory='on' AND pt.stock > 0))"; if (empty($filtering['where'])) { $filtering['where'] = "true"; } if (empty($filtering['orderby'])) { $filtering['orderby'] = "name"; } switch (strtolower($filtering['orderby'])) { case "id": $orderby = "cat.id"; break; case "slug": $orderby = "cat.slug"; break; case "count": $orderby = "total"; break; default: $orderby = "cat.name"; } if (empty($filtering['order'])) { $filtering['order'] = "ASC"; } switch (strtoupper($filtering['order'])) { case "DESC": $order = "DESC"; break; default: $order = "ASC"; } $category_table = DatabaseObject::tablename(Category::$table); $product_table = DatabaseObject::tablename(Product::$table); $price_table = DatabaseObject::tablename(Price::$table); $query = "SELECT {$filtering['columns']} FROM {$category_table} AS cat LEFT JOIN {$this->_table} AS sc ON sc.category=cat.id LEFT JOIN {$product_table} AS pd ON sc.product=pd.id LEFT JOIN {$price_table} AS pt ON pt.product=pd.id AND pt.type != 'N/A' WHERE {$filtering['where']} GROUP BY cat.id ORDER BY cat.parent DESC,{$orderby} {$order} {$filtering['limit']}"; $categories = $db->query($query, AS_ARRAY); if (count($categories) > 1) { $categories = sort_tree($categories); } if ($results) { return $categories; } foreach ($categories as $category) { $category->outofstock = false; if (isset($category->inventory)) { if ($category->inventory == "on" && $category->stock == 0) { $category->outofstock = true; } if (!$this->outofstock && $category->outofstock) { continue; } } $this->categories[$category->id] = new Category(); $this->categories[$category->id]->populate($category); if (isset($category->depth)) { $this->categories[$category->id]->depth = $category->depth; } else { $this->categories[$category->id]->depth = 0; } if (isset($category->total)) { $this->categories[$category->id]->total = $category->total; } else { $this->categories[$category->id]->total = 0; } if (isset($category->stock)) { $this->categories[$category->id]->stock = $category->stock; } else { $this->categories[$category->id]->stock = 0; } if (isset($category->outofstock)) { $this->categories[$category->id]->outofstock = $category->outofstock; } $this->categories[$category->id]->children = false; if ($category->total > 0 && isset($this->categories[$category->parent])) { $this->categories[$category->parent]->children = true; } } if ($showsmart == "before" || $showsmart == "after") { $this->smart_categories($showsmart); } return true; }
/** * Recursively sorts a heirarchical tree of data * * @param array $item The item data to be sorted * @param int $parent (internal) The parent item of the current iteration * @param int $key (internal) The identified index of the parent item in the current iteration * @param int $depth (internal) The number of the nested depth in the current iteration * @return array The sorted tree of data **/ function sort_tree ($items,$parent=0,$key=-1,$depth=-1) { $depth++; $position = 1; $result = array(); if ($items) { foreach ($items as $item) { // Preserve initial priority if (isset($item->priority)) $item->_priority = $item->priority; if ($item->parent == $parent) { $item->parentkey = $key; $item->depth = $depth; $item->priority = $position++; $result[] = $item; $children = sort_tree($items, $item->id, count($result)-1, $depth); $result = array_merge($result,$children); // Add children in as they are found } } } $depth--; return $result; }
/** * Renders a drop-down menu for selecting parent categories * * @since 1.0 * @param int $selection The id of the currently selected parent category * @param int $current The id of the currently edited category * @return void Description... **/ function menu ($selection=false,$current=false) { $db = DB::get(); $table = DatabaseObject::tablename(Category::$table); $categories = $db->query("SELECT id,name,parent FROM $table ORDER BY parent,name",AS_ARRAY); $categories = sort_tree($categories); $options = ''; foreach ($categories as $category) { $padding = str_repeat(" ",$category->depth*3); $selected = ($category->id == $selection)?' selected="selected"':''; $disabled = ($current && $category->id == $current)?' disabled="disabled"':''; $options .= '<option value="'.$category->id.'"'.$selected.$disabled.'>'.$padding.esc_html($category->name).'</option>'; } return $options; }
/** * Load categories from the catalog index * * @since 1.0 * @version 1.1 * * @param array $loading (optional) Loading options for building the query * @param boolean $showsmart (optional) Include smart categories in the listing * @param boolean $results (optional) Return the raw structure of results without aggregate processing * @return boolean|object True when categories are loaded and processed, object of results when $results is set **/ function load_categories ($loading=array(),$showsmart=false,$results=false) { $db = DB::get(); $category_table = DatabaseObject::tablename(Category::$table); $product_table = DatabaseObject::tablename(Product::$table); $price_table = DatabaseObject::tablename(Price::$table); $defaults = array( 'columns' => "cat.id,cat.parent,cat.name,cat.description,cat.uri,cat.slug,count(DISTINCT pd.id) AS total,IF(SUM(IF(pt.inventory='off',1,0) OR pt.inventory IS NULL)>0,'off','on') AS inventory, SUM(pt.stock) AS stock", 'where' => array(), 'joins' => array( "LEFT JOIN $this->_table AS sc ON sc.parent=cat.id AND sc.type='category'", "LEFT JOIN $product_table AS pd ON sc.product=pd.id", "LEFT JOIN $price_table AS pt ON pt.product=pd.id AND pt.type != 'N/A'" ), 'limit' => false, 'orderby' => 'name', 'order' => 'ASC', 'parent' => false, 'ancestry' => false, 'outofstock' => $this->outofstock ); $options = array_merge($defaults,$loading); extract($options); if (!is_array($where)) $where = array($where); if (!$outofstock) $where[] = "(pt.inventory='off' OR (pt.inventory='on' AND pt.stock > 0))"; if ($parent !== false) $where[] = "cat.parent=".$parent; else $parent = 0; if ($ancestry) { if (!empty($where)) $where = array("cat.id IN (SELECT parent FROM $category_table WHERE parent != 0) OR (".join(" AND ",$where).")"); else $where = array("cat.id IN (SELECT parent FROM $category_table WHERE parent != 0)"); } switch(strtolower($orderby)) { case "id": $orderby = "cat.id"; break; case "slug": $orderby = "cat.slug"; break; case "count": $orderby = "total"; break; default: $orderby = "cat.name"; } switch(strtoupper($order)) { case "DESC": $order = "DESC"; break; default: $order = "ASC"; } if ($limit !== false) $limit = "LIMIT $limit"; $joins = join(' ',$joins); if (!empty($where)) $where = "WHERE ".join(' AND ',$where); else $where = false; $query = "SELECT $columns FROM $category_table AS cat $joins $where GROUP BY cat.id ORDER BY cat.parent DESC,cat.priority,$orderby $order $limit"; $categories = $db->query($query,AS_ARRAY); if (count($categories) > 1) $categories = sort_tree($categories, $parent); if ($results) return $categories; foreach ($categories as $category) { $category->outofstock = false; if (isset($category->inventory)) { if ($category->inventory == "on" && $category->stock == 0) $category->outofstock = true; if (!$this->outofstock && $category->outofstock) continue; } $id = '_'.$category->id; $this->categories[$id] = new Category(); $this->categories[$id]->populate($category); if (isset($category->depth)) $this->categories[$id]->depth = $category->depth; else $this->categories[$id]->depth = 0; if (isset($category->total)) $this->categories[$id]->total = $category->total; else $this->categories[$id]->total = 0; if (isset($category->stock)) $this->categories[$id]->stock = $category->stock; else $this->categories[$id]->stock = 0; if (isset($category->outofstock)) $this->categories[$id]->outofstock = $category->outofstock; $this->categories[$id]->_children = false; if (isset($category->total) && $category->total > 0 && isset($this->categories[$category->parent])) { $ancestor = $category->parent; // Recursively flag the ancestors as having children while (isset($this->categories[$ancestor])) { $this->categories[$ancestor]->_children = true; $ancestor = $this->categories[$ancestor]->parent; } } } if ($showsmart == "before" || $showsmart == "after") $this->smart_categories($showsmart); return true; }
/** * sort_tree * Sorts a heirarchical tree of data */ function sort_tree($items, $parent = 0, $key = -1, $depth = -1) { $depth++; $result = array(); if ($items) { foreach ($items as $item) { if ($item->parent == $parent) { $item->parentkey = $key; $item->depth = $depth; $result[] = $item; $children = sort_tree($items, $item->id, count($result) - 1, $depth); $result = array_merge($result, $children); // Add children in as they are found } } } $depth--; return $result; }
do_meta_boxes('admin_page_shopp-products-edit', 'advanced', $Product); ?> </div> </div> </div> <!-- #poststuff --> </form> </div> <?php } else { $db =& DB::get(); $category_table = DatabaseObject::tablename(Category::$table); $categories = $db->query("SELECT id,name,parent FROM {$category_table} ORDER BY parent,name", AS_ARRAY); $categories = sort_tree($categories); if (empty($categories)) { $categories = array(); } $categories_menu = '<option value="0" rel="-1,-1">' . __('Parent Category', 'Shopp') . '…</option>'; foreach ($categories as $category) { $padding = str_repeat(" ", $category->depth * 3); $categories_menu .= '<option value="' . $category->id . '" rel="' . $category->parent . ',' . $category->depth . '">' . $padding . $category->name . '</option>'; } $selectedCategories = array(); foreach ($Product->categories as $category) { $selectedCategories[] = $category->id; } ?> <div class="wrap shopp">