some portal work
[openemr.git] / library / classes / Controller.class.php
blobe25139dc0636aedbebd86769f9b3523f3fb3f208
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['OE_SITE_DIR'] . '/documents/smarty/main';
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></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('GLOBALS', $GLOBALS);
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 call_user_func_array(array(&$obj,$func), array($var, $_POST));
60 return true;
63 function function_argument_error()
65 $this->display($GLOBALS['template_dir'] . "error/" . $this->template_mod . "_function_argument.html");
66 exit;
69 function i_once($file)
71 return include_once($file);
74 function act($qarray)
77 if (isset($_GET['process'])) {
78 unset($_GET['process']);
79 unset($qarray['process']);
80 $_POST['process'] = "true";
83 $args = array_reverse(array_keys($qarray));
84 $c_name = preg_replace("/[^A-Za-z0-9_]/", "", array_pop($args));
85 $parts = explode("_", $c_name);
86 $name = "";
88 foreach ($parts as $p) {
89 $name .= ucfirst($p);
92 $c_name = $name;
93 $c_action = preg_replace("/[^A-Za-z0-9_]/", "", array_pop($args));
94 $args = array_reverse($args);
96 if (!call_user_func(array("Controller","i_once"), $GLOBALS['fileroot'] ."/controllers/C_" . $c_name . ".class.php")) {
97 echo "Unable to load controller $name\n, please check the first argument supplied in the URL and try again";
98 exit;
101 $obj_name = "C_" . $c_name;
102 $c_obj = new $obj_name();
104 if (empty($c_action)) {
105 $c_action = "default";
108 $c_obj->_current_action = $c_action;
109 $args_array = array();
111 foreach ($args as $arg) {
112 $arg = preg_replace("/[^A-Za-z0-9_]/", "", $arg);
113 //this is a workaround because call user func does funny things with passing args if they have no assigned value
114 //2013-02-10 EMR Direct: workaround modified since "0" is also considered empty;
115 if (empty($qarray[$arg]) && $qarray[$arg]!="0") {
116 //if argument is empty pass null as value and arg as assoc array key
117 $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")) {
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;
134 //echo "ca: " . $c_action . "_action";
135 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"), $args_array);
136 } else {
137 if (is_callable(array(&$c_obj,$c_action . "_action"))) {
138 //echo "ca: " . $c_action . "_action";
139 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"), $args_array);
140 } else {
141 echo "The action trying to be performed: " . $c_action ." does not exist controller: ". $name;
146 return $output;
149 function _link($action = "default", $inlining = false)
151 $url_parts = explode("&", $_SERVER['REQUEST_URI']);
152 $link = array_shift($url_parts);
153 //print_r($url_parts);
155 if (strpos($url_parts[0], "=") === false) {
156 $inline_arg = $url_parts[0];
157 $url_parts[0] = $action;
158 } else {
159 array_unshift($url_parts, $action);
162 if ($inlining) {
163 $link .= "&" . urlencode($inline_arg);
164 $link .= "&action=" . urlencode($url_parts[0]);
165 } else {
166 $link .= "&" . urlencode($url_parts[0]);
169 foreach ($this->_args as $arg_name => $arg) {
170 $link .= "&" . urlencode($arg_name) . "=" . urlencode($arg);
173 $link .= "&";
174 return $link;