psr12 fixes for new PHP_CodeSniffer (#4795)
[openemr.git] / src / Menu / MainMenuRole.php
blob52e0392efd9ef8d6dbb5344712c6b6bd5f89d771
1 <?php
3 /**
4 * MainMenuRole class.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 namespace OpenEMR\Menu;
15 use OpenEMR\Services\UserService;
16 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18 class MainMenuRole extends MenuRole
20 /**
21 * @var EventDispatcher
23 private $dispatcher;
25 /**
26 * Constructor
28 public function __construct(EventDispatcherInterface $dispatcher)
30 // This is where the magic happens to support special menu items.
31 // An empty menu_update_map array is created in MenuRole class
32 // constructor. Adding to this array will link special menu items
33 // to functions in this class.
34 parent::__construct();
35 $this->menu_update_map["Visit Forms"] = "updateVisitForms";
36 $this->menu_update_map["Blank Forms"] = "updateBlankForms";
37 $this->dispatcher = $dispatcher;
40 /**
41 * Collect the Menu for logged in user.
43 * @return array representation of the Menu
45 public function getMenu()
47 // Collect the selected menu of user
48 $mainMenuRole = $this->getMenuRole();
50 // Load the selected menu
51 if (preg_match("/.json$/", $mainMenuRole)) {
52 // load custom menu (includes .json in id)
53 $menu_parsed = json_decode(file_get_contents($GLOBALS['OE_SITE_DIR'] . "/documents/custom_menus/" . $mainMenuRole));
54 } else {
55 // load a standardized menu (does not include .json in id)
56 $menu_parsed = json_decode(file_get_contents($GLOBALS['fileroot'] . "/interface/main/tabs/menu/menus/" . $mainMenuRole . ".json"));
59 // if error, then die and report error
60 if (!$menu_parsed) {
61 die("\nJSON ERROR: " . json_last_error());
64 $this->menuUpdateEntries($menu_parsed);
65 $updatedMenuEvent = $this->dispatcher->dispatch(MenuEvent::MENU_UPDATE, new MenuEvent($menu_parsed));
67 $menu_restrictions = array();
68 $tmp = $updatedMenuEvent->getMenu();
69 $this->menuApplyRestrictions($tmp, $menu_restrictions);
70 $updatedRestrictions = $this->dispatcher->dispatch(MenuEvent::MENU_RESTRICT, new MenuEvent($menu_restrictions));
72 return $updatedRestrictions->getMenu();
75 /**
76 * Build the html select element to list the MainMenuRole options.
78 * @var string $selected Current MainMenuRole for current users.
79 * @return string Html select element to list the MainMenuRole options.
81 public function displayMenuRoleSelector($selected = "")
83 $output = "<select name='main_menu_role' id='main_menu_role' class='form-control'>";
84 $output .= "<option value='standard' " . (($selected == "standard") ? "selected" : "") . ">" . xlt("Standard") . "</option>";
85 $output .= "<option value='answering_service' " . (($selected == "answering_service") ? "selected" : "") . ">" . xlt("Answering Service") . "</option>";
86 $output .= "<option value='front_office' " . (($selected == "front_office") ? "selected" : "") . ">" . xlt("Front Office") . "</option>";
87 $customMenuDir = $GLOBALS['OE_SITE_DIR'] . "/documents/custom_menus";
88 if (file_exists($customMenuDir)) {
89 $dHandle = opendir($customMenuDir);
90 while (false !== ($menuCustom = readdir($dHandle))) {
91 // Only process files that contain *.json
92 if (preg_match("/.json$/", $menuCustom)) {
93 $selectedTag = ($selected == $menuCustom) ? "selected" : "";
94 $output .= "<option value='" . attr($menuCustom) . "' " . $selectedTag . ">";
95 // Drop the .json and translate the name
96 $output .= xlt(substr($menuCustom, 0, -5));
97 $output .= "</option>";
101 closedir($dHandle);
104 $output .= "</select>";
105 return $output;
109 * Collect the MainMenuRole for logged in user.
111 * @return string Identifier for the MainMenuRole
113 private function getMenuRole()
115 $userService = new UserService();
116 $user = $userService->getCurrentlyLoggedInUser();
117 $mainMenuRole = $user['main_menu_role'];
118 if (empty($mainMenuRole)) {
119 $mainMenuRole = "standard";
122 return $mainMenuRole;
125 // This creates menu entries for all encounter forms.
127 protected function updateVisitForms(&$menu_list)
129 $menu_list->children = array();
130 $reglastcat = '';
131 $regrows = getFormsByCategory('1', false);
132 foreach ($regrows as $entry) {
133 $option_id = $entry['directory'];
134 $title = trim($entry['nickname']);
135 if (empty($title)) {
136 $title = $entry['name'];
138 if ($entry['category'] != $reglastcat) {
139 // New category. Close out the previous one if it exists.
140 if ($reglastcat) {
141 array_push($menu_list->children, $catEntry);
143 // Create the new category's object.
144 $reglastcat = $entry['category'];
145 $catEntry = new \stdClass();
146 $catEntry->label = xl_form_title($reglastcat);
147 $catEntry->icon = 'fa-caret-right';
148 $catEntry->requirement = 2;
149 $catEntry->children = array();
151 // Create object for form menu item and put it in its category object.
152 $formEntry = new \stdClass();
153 $formEntry->label = xl_form_title($title);
154 $formEntry->url = '/interface/patient_file/encounter/load_form.php?formname=' . urlencode($option_id);
155 $formEntry->requirement = 2;
156 $formEntry->target = 'enc';
157 // Plug in ACO attribute, if any, of this form.
158 if (!empty($entry['aco_spec'])) {
159 $tmp = explode('|', $entry['aco_spec']);
160 if (!empty($tmp[1])) {
161 $formEntry->acl_req = array($tmp[0], $tmp[1], 'write', 'addonly');
164 if (!empty($catEntry->children)) {
165 array_push($catEntry->children, $formEntry);
168 // Close out last category.
169 if ($reglastcat) {
170 array_push($menu_list->children, $catEntry);
174 // This creates LBF menu entries for Reports -> Blank Forms,
175 // within form categories. Core items are already there.
176 // Because these are blank forms there are no access restrictions.
178 protected function updateBlankForms(&$menu_list)
180 // Generate the Blank Form items for visit forms, both traditional and LBF.
181 $reglastcat = '';
182 $regrows = getFormsByCategory('1', true);
183 foreach ($regrows as $entry) {
184 $option_id = $entry['directory'];
185 $title = trim($entry['nickname']);
186 if (empty($title)) {
187 $title = $entry['name'];
189 if ($entry['category'] != $reglastcat) {
190 // New category. Close out the previous one if it exists.
191 if ($reglastcat) {
192 array_push($menu_list->children, $catEntry);
194 // Create the new category's object.
195 $reglastcat = $entry['category'];
196 $catEntry = new \stdClass();
197 $catEntry->label = xl_form_title($reglastcat);
198 $catEntry->icon = 'fa-caret-right';
199 $catEntry->requirement = 0;
200 $catEntry->children = array();
202 // Create object for form menu item and put it in its category object.
203 $formEntry = new \stdClass();
204 $formEntry->label = xl_form_title($title);
205 $formEntry->url = '/interface/forms/LBF/printable.php?isform=1&formname=' . urlencode($option_id);
206 $formEntry->requirement = 0;
207 $formEntry->target = 'pop';
208 array_push($catEntry->children, $formEntry);
210 // Close out last category.
211 if ($reglastcat) {
212 array_push($menu_list->children, $catEntry);