Added support for document templates.
[openemr.git] / controllers / C_DocumentCategory.class.php
blob146259994c8b231662e04ea9c07da483c41bc00f
1 <?php
3 require_once (dirname(__FILE__) . "/../library/classes/Controller.class.php");
4 require_once(dirname(__FILE__) . "/../library/classes/CategoryTree.class.php");
5 require_once(dirname(__FILE__) . "/../library/classes/TreeMenu.php");
7 class C_DocumentCategory extends Controller {
9 var $template_mod;
10 var $document_categories;
11 var $tree;
12 var $link;
14 function C_DocumentCategory($template_mod = "general") {
15 parent::Controller();
16 $this->document_categories = array();
17 $this->template_mod = $template_mod;
18 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
19 $this->assign("CURRENT_ACTION", $GLOBALS['webroot']."/controller.php?" . "practice_settings&document_category&");
20 $this->link = $GLOBALS['webroot']."/controller.php?" . "document_category&";
21 $this->assign("STYLE", $GLOBALS['style']);
23 $t = new CategoryTree(1);
24 //print_r($t->tree);
25 $this->tree = $t;
29 function default_action() {
30 return $this->list_action();
33 function list_action() {
34 //$this->tree->rebuild_tree(1,1);
36 $icon = 'folder.gif';
37 $expandedIcon = 'folder-expanded.gif';
38 $menu = new HTML_TreeMenu();
39 $this->_last_node = null;
40 $rnode = $this->_array_recurse($this->tree->tree);
42 $menu->addItem($rnode);
43 $treeMenu = &new HTML_TreeMenu_DHTML($menu, array('images' => 'images', 'defaultClass' => 'treeMenuDefault'));
44 $this->assign("tree_html",$treeMenu->toHTML());
46 return $this->fetch($GLOBALS['template_dir'] . "document_categories/" . $this->template_mod . "_list.html");
49 function add_node_action($parent_is) {
50 //echo $parent_is ."<br>";
51 //echo $this->tree->get_node_name($parent_is);
52 $this->assign("parent_name",$this->tree->get_node_name($parent_is));
53 $this->assign("parent_is",$parent_is);
54 $this->assign("add_node",true);
55 return $this->list_action();
58 function add_node_action_process() {
59 if ($_POST['process'] != "true")
60 return;
61 $name = $_POST['name'];
62 $parent_is = $_POST['parent_is'];
63 $parent_name = $this->tree->get_node_name($parent_is);
64 $this->tree->add_node($parent_is,$name);
65 $trans_message = xl('Sub-category','','',' ') . "'" . xl_document_category($name) . "'" . xl('successfully added to category,','',' ',' ') . "'" . $parent_name . "'";
66 $this->assign("message",$trans_message);
67 $this->_state = false;
68 return $this->list_action();
71 function delete_node_action_process($id) {
72 if ($_POST['process'] != "true")
73 return;
74 $category_name = $this->tree->get_node_name($id);
75 $category_info = $this->tree->get_node_info($id);
76 $parent_name = $this->tree->get_node_name($category_info['parent']);
78 if($parent_name != false && $parent_name != '')
80 $this->tree->delete_node($id);
81 $trans_message = xl('Category','','',' ') . "'" . $category_name . "'" . xl('had been successfully deleted. Any sub-categories if present were moved below','',' ',' ') . "'" . $parent_name . "'" . xl('.') . "<br>";
82 $this->assign("message",$trans_message);
84 if (is_numeric($id)) {
85 $sql = "UPDATE categories_to_documents set category_id = '" . $category_info['parent'] . "' where category_id = '" . $id ."'";
86 $this->tree->_db->Execute($sql);
89 else
91 $trans_message = xl('Category','','',' ') . "'" . $category_name . "'" . xl('is a root node and can not be deleted.','',' ') . "<br>";
92 $this->assign("message",$trans_message);
94 $this->_state = false;
96 return $this->list_action();
99 function edit_action_process() {
100 if ($_POST['process'] != "true")
101 return;
102 //print_r($_POST);
103 if (is_numeric($_POST['id'])) {
104 $this->document_categories[0] = new Pharmacy($_POST['id']);
106 else {
107 $this->document_categories[0] = new Pharmacy();
109 parent::populate_object($this->document_categories[0]);
110 //print_r($this->document_categories[0]);
111 //echo $this->document_categories[0]->toString(true);
112 $this->document_categories[0]->persist();
113 //echo "action processeed";
114 $_POST['process'] = "";
117 function &_array_recurse($array) {
118 if (!is_array($array)) {
119 $array = array();
121 $node = &$this->_last_node;
122 $icon = 'folder.gif';
123 $expandedIcon = 'folder-expanded.gif';
124 foreach($array as $id => $ar) {
125 if (is_array($ar) || !empty($id)) {
126 if ($node == null) {
128 //echo "r:" . $this->tree->get_node_name($id) . "<br>";
129 $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));
130 $this->_last_node = &$rnode;
131 $node = &$rnode;
133 else {
134 //echo "p:" . $this->tree->get_node_name($id) . "<br>";
135 $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)));
137 if (is_array($ar)) {
138 $this->_array_recurse($ar);
141 else {
142 if ($id === 0 && !empty($ar)) {
143 $info = $this->tree->get_node_info($id);
144 //echo "b:" . $this->tree->get_node_name($id) . "<br>";
145 $node->addItem(new HTML_TreeNode(array('text' => $info['value'], 'link' => $this->_link("add_node",true) . "parent_id=" . ($id) . "&", 'icon' => $icon, 'expandedIcon' => $expandedIcon)));
147 else {
148 //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
149 //this conditional tree could be more efficient but working with trees makes my head hurt, TODO
150 if ($id !== 0 && is_object($node)) {
151 //echo "n:" . $this->tree->get_node_name($id) . "<br>";
152 $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)));
157 return $node;