migrated ubiquitous libraries to composer autoloader (#421)
[openemr.git] / library / classes / Controller.class.php
blob8a76518bf7203f59d3a6c1c1fa66ebe061417069
1 <?php
5 class Controller extends Smarty {
7 var $_current_action;
8 var $_state;
9 var $_args = array();
11 function __construct() {
12 parent::__construct();
13 $this->template_mod = "general";
14 $this->_current_action = "";
15 $this->_state = true;
16 $this->compile_dir = $GLOBALS['fileroot'] . "/interface/main/calendar/modules/PostCalendar/pntemplates/compiled";
17 $this->compile_check = true;
18 $this->plugins_dir = array(dirname(__FILE__) . "/../smarty/plugins", $GLOBALS['vendor_dir'] . "/smarty/smarty/libs/plugins");
19 $this->assign("PROCESS", "true");
20 $this->assign("HEADER", "<html><head><?php html_header_show();?></head><body>");
21 $this->assign("FOOTER", "</body></html>");
22 $this->assign("CONTROLLER", "controller.php?");
23 $this->assign("CONTROLLER_THIS", "controller.php?" . $_SERVER['QUERY_STRING']);
24 $this->assign("WEBROOT", $GLOBALS['webroot']);
27 function set_current_action($action) {
28 $this->_current_action = $action;
31 function default_action() {
32 echo "<html><body></body></html>";
35 function process_action() {
36 $this->default_action();
39 function populate_object(&$obj) {
40 if(!is_object($obj)) {
41 $this->function_argument_error();
44 foreach($_POST as $varname => $var) {
45 $varname = preg_replace("/[^A-Za-z0-9_]/","",$varname);
46 $func = "set_" . $varname;
47 if ( (!(strpos("_",$varname) === 0)) && is_callable(array($obj,$func)) ) {
48 //echo "c: $func on w: " . $var . "<br />";
50 //modified 01-2010 by BGM to centralize to formdata.inc.php
51 // have place several debug statements to allow standardized testing over next several months
52 if (!is_array($var)) {
53 //DEBUG LINE - error_log("Controller populate before strip: ".$var, 0);
54 $var = strip_escape_custom($var);
55 //DEBUG LINE - error_log("Controller populate after strip: ".$var, 0);
58 call_user_func_array(array(&$obj,$func),array($var, $_POST));
62 return true;
65 function function_argument_error() {
66 $this->display($GLOBALS['template_dir'] . "error/" . $this->template_mod . "_function_argument.html");
67 exit;
70 function i_once($file) {
71 return include_once($file);
74 function act($qarray) {
76 if (isset($_GET['process'])){
77 unset($_GET['process']);
78 unset($qarray['process']);
79 $_POST['process'] = "true";
81 $args = array_reverse(array_keys($qarray));
82 $c_name = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
83 $parts = explode("_",$c_name);
84 $name = "";
86 foreach($parts as $p) {
87 $name .= ucfirst($p);
90 $c_name = $name;
91 $c_action = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
92 $args = array_reverse($args);
94 if(!call_user_func(array("Controller","i_once"),$GLOBALS['fileroot'] ."/controllers/C_" . $c_name . ".class.php")) {
95 echo "Unable to load controller $name\n, please check the first argument supplied in the URL and try again";
96 exit;
99 $obj_name = "C_" . $c_name;
100 $c_obj = new $obj_name();
102 if (empty ($c_action)) {
103 $c_action = "default";
106 $c_obj->_current_action = $c_action;
107 $args_array = array();
109 foreach ($args as $arg) {
110 $arg = preg_replace("/[^A-Za-z0-9_]/","",$arg);
111 //this is a workaround because call user func does funny things with passing args if they have no assigned value
112 //2013-02-10 EMR Direct: workaround modified since "0" is also considered empty;
113 if (empty($qarray[$arg]) && $qarray[$arg]!="0") {
114 //if argument is empty pass null as value and arg as assoc array key
115 $args_array[$arg] = null;
117 else {
118 $args_array[$arg] = $qarray[$arg];
122 $output = "";
123 //print_r($args_array);
124 if (isset($_POST['process']) && ($_POST['process']== "true")) {
126 if (is_callable(array(&$c_obj,$c_action . "_action_process"))) {
127 //echo "ca: " . $c_action . "_action_process";
128 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action_process"),$args_array);
129 if ($c_obj->_state == false) {
130 return $output;
133 //echo "ca: " . $c_action . "_action";
134 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
137 else {
138 if (is_callable(array(&$c_obj,$c_action . "_action"))) {
139 //echo "ca: " . $c_action . "_action";
140 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
142 else {
143 echo "The action trying to be performed: " . $c_action ." does not exist controller: ". $name;
148 return $output;
151 function _link($action = "default",$inlining = false) {
152 $url_parts = explode("&",$_SERVER['REQUEST_URI']);
153 $link = array_shift($url_parts);
154 //print_r($url_parts);
156 if (strpos($url_parts[0],"=") === false) {
157 $inline_arg = $url_parts[0];
158 $url_parts[0] = $action;
160 else {
161 array_unshift($url_parts,$action);
163 if ($inlining) {
164 $link .= "&" . $inline_arg;
165 $link .= "&action=" . $url_parts[0];
167 else {
168 $link .= "&" . $url_parts[0];
171 foreach ($this->_args as $arg_name => $arg) {
172 $link .= "&" . $arg_name . "=" . $arg;
174 $link .= "&";
175 return $link;