another minor fix to prior commit
[openemr.git] / library / classes / Controller.class.php
blob69735683191af1a18c2ba56b4c5b27053c7569cf
1 <?php
3 require_once(dirname(__FILE__) . "/../formdata.inc.php");
6 class Controller extends Smarty {
8 var $_current_action;
9 var $_state;
10 var $_args = array();
12 function __construct() {
13 parent::__construct();
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->plugins_dir = array(dirname(__FILE__) . "/../smarty/plugins", $GLOBALS['vendor_dir'] . "/smarty/smarty/libs/plugins");
20 $this->assign("PROCESS", "true");
21 $this->assign("HEADER", "<html><head><?php html_header_show();?></head><body>");
22 $this->assign("FOOTER", "</body></html>");
23 $this->assign("CONTROLLER", "controller.php?");
24 $this->assign("CONTROLLER_THIS", "controller.php?" . $_SERVER['QUERY_STRING']);
25 $this->assign("WEBROOT", $GLOBALS['webroot']);
28 function set_current_action($action) {
29 $this->_current_action = $action;
32 function default_action() {
33 echo "<html><body></body></html>";
36 function process_action() {
37 $this->default_action();
40 function populate_object(&$obj) {
41 if(!is_object($obj)) {
42 $this->function_argument_error();
45 foreach($_POST as $varname => $var) {
46 $varname = preg_replace("/[^A-Za-z0-9_]/","",$varname);
47 $func = "set_" . $varname;
48 if ( (!(strpos("_",$varname) === 0)) && is_callable(array($obj,$func)) ) {
49 //echo "c: $func on w: " . $var . "<br />";
51 //modified 01-2010 by BGM to centralize to formdata.inc.php
52 // have place several debug statements to allow standardized testing over next several months
53 if (!is_array($var)) {
54 //DEBUG LINE - error_log("Controller populate before strip: ".$var, 0);
55 $var = strip_escape_custom($var);
56 //DEBUG LINE - error_log("Controller populate after strip: ".$var, 0);
59 call_user_func_array(array(&$obj,$func),array($var, $_POST));
63 return true;
66 function function_argument_error() {
67 $this->display($GLOBALS['template_dir'] . "error/" . $this->template_mod . "_function_argument.html");
68 exit;
71 function i_once($file) {
72 return include_once($file);
75 function act($qarray) {
77 if (isset($_GET['process'])){
78 unset($_GET['process']);
79 unset($qarray['process']);
80 $_POST['process'] = "true";
82 $args = array_reverse(array_keys($qarray));
83 $c_name = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
84 $parts = explode("_",$c_name);
85 $name = "";
87 foreach($parts as $p) {
88 $name .= ucfirst($p);
91 $c_name = $name;
92 $c_action = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
93 $args = array_reverse($args);
95 if(!call_user_func(array(Controller,"i_once"),$GLOBALS['fileroot'] ."/controllers/C_" . $c_name . ".class.php")) {
96 echo "Unable to load controller $name\n, please check the first argument supplied in the URL and try again";
97 exit;
100 $obj_name = "C_" . $c_name;
101 $c_obj = new $obj_name();
103 if (empty ($c_action)) {
104 $c_action = "default";
107 $c_obj->_current_action = $c_action;
108 $args_array = array();
110 foreach ($args as $arg) {
111 $arg = preg_replace("/[^A-Za-z0-9_]/","",$arg);
112 //this is a workaround because call user func does funny things with passing args if they have no assigned value
113 //2013-02-10 EMR Direct: workaround modified since "0" is also considered empty;
114 if (empty($qarray[$arg]) && $qarray[$arg]!="0") {
115 //if argument is empty pass null as value and arg as assoc array key
116 $args_array[$arg] = null;
118 else {
119 $args_array[$arg] = $qarray[$arg];
123 $output = "";
124 //print_r($args_array);
125 if (isset($_POST['process']) && ($_POST['process']== "true")) {
127 if (is_callable(array(&$c_obj,$c_action . "_action_process"))) {
128 //echo "ca: " . $c_action . "_action_process";
129 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action_process"),$args_array);
130 if ($c_obj->_state == false) {
131 return $output;
134 //echo "ca: " . $c_action . "_action";
135 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
138 else {
139 if (is_callable(array(&$c_obj,$c_action . "_action"))) {
140 //echo "ca: " . $c_action . "_action";
141 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
143 else {
144 echo "The action trying to be performed: " . $c_action ." does not exist controller: ". $name;
149 return $output;
152 function _link($action = "default",$inlining = false) {
153 $url_parts = explode("&",$_SERVER['REQUEST_URI']);
154 $link = array_shift($url_parts);
155 //print_r($url_parts);
157 if (strpos($url_parts[0],"=") === false) {
158 $inline_arg = $url_parts[0];
159 $url_parts[0] = $action;
161 else {
162 array_unshift($url_parts,$action);
164 if ($inlining) {
165 $link .= "&" . $inline_arg;
166 $link .= "&action=" . $url_parts[0];
168 else {
169 $link .= "&" . $url_parts[0];
172 foreach ($this->_args as $arg_name => $arg) {
173 $link .= "&" . $arg_name . "=" . $arg;
175 $link .= "&";
176 return $link;