Highway to PSR2
[openemr.git] / library / classes / Controller.class.php
blob9cf6b4285d063138cb99beab9b47f0475274d341
1 <?php
5 class Controller extends Smarty
8 var $_current_action;
9 var $_state;
10 var $_args = array();
12 function __construct()
14 parent::__construct();
15 $this->template_mod = "general";
16 $this->_current_action = "";
17 $this->_state = true;
18 $this->compile_dir = $GLOBALS['fileroot'] . "/interface/main/calendar/modules/PostCalendar/pntemplates/compiled";
19 $this->compile_check = true;
20 $this->plugins_dir = array(dirname(__FILE__) . "/../smarty/plugins", $GLOBALS['vendor_dir'] . "/smarty/smarty/libs/plugins");
21 $this->assign("PROCESS", "true");
22 $this->assign("HEADER", "<html><head><?php html_header_show();?></head><body>");
23 $this->assign("FOOTER", "</body></html>");
24 $this->assign("CONTROLLER", "controller.php?");
25 $this->assign("CONTROLLER_THIS", "controller.php?" . $_SERVER['QUERY_STRING']);
26 $this->assign("WEBROOT", $GLOBALS['webroot']);
29 function set_current_action($action)
31 $this->_current_action = $action;
34 function default_action()
36 echo "<html><body></body></html>";
39 function process_action()
41 $this->default_action();
44 function populate_object(&$obj)
46 if (!is_object($obj)) {
47 $this->function_argument_error();
50 foreach ($_POST as $varname => $var) {
51 $varname = preg_replace("/[^A-Za-z0-9_]/", "", $varname);
52 $func = "set_" . $varname;
53 if ((!(strpos("_", $varname) === 0)) && is_callable(array($obj,$func))) {
54 //echo "c: $func on w: " . $var . "<br />";
56 //modified 01-2010 by BGM to centralize to formdata.inc.php
57 // have place several debug statements to allow standardized testing over next several months
58 if (!is_array($var)) {
59 //DEBUG LINE - error_log("Controller populate before strip: ".$var, 0);
60 $var = strip_escape_custom($var);
61 //DEBUG LINE - error_log("Controller populate after strip: ".$var, 0);
64 call_user_func_array(array(&$obj,$func), array($var, $_POST));
68 return true;
71 function function_argument_error()
73 $this->display($GLOBALS['template_dir'] . "error/" . $this->template_mod . "_function_argument.html");
74 exit;
77 function i_once($file)
79 return include_once($file);
82 function act($qarray)
85 if (isset($_GET['process'])) {
86 unset($_GET['process']);
87 unset($qarray['process']);
88 $_POST['process'] = "true";
91 $args = array_reverse(array_keys($qarray));
92 $c_name = preg_replace("/[^A-Za-z0-9_]/", "", array_pop($args));
93 $parts = explode("_", $c_name);
94 $name = "";
96 foreach ($parts as $p) {
97 $name .= ucfirst($p);
100 $c_name = $name;
101 $c_action = preg_replace("/[^A-Za-z0-9_]/", "", array_pop($args));
102 $args = array_reverse($args);
104 if (!call_user_func(array("Controller","i_once"), $GLOBALS['fileroot'] ."/controllers/C_" . $c_name . ".class.php")) {
105 echo "Unable to load controller $name\n, please check the first argument supplied in the URL and try again";
106 exit;
109 $obj_name = "C_" . $c_name;
110 $c_obj = new $obj_name();
112 if (empty($c_action)) {
113 $c_action = "default";
116 $c_obj->_current_action = $c_action;
117 $args_array = array();
119 foreach ($args as $arg) {
120 $arg = preg_replace("/[^A-Za-z0-9_]/", "", $arg);
121 //this is a workaround because call user func does funny things with passing args if they have no assigned value
122 //2013-02-10 EMR Direct: workaround modified since "0" is also considered empty;
123 if (empty($qarray[$arg]) && $qarray[$arg]!="0") {
124 //if argument is empty pass null as value and arg as assoc array key
125 $args_array[$arg] = null;
126 } else {
127 $args_array[$arg] = $qarray[$arg];
131 $output = "";
132 //print_r($args_array);
133 if (isset($_POST['process']) && ($_POST['process']== "true")) {
134 if (is_callable(array(&$c_obj,$c_action . "_action_process"))) {
135 //echo "ca: " . $c_action . "_action_process";
136 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action_process"), $args_array);
137 if ($c_obj->_state == false) {
138 return $output;
142 //echo "ca: " . $c_action . "_action";
143 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"), $args_array);
144 } else {
145 if (is_callable(array(&$c_obj,$c_action . "_action"))) {
146 //echo "ca: " . $c_action . "_action";
147 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"), $args_array);
148 } else {
149 echo "The action trying to be performed: " . $c_action ." does not exist controller: ". $name;
154 return $output;
157 function _link($action = "default", $inlining = false)
159 $url_parts = explode("&", $_SERVER['REQUEST_URI']);
160 $link = array_shift($url_parts);
161 //print_r($url_parts);
163 if (strpos($url_parts[0], "=") === false) {
164 $inline_arg = $url_parts[0];
165 $url_parts[0] = $action;
166 } else {
167 array_unshift($url_parts, $action);
170 if ($inlining) {
171 $link .= "&" . $inline_arg;
172 $link .= "&action=" . $url_parts[0];
173 } else {
174 $link .= "&" . $url_parts[0];
177 foreach ($this->_args as $arg_name => $arg) {
178 $link .= "&" . $arg_name . "=" . $arg;
181 $link .= "&";
182 return $link;