removed dangling </table>
[openemr.git] / library / classes / Controller.class.php
blob7f3d2f726e721136296dde666289a05116fa47ee
1 <?php
3 require_once(dirname(__FILE__) . "/../Smarty.class.php");
4 define("SMARTY_DIR", dirname(__FILE__) . "/../");
6 class Controller extends Smarty {
8 var $_current_action;
9 var $_state;
10 var $_args = array();
12 function Controller() {
13 parent::Smarty();
14 $this->template_mod = "general";
15 $this->_current_action = "";
16 $this->_state = true;
17 $this->compile_dir = $GLOBALS['fileroot'] . "/interface/main/calendar/modules/PostCalendar/pntemplates/compiled";
18 $this->compile_check = true;
19 $this->assign("PROCESS", "true");
20 $this->assign("HEADER", "<html><head></head><body>");
21 $this->assign("FOOTER", "</body></html>");
22 $this->assign("CONTROLLER", "controller.php?");
23 $this->assign("CONTROLLER_THIS", "controller.php?" . $_SERVER['QUERY_STRING']);
26 function set_current_action($action) {
27 $this->_current_action = $action;
30 function default_action() {
31 echo "<html><body></body></html>";
34 function process_action() {
35 $this->default_action();
38 function populate_object(&$obj) {
39 if(!is_object($obj)) {
40 $this->function_argument_error();
43 foreach($_POST as $varname => $var) {
44 $varname = preg_replace("/[^A-Za-z0-9_]/","",$varname);
45 $func = "set_" . $varname;
46 if ( (!(strpos("_",$varname) === 0)) && is_callable(array($obj,$func)) ) {
47 //echo "c: $func on w: " . $var . "<br />";
48 if ((get_magic_quotes_gpc() || get_magic_quotes_runtime()) && !is_array($var) ) {
49 $var = stripslashes($var);
51 call_user_func_array(array(&$obj,$func),array($var, $_POST));
55 return true;
58 function function_argument_error() {
59 $this->display($GLOBALS['template_dir'] . "error/" . $this->template_mod . "_function_argument.html");
60 exit;
63 function i_once($file) {
64 return include_once($file);
67 function act($qarray) {
69 if (isset($_GET['process'])){
70 unset($_GET['process']);
71 unset($qarray['process']);
72 $_POST['process'] = "true";
74 $args = array_reverse(array_keys($qarray));
75 $c_name = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
76 $parts = split("_",$c_name);
77 $name = "";
79 foreach($parts as $p) {
80 $name .= ucfirst($p);
83 $c_name = $name;
84 $c_action = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
85 $args = array_reverse($args);
87 // load dutch version for C_Prescription.class
88 if ( (LANGUAGE == '5') && ($c_name == "Prescription") ) $c_name .= 'dutch';
90 if(!@call_user_func(array(Controller,"i_once"),$GLOBALS['fileroot'] ."/controllers/C_" . $c_name . ".class.php")) {
91 echo "Unable to load controller $name\n, please check the first argument supplied in the URL and try again";
92 exit;
95 $obj_name = "C_" . $c_name;
96 $c_obj = new $obj_name();
98 if (empty ($c_action)) {
99 $c_action = "default";
102 $c_obj->_current_action = $c_action;
103 $args_array = array();
105 foreach ($args as $arg) {
106 $arg = preg_replace("/[^A-Za-z0-9_]/","",$arg);
107 //this is a workaround because call user func does funny things with passing args if they have no assigned value
108 if (empty($qarray[$arg])) {
109 //if argument is empty pass null as value and arg as assoc array key
110 $args_array[$arg] = null;
112 else {
113 $args_array[$arg] = $qarray[$arg];
117 $output = "";
118 //print_r($args_array);
119 if ($_POST['process'] == "true") {
121 if (is_callable(array(&$c_obj,$c_action . "_action_process"))) {
122 //echo "ca: " . $c_action . "_action_process";
123 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action_process"),$args_array);
124 if ($c_obj->_state == false) {
125 return $output;
128 //echo "ca: " . $c_action . "_action";
129 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
132 else {
133 if (is_callable(array(&$c_obj,$c_action . "_action"))) {
134 //echo "ca: " . $c_action . "_action";
135 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
137 else {
138 echo "The action trying to be performed: " . $c_action ." does not exist controller: ". $name;
143 return $output;
146 function _link($action = "default",$inlining = false) {
147 $url_parts = split("&",$_SERVER['REQUEST_URI']);
148 $link = array_shift($url_parts);
149 //print_r($url_parts);
151 if (strpos($url_parts[0],"=") === false) {
152 $inline_arg = $url_parts[0];
153 $url_parts[0] = $action;
155 else {
156 array_unshift($url_parts,$action);
158 if ($inlining) {
159 $link .= "&" . $inline_arg;
160 $link .= "&action=" . $url_parts[0];
162 else {
163 $link .= "&" . $url_parts[0];
166 foreach ($this->_args as $arg_name => $arg) {
167 $link .= "&" . $arg_name . "=" . $arg;
169 $link .= "&";
170 return $link;