Translation project - clean up a duplicate constant
[openemr.git] / library / classes / Controller.class.php
blob27cddacd94f8971c5f7dbdb4d33a9ffa976d4672
1 <?php
3 require_once(dirname(__FILE__) . "/../Smarty.class.php");
4 require_once(dirname(__FILE__) . "/../formdata.inc.php");
5 if (!defined('SMARTY_DIR')) {
6 define("SMARTY_DIR", dirname(__FILE__) . "/../");
10 class Controller extends Smarty {
12 var $_current_action;
13 var $_state;
14 var $_args = array();
16 function Controller() {
17 parent::Smarty();
18 $this->template_mod = "general";
19 $this->_current_action = "";
20 $this->_state = true;
21 $this->compile_dir = $GLOBALS['fileroot'] . "/interface/main/calendar/modules/PostCalendar/pntemplates/compiled";
22 $this->compile_check = true;
23 $this->assign("PROCESS", "true");
24 $this->assign("HEADER", "<html><head>
25 <?php html_header_show();?></head><body>");
26 $this->assign("FOOTER", "</body></html>");
27 $this->assign("CONTROLLER", "controller.php?");
28 $this->assign("CONTROLLER_THIS", "controller.php?" . $_SERVER['QUERY_STRING']);
29 $this->assign("WEBROOT", $GLOBALS['webroot']);
32 function set_current_action($action) {
33 $this->_current_action = $action;
36 function default_action() {
37 echo "<html><body></body></html>";
40 function process_action() {
41 $this->default_action();
44 function populate_object(&$obj) {
45 if(!is_object($obj)) {
46 $this->function_argument_error();
49 foreach($_POST as $varname => $var) {
50 $varname = preg_replace("/[^A-Za-z0-9_]/","",$varname);
51 $func = "set_" . $varname;
52 if ( (!(strpos("_",$varname) === 0)) && is_callable(array($obj,$func)) ) {
53 //echo "c: $func on w: " . $var . "<br />";
55 //modified 01-2010 by BGM to centralize to formdata.inc.php
56 // have place several debug statements to allow standardized testing over next several months
57 if (!is_array($var)) {
58 //DEBUG LINE - error_log("Controller populate before strip: ".$var, 0);
59 $var = strip_escape_custom($var);
60 //DEBUG LINE - error_log("Controller populate after strip: ".$var, 0);
63 call_user_func_array(array(&$obj,$func),array($var, $_POST));
67 return true;
70 function function_argument_error() {
71 $this->display($GLOBALS['template_dir'] . "error/" . $this->template_mod . "_function_argument.html");
72 exit;
75 function i_once($file) {
76 return include_once($file);
79 function act($qarray) {
81 if (isset($_GET['process'])){
82 unset($_GET['process']);
83 unset($qarray['process']);
84 $_POST['process'] = "true";
86 $args = array_reverse(array_keys($qarray));
87 $c_name = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
88 $parts = explode("_",$c_name);
89 $name = "";
91 foreach($parts as $p) {
92 $name .= ucfirst($p);
95 $c_name = $name;
96 $c_action = preg_replace("/[^A-Za-z0-9_]/","",array_pop($args));
97 $args = array_reverse($args);
99 if(!@call_user_func(array(Controller,"i_once"),$GLOBALS['fileroot'] ."/controllers/C_" . $c_name . ".class.php")) {
100 echo "Unable to load controller $name\n, please check the first argument supplied in the URL and try again";
101 exit;
104 $obj_name = "C_" . $c_name;
105 $c_obj = new $obj_name();
107 if (empty ($c_action)) {
108 $c_action = "default";
111 $c_obj->_current_action = $c_action;
112 $args_array = array();
114 foreach ($args as $arg) {
115 $arg = preg_replace("/[^A-Za-z0-9_]/","",$arg);
116 //this is a workaround because call user func does funny things with passing args if they have no assigned value
117 //2013-02-10 EMR Direct: workaround modified since "0" is also considered empty;
118 if (empty($qarray[$arg]) && $qarray[$arg]!="0") {
119 //if argument is empty pass null as value and arg as assoc array key
120 $args_array[$arg] = null;
122 else {
123 $args_array[$arg] = $qarray[$arg];
127 $output = "";
128 //print_r($args_array);
129 if (isset($_POST['process']) && ($_POST['process']== "true")) {
131 if (is_callable(array(&$c_obj,$c_action . "_action_process"))) {
132 //echo "ca: " . $c_action . "_action_process";
133 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action_process"),$args_array);
134 if ($c_obj->_state == false) {
135 return $output;
138 //echo "ca: " . $c_action . "_action";
139 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
142 else {
143 if (is_callable(array(&$c_obj,$c_action . "_action"))) {
144 //echo "ca: " . $c_action . "_action";
145 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"),$args_array);
147 else {
148 echo "The action trying to be performed: " . $c_action ." does not exist controller: ". $name;
153 return $output;
156 function _link($action = "default",$inlining = false) {
157 $url_parts = explode("&",$_SERVER['REQUEST_URI']);
158 $link = array_shift($url_parts);
159 //print_r($url_parts);
161 if (strpos($url_parts[0],"=") === false) {
162 $inline_arg = $url_parts[0];
163 $url_parts[0] = $action;
165 else {
166 array_unshift($url_parts,$action);
168 if ($inlining) {
169 $link .= "&" . $inline_arg;
170 $link .= "&action=" . $url_parts[0];
172 else {
173 $link .= "&" . $url_parts[0];
176 foreach ($this->_args as $arg_name => $arg) {
177 $link .= "&" . $arg_name . "=" . $arg;
179 $link .= "&";
180 return $link;