minor fix to prior commit
[openemr.git] / controllers / C_DocumentCategory.class.php
blobfcb1439bca50f67078e6df46ee779fb4e3d87bb2
1 <?php
4 class C_DocumentCategory extends Controller {
6 var $template_mod;
7 var $document_categories;
8 var $tree;
9 var $link;
11 function __construct($template_mod = "general") {
12 parent::__construct();
13 $this->document_categories = array();
14 $this->template_mod = $template_mod;
15 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
16 $this->assign("CURRENT_ACTION", $GLOBALS['webroot']."/controller.php?" . "practice_settings&document_category&");
17 $this->link = $GLOBALS['webroot']."/controller.php?" . "document_category&";
18 $this->assign("STYLE", $GLOBALS['style']);
19 $this->assign("V_JS_INCLUDES", $GLOBALS['v_js_includes']);
21 $t = new CategoryTree(1);
22 //print_r($t->tree);
23 $this->tree = $t;
27 function default_action() {
28 return $this->list_action();
31 function list_action() {
32 //$this->tree->rebuild_tree(1,1);
34 $icon = 'folder.gif';
35 $expandedIcon = 'folder-expanded.gif';
36 $menu = new HTML_TreeMenu();
37 $this->_last_node = null;
38 $rnode = $this->_array_recurse($this->tree->tree);
40 $menu->addItem($rnode);
41 $treeMenu = new HTML_TreeMenu_DHTML($menu, array('images' => 'images', 'defaultClass' => 'treeMenuDefault'));
42 $this->assign("tree_html",$treeMenu->toHTML());
44 return $this->fetch($GLOBALS['template_dir'] . "document_categories/" . $this->template_mod . "_list.html");
47 function add_node_action($parent_is) {
48 //echo $parent_is ."<br>";
49 //echo $this->tree->get_node_name($parent_is);
50 $info = $this->tree->get_node_info($parent_is);
51 $this->assign("parent_name",$this->tree->get_node_name($parent_is));
52 $this->assign("parent_is",$parent_is);
53 $this->assign("add_node",true);
54 $this->assign("edit_node", false);
55 $this->assign("VALUE", '');
56 // Access control defaults to that of the parent.
57 $this->assign("ACO_OPTIONS", "<option value=''></option>" . gen_aco_html_options($info['aco_spec']));
58 return $this->list_action();
61 function add_node_action_process() {
62 if ($_POST['process'] != "true") return;
63 $name = $_POST['name'];
64 $parent_is = $_POST['parent_is'];
65 $parent_name = $this->tree->get_node_name($parent_is);
66 $this->tree->add_node($parent_is, $name, $_POST['value'], $_POST['aco_spec']);
67 $trans_message = xl('Sub-category','','',' ') . "'" . xl_document_category($name) . "'" . xl('successfully added to category,','',' ',' ') . "'" . $parent_name . "'";
68 $this->assign("message",$trans_message);
69 $this->_state = false;
70 return $this->list_action();
73 function edit_node_action($parent_is) {
74 $info = $this->tree->get_node_info($parent_is);
75 $this->assign("parent_is",$parent_is);
76 $this->assign("NAME" , $this->tree->get_node_name($parent_is));
77 $this->assign("VALUE", $info['value']);
78 $this->assign("ACO_OPTIONS", "<option value=''></option>" . gen_aco_html_options($info['aco_spec']));
79 $this->assign("add_node",false);
80 $this->assign("edit_node",true);
81 return $this->list_action();
84 function edit_node_action_process() {
85 if ($_POST['process'] != "true") return;
86 $parent_is = $_POST['parent_is'];
87 $this->tree->edit_node($parent_is, $_POST['name'], $_POST['value'], $_POST['aco_spec']);
88 $trans_message = xl('Category changed.');
89 $this->assign("message", $trans_message);
90 $this->_state = false;
91 return $this->list_action();
94 function delete_node_action_process($id) {
95 if ($_POST['process'] != "true")
96 return;
97 $category_name = $this->tree->get_node_name($id);
98 $category_info = $this->tree->get_node_info($id);
99 $parent_name = $this->tree->get_node_name($category_info['parent']);
101 if($parent_name != false && $parent_name != '')
103 $this->tree->delete_node($id);
104 $trans_message = xl('Category','','',' ') . "'" . $category_name . "'" . xl('had been successfully deleted. Any sub-categories if present were moved below','',' ',' ') . "'" . $parent_name . "'" . xl('.') . "<br>";
105 $this->assign("message",$trans_message);
107 if (is_numeric($id)) {
108 $sql = "UPDATE categories_to_documents set category_id = '" . $category_info['parent'] . "' where category_id = '" . $id ."'";
109 $this->tree->_db->Execute($sql);
112 else
114 $trans_message = xl('Category','','',' ') . "'" . $category_name . "'" . xl('is a root node and can not be deleted.','',' ') . "<br>";
115 $this->assign("message",$trans_message);
117 $this->_state = false;
119 return $this->list_action();
122 function &_array_recurse($array) {
123 if (!is_array($array)) {
124 $array = array();
126 $node = &$this->_last_node;
127 $icon = 'folder.gif';
128 $expandedIcon = 'folder-expanded.gif';
129 foreach($array as $id => $ar) {
130 if (is_array($ar) || !empty($id)) {
131 if ($node == null) {
133 //echo "r:" . $this->tree->get_node_name($id) . "<br>";
134 $rnode = new HTML_TreeNode(array('text' => $this->tree->get_node_name($id), 'link' => $this->_link("add_node",true) . "parent_id=" . ($id) . "&", 'icon' => $icon, 'expandedIcon' => $expandedIcon, 'expanded' => false));
135 $this->_last_node = &$rnode;
136 $node = &$rnode;
138 else {
139 //echo "p:" . $this->tree->get_node_name($id) . "<br>";
140 $this->_last_node = &$node->addItem(new HTML_TreeNode(array('text' => $this->tree->get_node_name($id), 'link' => $this->_link("add_node",true) . "parent_id=" . ($id) . "&", 'icon' => $icon, 'expandedIcon' => $expandedIcon)));
142 if (is_array($ar)) {
143 $this->_array_recurse($ar);
146 else {
147 if ($id === 0 && !empty($ar)) {
148 $info = $this->tree->get_node_info($id);
149 //echo "b:" . $this->tree->get_node_name($id) . "<br>";
150 $node->addItem(new HTML_TreeNode(array('text' => $info['value'], 'link' => $this->_link("add_node",true) . "parent_id=" . ($id) . "&", 'icon' => $icon, 'expandedIcon' => $expandedIcon)));
152 else {
153 //there is a third case that is implicit here when title === 0 and $ar is empty, in that case we do not want to do anything
154 //this conditional tree could be more efficient but working with trees makes my head hurt, TODO
155 if ($id !== 0 && is_object($node)) {
156 //echo "n:" . $this->tree->get_node_name($id) . "<br>";
157 $node->addItem(new HTML_TreeNode(array('text' => $this->tree->get_node_name($id), 'link' => $this->_link("add_node",true) . "parent_id=" . ($id) . "&", 'icon' => $icon, 'expandedIcon' => $expandedIcon)));
162 return $node;