feat: support loop 2420E (#7405)
[openemr.git] / library / classes / Controller.class.php
blobad452a64af0e141fd34a10568cdf4f669870856b
1 <?php
3 use OpenEMR\Common\Acl\AclMain;
4 use OpenEMR\Common\Twig\TwigContainer;
6 // TODO: @adunsulag move these into src/
7 class Controller extends Smarty
9 public $template_mod;
10 public $_current_action;
11 public $_state;
12 public $_args = array();
13 protected $form = null;
15 public function __construct()
17 parent::__construct();
18 $this->template_mod = "general";
19 $this->_current_action = "";
20 $this->_state = true;
21 $this->setCompileDir($GLOBALS['OE_SITE_DIR'] . '/documents/smarty/main');
22 $this->setCompileCheck(true);
23 $this->setPluginsDir([__DIR__ . "/../smarty/plugins", $GLOBALS['vendor_dir'] . "/smarty/smarty/libs/plugins"]);
24 $this->assign("PROCESS", "true");
25 $this->assign("HEADER", "<html><head></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('GLOBALS', $GLOBALS);
32 public function set_current_action($action)
34 $this->_current_action = $action;
37 public function default_action()
39 echo "<html><body></body></html>";
42 public function process_action()
44 $this->default_action();
47 public function populate_object(&$obj)
49 if (!is_object($obj)) {
50 $this->function_argument_error();
53 foreach ($_POST as $varname => $var) {
54 $varname = preg_replace("/[^A-Za-z0-9_]/", "", $varname);
55 $func = "set_" . $varname;
56 if ((!(str_starts_with("_", $varname))) && is_callable(array($obj,$func))) {
57 //echo "c: $func on w: " . $var . "<br />";
59 $obj->$func($var, $_POST);
63 return true;
66 public function function_argument_error()
68 $this->display($GLOBALS['template_dir'] . "error/" . $this->template_mod . "_function_argument.html");
69 exit;
72 public function i_once($file)
74 return include_once($file);
77 public function act($qarray)
79 if ((array_key_first($qarray) ?? '') == 'practice_settings') {
80 if (!AclMain::aclCheckCore('admin', 'practice')) {
81 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Practice Settings")]);
82 exit;
86 if ((array_key_first($qarray) ?? '') == 'prescription') {
87 if (!AclMain::aclCheckCore('patients', 'rx')) {
88 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Prescriptions")]);
89 exit;
93 if (isset($_GET['process'])) {
94 unset($_GET['process']);
95 unset($qarray['process']);
96 $_POST['process'] = "true";
99 $args = array_reverse(array_keys($qarray));
100 $c_name = preg_replace("/[^A-Za-z0-9_]/", "", array_pop($args));
101 $parts = explode("_", $c_name);
102 $name = "";
104 foreach ($parts as $p) {
105 $name .= ucfirst($p);
108 $c_name = $name;
109 $c_action = preg_replace("/[^A-Za-z0-9_]/", "", array_pop($args));
110 $args = array_reverse($args);
112 if (!$this->i_once($GLOBALS['fileroot'] . "/controllers/C_" . $c_name . ".class.php")) {
113 echo "Unable to load controller $name\n, please check the first argument supplied in the URL and try again";
114 exit;
117 $obj_name = "C_" . $c_name;
118 $c_obj = new $obj_name();
120 if (empty($c_action)) {
121 $c_action = "default";
124 $c_obj->_current_action = $c_action;
125 $args_array = array();
127 foreach ($args as $arg) {
128 $arg = preg_replace("/[^A-Za-z0-9_]/", "", $arg);
129 //this is a workaround because call user func does funny things with passing args if they have no assigned value
130 //2013-02-10 EMR Direct: workaround modified since "0" is also considered empty;
131 if (empty($qarray[$arg]) && $qarray[$arg] != "0") {
132 //if argument is empty pass null as value
133 $args_array[] = null;
134 } else {
135 $args_array[] = $qarray[$arg];
139 $output = "";
140 //print_r($args_array);
141 // can no longer rely on is_callable since smarty 4 invokes a __call function deep within
142 // its classes, thus is_callable() is always true. so need to do both the is_callable
143 // and a method_exists() check.
144 if (isset($_POST['process']) && ($_POST['process'] == "true")) {
145 if (is_callable(array(&$c_obj, $c_action . "_action_process")) && method_exists($c_obj, $c_action . "_action_process")) {
146 //echo "ca: " . $c_action . "_action_process";
147 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action_process"), $args_array);
148 if ($c_obj->_state == false) {
149 return $output;
153 //echo "ca: " . $c_action . "_action";
154 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"), $args_array);
155 } elseif (is_callable(array(&$c_obj, $c_action . "_action")) && method_exists($c_obj, $c_action . "_action")) {
156 //echo "ca: " . $c_action . "_action";
157 $output .= call_user_func_array(array(&$c_obj,$c_action . "_action"), $args_array);
158 } else {
159 echo "The action trying to be performed: " . $c_action . " does not exist controller: " . $name;
163 return $output;
166 public function _link($action = "default", $inlining = false)
168 $url_parts = explode("&", $_SERVER['REQUEST_URI']);
169 $link = array_shift($url_parts);
170 //print_r($url_parts);
172 if (strpos($url_parts[0], "=") === false) {
173 $inline_arg = $url_parts[0];
174 $url_parts[0] = $action;
175 } else {
176 array_unshift($url_parts, $action);
179 if ($inlining) {
180 $link .= "&" . urlencode($inline_arg);
181 $link .= "&action=" . urlencode($url_parts[0]);
182 } else {
183 $link .= "&" . urlencode($url_parts[0]);
186 foreach ($this->_args as $arg_name => $arg) {
187 $link .= "&" . urlencode($arg_name) . "=" . urlencode($arg);
190 $link .= "&";
191 return $link;