dashboard.lib.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * DashboardManager can be used to manage dashboard
  5. * author Christian Fasanando <christian1827@gmail.com>.
  6. */
  7. class DashboardManager
  8. {
  9. /**
  10. * Constructor.
  11. */
  12. public function __construct()
  13. {
  14. }
  15. /**
  16. * This function allows easy activating and inactivating of dashboard plugins.
  17. */
  18. public static function handle_dashboard_plugins()
  19. {
  20. $token = Security::get_existing_token();
  21. $tokenCondition = '&sec_token='.$token;
  22. /* We scan the plugin directory. Each folder is a potential plugin. */
  23. $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/';
  24. $possiblePlugins = self::getPossibleDashboardPluginsPath();
  25. $table_cols = ['name', 'version', 'description'];
  26. echo Display::page_subheader(get_lang('Dashboard plugins'));
  27. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).$tokenCondition.'">';
  28. echo '<table class="table table-hover">';
  29. echo '<tr>';
  30. echo '<th width="50px">'.get_lang('Enabled').'</th>';
  31. echo '<th width="250px">'.get_lang('Name').'</th>';
  32. echo '<th width="100px">'.get_lang('Version').'</th>';
  33. echo '<th>'.get_lang('Description').'</th>';
  34. echo '</tr>';
  35. $disabled_blocks_data = self::get_block_data_without_plugin();
  36. // We display all the possible enabled or disabled plugins
  37. foreach ($possiblePlugins as $testplugin) {
  38. $plugin_info_file = $dashboard_pluginpath.$testplugin."/$testplugin.info";
  39. if (file_exists($plugin_info_file) && is_readable($plugin_info_file)) {
  40. $plugin_info = api_parse_info_file($plugin_info_file);
  41. // change index to lower case
  42. $plugin_info = array_change_key_case($plugin_info);
  43. echo '<tr>';
  44. self::display_dashboard_plugin_checkboxes($testplugin);
  45. for ($i = 0; $i < count($table_cols); $i++) {
  46. if (isset($plugin_info[strtolower($table_cols[$i])])) {
  47. echo '<td>';
  48. echo $plugin_info[$table_cols[$i]];
  49. echo '</td>';
  50. } else {
  51. echo '<td></td>';
  52. }
  53. }
  54. echo '</tr>';
  55. } else {
  56. if ($testplugin != 'css') {
  57. echo Display::tag(
  58. 'tr',
  59. Display::tag(
  60. 'td',
  61. get_lang('Check file permissions').' '.Security::remove_XSS($plugin_info_file),
  62. ['colspan' => '3']
  63. )
  64. );
  65. }
  66. }
  67. }
  68. // display all disabled block data
  69. if (count($disabled_blocks_data) > 0) {
  70. foreach ($disabled_blocks_data as $disabled_block) {
  71. echo '<tr style="background-color:#eee">';
  72. echo '<td><center><input type="checkbox" name="disabled_block" value="true" checked disabled /></center>';
  73. for ($j = 0; $j < count($table_cols); $j++) {
  74. if (isset($disabled_block[strtolower($table_cols[$j])])) {
  75. if ($j == 2) {
  76. echo '<td>';
  77. echo '<font color="#aaa">'.$disabled_block[$table_cols[$j]].'</font><br />';
  78. echo '<font color="red">'.get_lang('This plugin has been deleted from the dashboard plugin directory').'</font>';
  79. echo '</td>';
  80. } else {
  81. echo '<td>';
  82. echo '<font color="#aaa">'.$disabled_block[$table_cols[$j]].'</font>';
  83. echo '</td>';
  84. }
  85. } else {
  86. echo '<td>&nbsp;</td>';
  87. }
  88. }
  89. echo '</tr>';
  90. }
  91. }
  92. echo '</table>';
  93. echo '<br />';
  94. echo '<button class="btn btn-default" type="submit" name="submit_dashboard_plugins" value="'.get_lang('EnableDashboard plugins').'">'.
  95. get_lang('EnableDashboard plugins').'</button></form>';
  96. }
  97. /**
  98. * display checkboxes for dashboard plugin list.
  99. *
  100. * @param string $plugin_path
  101. */
  102. public static function display_dashboard_plugin_checkboxes($plugin_path)
  103. {
  104. $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK);
  105. $sql = "SELECT * FROM $tbl_block
  106. WHERE path = '".Database::escape_string($plugin_path)."' AND active = 1";
  107. $rs = Database::query($sql);
  108. $checked = '';
  109. if (Database::num_rows($rs) > 0) {
  110. $checked = "checked";
  111. }
  112. echo "<td align=\"center\">";
  113. echo '<input type="checkbox" name="'.$plugin_path.'" value="true" '.$checked.'/>';
  114. echo "</td>";
  115. }
  116. /**
  117. * This function allows easy activating and inactivating
  118. * of plugins and save them inside db.
  119. *
  120. * @param array $plugin_paths dashboard plugin paths
  121. * return int affected rows
  122. */
  123. public static function store_dashboard_plugins($plugin_paths)
  124. {
  125. $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK);
  126. $affected_rows = 0;
  127. // get all plugins path inside plugin directory
  128. $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/';
  129. $possiblePlugins = self::getPossibleDashboardPluginsPath();
  130. if (count($possiblePlugins) > 0) {
  131. $selected_plugins = array_intersect(array_keys($plugin_paths), $possiblePlugins);
  132. $not_selected_plugins = array_diff($possiblePlugins, array_keys($plugin_paths));
  133. // get blocks id from not selected path
  134. $not_selected_blocks_id = [];
  135. foreach ($not_selected_plugins as $plugin) {
  136. $block_data = self::get_enabled_dashboard_blocks($plugin);
  137. if (!empty($block_data[$plugin])) {
  138. $not_selected_blocks_id[] = $block_data[$plugin]['id'];
  139. }
  140. }
  141. /* clean not selected plugins for extra user data and block data */
  142. // clean from extra user data
  143. $field_variable = 'dashboard';
  144. $extra_user_data = UserManager::get_extra_user_data_by_field_variable($field_variable);
  145. if (!empty($extra_user_data) && count($extra_user_data) > 0) {
  146. foreach ($extra_user_data as $key => $user_data) {
  147. $user_id = $key;
  148. $user_block_data = self::get_user_block_data($user_id);
  149. $user_block_id = array_keys($user_block_data);
  150. // clean disabled block data
  151. foreach ($user_block_id as $block_id) {
  152. if (in_array($block_id, $not_selected_blocks_id)) {
  153. unset($user_block_data[$block_id]);
  154. }
  155. }
  156. // get columns and blocks id for updating extra user data
  157. $columns = [];
  158. $user_blocks_id = [];
  159. foreach ($user_block_data as $data) {
  160. $user_blocks_id[$data['block_id']] = true;
  161. $columns[$data['block_id']] = $data['column'];
  162. }
  163. // update extra user blocks data
  164. self::store_user_blocks($user_id, $user_blocks_id, $columns);
  165. }
  166. }
  167. // clean from block data
  168. if (!empty($not_selected_blocks_id)) {
  169. $sql_check = "SELECT id FROM $tbl_block
  170. WHERE id IN(".implode(',', $not_selected_blocks_id).")";
  171. $rs_check = Database::query($sql_check);
  172. if (Database::num_rows($rs_check) > 0) {
  173. $del = "DELETE FROM $tbl_block WHERE id IN(".implode(',', $not_selected_blocks_id).")";
  174. Database::query($del);
  175. }
  176. }
  177. // store selected plugins
  178. if (!empty($selected_plugins) && count($selected_plugins) > 0) {
  179. foreach ($selected_plugins as $testplugin) {
  180. $selected_path = Database::escape_string($testplugin);
  181. // check if the path already stored inside block table for updating or adding it
  182. $sql = "SELECT path FROM $tbl_block WHERE path = '$selected_path'";
  183. $rs = Database::query($sql);
  184. if (Database::num_rows($rs) > 0) {
  185. // update
  186. $upd = "UPDATE $tbl_block SET active = 1 WHERE path = '$selected_path'";
  187. $result = Database::query($upd);
  188. $affected_rows = Database::affected_rows($result);
  189. } else {
  190. // insert
  191. $plugin_info_file = $dashboard_pluginpath.$testplugin."/$testplugin.info";
  192. $plugin_info = [];
  193. if (file_exists($plugin_info_file)) {
  194. $plugin_info = api_parse_info_file($plugin_info_file);
  195. }
  196. // change keys to lower case
  197. $plugin_info = array_change_key_case($plugin_info);
  198. // setting variables
  199. $plugin_name = $testplugin;
  200. $plugin_description = '';
  201. $plugin_controller = '';
  202. $plugin_path = $testplugin;
  203. if (isset($plugin_info['name'])) {
  204. $plugin_name = Database::escape_string($plugin_info['name']);
  205. }
  206. if (isset($plugin_info['description'])) {
  207. $plugin_description = Database::escape_string($plugin_info['description']);
  208. }
  209. if (isset($plugin_info['controller'])) {
  210. $plugin_controller = Database::escape_string($plugin_info['controller']);
  211. }
  212. $ins = "INSERT INTO $tbl_block(name, description, path, controller, active)
  213. VALUES ('$plugin_name', '$plugin_description', '$plugin_path', '$plugin_controller', 1)";
  214. $result = Database::query($ins);
  215. $affected_rows = Database::affected_rows($result);
  216. }
  217. }
  218. }
  219. }
  220. return $affected_rows;
  221. }
  222. /**
  223. * Get all plugins path inside dashboard directory.
  224. *
  225. * @return array name plugins directories
  226. */
  227. public static function getPossibleDashboardPluginsPath()
  228. {
  229. // get all plugins path inside plugin directory
  230. /* We scan the plugin directory. Each folder is a potential plugin. */
  231. $possiblePlugins = [];
  232. $dashboard_pluginpath = api_get_path(SYS_PLUGIN_PATH).'dashboard/';
  233. $handle = @opendir($dashboard_pluginpath);
  234. while (false !== ($file = readdir($handle))) {
  235. if ($file != '.' && $file != '..' && is_dir($dashboard_pluginpath.$file)) {
  236. $possiblePlugins[] = $file;
  237. }
  238. }
  239. @closedir($handle);
  240. return $possiblePlugins;
  241. }
  242. /**
  243. * Get all blocks data without plugin directory.
  244. *
  245. * @return array Block data
  246. */
  247. public static function get_block_data_without_plugin()
  248. {
  249. $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK);
  250. $possiblePlugins = self::getPossibleDashboardPluginsPath();
  251. // We check if plugin exists inside directory for updating active field
  252. $sql = "SELECT * FROM $tbl_block";
  253. $rs = Database::query($sql);
  254. if (Database::num_rows($rs) > 0) {
  255. while ($row = Database::fetch_array($rs)) {
  256. if (!in_array($row['path'], $possiblePlugins)) {
  257. $active = 0;
  258. } else {
  259. $active = 1;
  260. }
  261. // update active
  262. $upd = "UPDATE $tbl_block SET active = '$active'
  263. WHERE path = '".$row['path']."'";
  264. Database::query($upd);
  265. }
  266. }
  267. // get disabled block data
  268. $block_data = [];
  269. $sql = "SELECT * FROM $tbl_block WHERE active = 0";
  270. $rs_block = Database::query($sql);
  271. if (Database::num_rows($rs_block) > 0) {
  272. while ($row_block = Database::fetch_array($rs_block)) {
  273. $block_data[] = $row_block;
  274. }
  275. }
  276. return $block_data;
  277. }
  278. /**
  279. * get data about enabled dashboard block (stored insise block table).
  280. *
  281. * @param string $path plugin path
  282. *
  283. * @return array data
  284. */
  285. public static function get_enabled_dashboard_blocks($path = '')
  286. {
  287. $tbl_block = Database::get_main_table(TABLE_MAIN_BLOCK);
  288. $condition_path = '';
  289. if (!empty($path)) {
  290. $path = Database::escape_string($path);
  291. $condition_path = ' AND path = "'.$path.'" ';
  292. }
  293. $sql = "SELECT * FROM $tbl_block WHERE active = 1 $condition_path ";
  294. $rs = Database::query($sql);
  295. $block_data = [];
  296. if (Database::num_rows($rs) > 0) {
  297. while ($row = Database::fetch_array($rs)) {
  298. $block_data[$row['path']] = $row;
  299. }
  300. }
  301. return $block_data;
  302. }
  303. /**
  304. * display user dashboard list.
  305. *
  306. * @param int User id
  307. */
  308. public static function display_user_dashboard_list($user_id)
  309. {
  310. $enabled_dashboard_plugins = self::get_enabled_dashboard_blocks();
  311. $user_block_data = self::get_user_block_data($user_id);
  312. $html = '';
  313. if (count($enabled_dashboard_plugins) > 0) {
  314. $html .= '<div style="margin-top:20px">';
  315. $html .= '<div><strong>'.get_lang('Select blocks to display in the dashboard blocks view').'</strong></div><br />';
  316. $html .= '<form name="dashboard_list" method="post" action="index.php?action=store_user_block">';
  317. $html .= '<table class="data_table">';
  318. $html .= '<tr>';
  319. $html .= '<th width="5%">';
  320. $html .= get_lang('Enabled');
  321. $html .= '</th>';
  322. $html .= '<th width="30%">';
  323. $html .= get_lang('Name');
  324. $html .= '</th>';
  325. $html .= '<th width="40%">';
  326. $html .= get_lang('Description');
  327. $html .= '</th>';
  328. $html .= '<th>';
  329. $html .= get_lang('Position (column)');
  330. $html .= '</th>';
  331. $html .= '</tr>';
  332. // We display all enabled plugins and the checkboxes
  333. foreach ($enabled_dashboard_plugins as $block) {
  334. $path = $block['path'];
  335. $controller_class = $block['controller'];
  336. $filename_controller = $path.'.class.php';
  337. $dashboard_plugin_path = api_get_path(SYS_PLUGIN_PATH).'dashboard/'.$path.'/';
  338. require_once $dashboard_plugin_path.$filename_controller;
  339. if (class_exists($controller_class)) {
  340. $obj_block = new $controller_class($user_id);
  341. // check if user is allowed to see the block
  342. if (method_exists($obj_block, 'is_block_visible_for_user')) {
  343. $is_block_visible_for_user = $obj_block->is_block_visible_for_user($user_id);
  344. if (!$is_block_visible_for_user) {
  345. continue;
  346. }
  347. }
  348. $html .= '<tr>';
  349. // checkboxes
  350. $html .= self::display_user_dashboard_list_checkboxes($user_id, $block['id']);
  351. $html .= '<td>'.$block['name'].'</td>';
  352. $html .= '<td>'.$block['description'].'</td>';
  353. $html .= '<td>
  354. <select class="selectpicker show-tick form-control" name="columns['.$block['id'].']">
  355. <option value="1" '.(isset($user_block_data[$block['id']]) && $user_block_data[$block['id']]['column'] == 1 ? 'selected' : '').' >1</option>
  356. <option value="2" '.(isset($user_block_data[$block['id']]) && $user_block_data[$block['id']]['column'] == 2 ? 'selected' : '').' >2</option>
  357. </select>
  358. </td>';
  359. $html .= '</tr>';
  360. } else {
  361. $html .= Display::tag('tr', Display::tag('td', get_lang('Error').' '.$controller_class, ['colspan' => '3']));
  362. }
  363. }
  364. $html .= '</table>';
  365. $html .= '<div class="row"><div class="col-md-12">';
  366. $html .= '<button class="btn btn-default" type="submit" name="submit_dashboard_list" value="'.get_lang('Enable dashboard block').'"><em class="fa fa-check-square"></em> '.
  367. get_lang('Enable dashboard block').'</button></form>';
  368. $html .= '</div></div>';
  369. } else {
  370. $html .= '<div style="margin-top:20px">'.get_lang('ThereAreNoEnabledDashboard plugins').'</div>';
  371. if (api_is_platform_admin()) {
  372. $html .= '<a class="btn btn-default" href="'.api_get_path(WEB_CODE_PATH).'admin/settings.php?category=Plugins">'.
  373. get_lang('Configure Dashboard Plugin').'</a>';
  374. }
  375. }
  376. return $html;
  377. }
  378. /**
  379. * display checkboxes for user dashboard list.
  380. *
  381. * @param int User id
  382. * @param int Block id
  383. */
  384. public static function display_user_dashboard_list_checkboxes($user_id, $block_id)
  385. {
  386. $user_id = intval($user_id);
  387. $user_block_data = self::get_user_block_data($user_id);
  388. $enabled_blocks_id = array_keys($user_block_data);
  389. $checked = '';
  390. if (in_array($block_id, $enabled_blocks_id)) {
  391. $checked = "checked";
  392. }
  393. $html = "<td align=\"center\">";
  394. $html .= '<input type="checkbox" name="enabled_blocks['.$block_id.']" value="true" '.$checked.'/>';
  395. $html .= "</td>";
  396. return $html;
  397. }
  398. /**
  399. * This function store enabled blocks id with its column position (block_id1:colum;block_id2:colum; ...)
  400. * inside extra user fields.
  401. *
  402. * @param int $user_id User id
  403. * @param array $enabled_blocks selected blocks
  404. * @param array $columns columns position
  405. *
  406. * @return bool
  407. */
  408. public static function store_user_blocks($user_id, $enabled_blocks, $columns)
  409. {
  410. $selected_blocks_id = [];
  411. if (is_array($enabled_blocks) && count($enabled_blocks) > 0) {
  412. $selected_blocks_id = array_keys($enabled_blocks);
  413. }
  414. // build data for storing inside extra user field
  415. $fname = 'dashboard';
  416. $fvalue = [];
  417. foreach ($selected_blocks_id as $block_id) {
  418. $fvalue[] = $block_id.':'.$columns[$block_id];
  419. }
  420. $upd_extra_field = UserManager::update_extra_field_value(
  421. $user_id,
  422. $fname,
  423. $fvalue
  424. );
  425. return $upd_extra_field;
  426. }
  427. /**
  428. * This function get user block data (block id with its number of column) from extra user data.
  429. *
  430. * @param int User id
  431. *
  432. * @return array data (block_id,column)
  433. */
  434. public static function get_user_block_data($user_id)
  435. {
  436. $user_id = intval($user_id);
  437. $field_variable = 'dashboard';
  438. $extra_user_data = UserManager::get_extra_user_data_by_field($user_id, $field_variable);
  439. if (!isset($extra_user_data[$field_variable])) {
  440. return [];
  441. }
  442. $extra_user_data = explode(';', $extra_user_data[$field_variable]);
  443. $data = [];
  444. foreach ($extra_user_data as $extra) {
  445. $split_extra = explode(':', $extra);
  446. if (!empty($split_extra)) {
  447. $block_id = $split_extra[0];
  448. $column = isset($split_extra[1]) ? $split_extra[1] : null;
  449. $data[$block_id] = ['block_id' => $block_id, 'column' => $column];
  450. }
  451. }
  452. return $data;
  453. }
  454. /**
  455. * This function update extra user blocks data after closing a dashboard block.
  456. *
  457. * @param int $user_id User id
  458. * @param string plugin path
  459. *
  460. * @return bool
  461. */
  462. public static function close_user_block($user_id, $path)
  463. {
  464. $enabled_dashboard_blocks = self::get_enabled_dashboard_blocks($path);
  465. $user_block_data = self::get_user_block_data($user_id);
  466. foreach ($enabled_dashboard_blocks as $enabled_block) {
  467. unset($user_block_data[$enabled_block['id']]);
  468. }
  469. // get columns and blocks id for updating extra user data
  470. $columns = [];
  471. $user_blocks_id = [];
  472. foreach ($user_block_data as $data) {
  473. $user_blocks_id[$data['block_id']] = true;
  474. $columns[$data['block_id']] = $data['column'];
  475. }
  476. // update extra user blocks data
  477. $upd_extra_field = self::store_user_blocks($user_id, $user_blocks_id, $columns);
  478. return $upd_extra_field;
  479. }
  480. /**
  481. * get links for styles from dashboard plugins.
  482. *
  483. * @return string links
  484. */
  485. public static function getStyleSheet()
  486. {
  487. return '<link rel="stylesheet" href="'.api_get_path(WEB_PLUGIN_PATH).'dashboard/css/default.css" type="text/css" />'.PHP_EOL;
  488. }
  489. }