PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/dashee/mcp.dashee.php

https://github.com/brandonkelly/dashEE
PHP | 467 lines | 278 code | 70 blank | 119 comment | 21 complexity | 162ab14e12aa6ecede5eac9148fc8265 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author ExpressionEngine Dev Team
  7. * @copyright Copyright (c) 2003 - 2011, EllisLab, Inc.
  8. * @license http://expressionengine.com/user_guide/license.html
  9. * @link http://expressionengine.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * dashEE Module Control Panel File
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Addons
  19. * @category Module
  20. * @author Chris Monnat
  21. * @link http://chrismonnat.com
  22. */
  23. class Dashee_mcp {
  24. public $return_data;
  25. private $_EE;
  26. private $_model;
  27. private $_base_qs;
  28. private $_base_url;
  29. private $_theme_url;
  30. private $_member_id;
  31. private $_settings;
  32. /**
  33. * Constructor
  34. */
  35. public function __construct()
  36. {
  37. $this->_EE =& get_instance();
  38. $this->_EE->load->model('dashee_model');
  39. $this->_model = $this->_EE->dashee_model;
  40. $this->_base_qs = 'C=addons_modules' .AMP .'M=show_module_cp' .AMP .'module=dashee';
  41. $this->_base_url = BASE .AMP .$this->_base_qs;
  42. $this->_theme_url = $this->_model->get_package_theme_url();
  43. $this->_member_id = $this->_EE->session->userdata('member_id');
  44. // get current members dash configuration for use throughout module
  45. $this->_get_member_settings($this->_member_id);
  46. }
  47. // ----------------------------------------------------------------
  48. /**
  49. * Index Function
  50. *
  51. * @return void
  52. */
  53. public function index()
  54. {
  55. $css = $this->_theme_url .'css/cp.css';
  56. $js = $this->_theme_url .'js/dashee.js';
  57. $this->_EE->cp->add_to_head('<link rel="stylesheet" type="text/css" href="'.$css.'" />');
  58. $this->_EE->cp->add_to_head('<script type="text/javascript" src="'.$js.'"></script>');
  59. $this->_EE->cp->set_variable('cp_page_title', lang('dashee_term'));
  60. $this->_EE->cp->set_right_nav(array('btn_collapse' => '#collapse', 'btn_expand' => '#expand', 'btn_widgets' => '#widgets'));
  61. // load widgets
  62. $widgets = $this->_widget_loader($this->_settings['widgets']);
  63. return $this->_EE->load->view('index', array('settings' => $this->_settings, 'content' => $widgets, 'theme_url' => $this->_theme_url), TRUE);
  64. }
  65. /**
  66. * AJAX METHOD
  67. * Get listing of all available widgets from installed modules.
  68. *
  69. * @return NULL
  70. */
  71. public function get_widget_listing()
  72. {
  73. $this->_EE->load->library('table');
  74. $map = directory_map(PATH_THIRD, 2);
  75. // Fetch installed modules.
  76. $installed_mods = $this->_model->get_installed_modules();
  77. // Determine which installed modules have widgets associated with them.
  78. $mods_with_widgets = array();
  79. foreach($map as $third_party => $jabber)
  80. {
  81. if(is_array($jabber))
  82. {
  83. if(in_array($third_party, $installed_mods) AND in_array('widgets', $jabber))
  84. {
  85. $mods_with_widgets[] = $third_party;
  86. }
  87. }
  88. }
  89. // Get array of all widgets of installed modules.
  90. $table_data = array();
  91. asort($mods_with_widgets);
  92. foreach($mods_with_widgets as $mod)
  93. {
  94. $path = PATH_THIRD.$mod.'/widgets/';
  95. $map = directory_map(PATH_THIRD.$mod.'/widgets', 1);
  96. if(is_array($map))
  97. {
  98. $col = 1;
  99. asort($map);
  100. foreach($map as $widget)
  101. {
  102. $this->_EE->lang->loadfile($mod);
  103. // check widget permissions before adding to table and skip if user doesn't have permission
  104. $obj = $this->_get_widget_object($mod, $widget);
  105. if(method_exists($obj, 'permissions') && !$obj->permissions())
  106. {
  107. continue;
  108. }
  109. $table_data[] = array(
  110. lang($this->_format_filename($widget).'_name'),
  111. lang($this->_format_filename($widget).'_description'),
  112. lang(strtolower($mod).'_module_name'),
  113. anchor($this->_base_url.AMP.'method=add_widget'.AMP.'mod='.$mod.AMP.'wgt='.$widget, 'Add')
  114. );
  115. }
  116. }
  117. }
  118. echo $this->_EE->load->view('widgets_listing', array('rows' => $table_data), TRUE);
  119. exit();
  120. }
  121. /**
  122. * Add Widget's Package Path
  123. *
  124. * Makes it possible for widgets to use $EE->load->view(), etc
  125. *
  126. * Should be called right before calling a widget's index() funciton
  127. */
  128. private function _add_widget_package_path($name)
  129. {
  130. $path = PATH_THIRD . $name . '/';
  131. $this->_EE->load->add_package_path($path);
  132. // manually add the view path if this is less than EE 2.1.5
  133. if (version_compare(APP_VER, '2.1.5', '<'))
  134. {
  135. $this->_EE->load->_ci_view_path = $path . 'views/';
  136. }
  137. }
  138. /**
  139. * Add selected widget to users dashboard and update config.
  140. *
  141. * @return void
  142. */
  143. public function add_widget()
  144. {
  145. $mod = $this->_EE->input->get('mod');
  146. $wgt = $this->_EE->input->get('wgt');
  147. if(isset($mod) AND isset($wgt))
  148. {
  149. $obj = $this->_get_widget_object($mod, $wgt);
  150. // determine which column has the least number of widgets in it so you can add the
  151. // new one to the one with the least
  152. $totals = array();
  153. $totals[1] = @count($this->_settings['widgets'][1]);
  154. $totals[2] = @count($this->_settings['widgets'][2]);
  155. $totals[3] = @count($this->_settings['widgets'][3]);
  156. $col = array_keys($totals, min($totals));
  157. $new_widget = array(
  158. 'mod' => $mod,
  159. 'wgt' => $wgt,
  160. );
  161. // add widget settings to config if present
  162. if(isset($obj->settings))
  163. {
  164. $new_widget['stng'] = json_encode($obj->settings);
  165. }
  166. $this->_settings['widgets'][$col[0]][] = $new_widget;
  167. // update members dashboard config in DB
  168. $this->_update_member();
  169. }
  170. $this->_EE->session->set_flashdata('message_success', lang('widget_added'));
  171. $this->_EE->functions->redirect($this->_base_url);
  172. }
  173. /**
  174. * AJAX METHOD
  175. * Remove selected widget from users dashboard and update settings.
  176. *
  177. * @return NULL
  178. */
  179. public function remove_widget()
  180. {
  181. $col = $this->_EE->input->get('col');
  182. $wgt = $this->_EE->input->get('wgt');
  183. if(isset($col) AND isset($wgt))
  184. {
  185. unset($this->_settings['widgets'][$col][$wgt]);
  186. $this->_update_member(FALSE);
  187. }
  188. }
  189. /**
  190. * AJAX METHOD
  191. * Update widget order and column placement in DB.
  192. *
  193. * @return NULL
  194. */
  195. public function update_widget_order()
  196. {
  197. $order = $this->_EE->input->get('order');
  198. if($order)
  199. {
  200. $widgets = explode('|', $order);
  201. $current = $this->_settings['widgets'];
  202. $widgets_only = array();
  203. $new = array();
  204. // get widget only settings in accessable array (without column number in front)
  205. foreach($current as $col => $wgts)
  206. {
  207. foreach($wgts as $id => $settings)
  208. {
  209. $widgets_only[$id] = $settings;
  210. }
  211. }
  212. // loop through widgets, separate based on delimiter and create new array based on submitted
  213. foreach($widgets as $widget)
  214. {
  215. $pieces = explode(':', $widget);
  216. $new[$pieces[0]][$pieces[1]] = $widgets_only[$pieces[1]];
  217. }
  218. $this->_settings['widgets'] = $new;
  219. $this->_update_member(FALSE);
  220. }
  221. return TRUE;
  222. }
  223. /**
  224. * AJAX METHOD
  225. * Display settings options for selected widget.
  226. *
  227. * @return NULL
  228. */
  229. public function widget_settings()
  230. {
  231. $col = $this->_EE->input->get('col');
  232. $wgt = $this->_EE->input->get('wgt');
  233. if(isset($col) AND isset($wgt))
  234. {
  235. $widget = $this->_settings['widgets'][$col][$wgt];
  236. $obj = $this->_get_widget_object($widget['mod'],$widget['wgt']);
  237. echo $obj->settings_form(json_decode($widget['stng']));
  238. exit();
  239. }
  240. }
  241. /**
  242. * AJAX METHOD
  243. * Attempt to update a widgets settings.
  244. *
  245. * @return NULL
  246. */
  247. public function update_settings()
  248. {
  249. $data = $_POST;
  250. $settings = array();
  251. $widget = $this->_settings['widgets'][$data['col']][$data['wgt']];
  252. foreach($data as $field => $value)
  253. {
  254. $settings[$field] = $value;
  255. }
  256. $settings_json = json_encode($settings);
  257. $this->_settings['widgets'][$data['col']][$data['wgt']]['stng'] = $settings_json;
  258. $this->_update_member(FALSE);
  259. $obj = $this->_get_widget_object($widget['mod'],$widget['wgt']);
  260. $this->_add_widget_package_path($widget['mod']);
  261. $content = $obj->index(json_decode($settings_json));
  262. $result = array(
  263. 'title' => $obj->title,
  264. 'content' => $content
  265. );
  266. echo json_encode($result);
  267. exit();
  268. }
  269. /**
  270. * Get/update users dashEE settings.
  271. *
  272. * @return array
  273. */
  274. public function _get_member_settings($member_id)
  275. {
  276. $settings = $this->_model->get_member_settings($member_id);
  277. $this->_EE->cp->get_installed_modules();
  278. // Ensure all widgets in users settings are still installed and files available.
  279. $update_member = FALSE;
  280. foreach($settings['widgets'] as $col => $widget)
  281. {
  282. if(is_array($widget))
  283. {
  284. foreach($widget as $id => $params)
  285. {
  286. if(!isset($this->_EE->cp->installed_modules[$params['mod']]) ||
  287. !file_exists(PATH_THIRD.$params['mod'].'/widgets/'.$params['wgt']))
  288. {
  289. unset($settings['widgets'][$col][$id]);
  290. $update_member = TRUE;
  291. }
  292. }
  293. }
  294. }
  295. $this->_settings = $settings;
  296. if($update_member)
  297. {
  298. $this->_update_member();
  299. }
  300. }
  301. /**
  302. * Attempt to update a members dashboard config in DB.
  303. *
  304. * @return array
  305. */
  306. private function _update_member($reindex = TRUE)
  307. {
  308. if($reindex)
  309. {
  310. // reindex widgets array before saving it to the DB
  311. $i = 1;
  312. $widgets = array(1 => array(), 2 => array(), 3 => array());
  313. foreach($this->_settings['widgets'] as $col => $widget)
  314. {
  315. if(is_array($widget))
  316. {
  317. foreach($widget as $id => $params)
  318. {
  319. $widgets[$col]['wgt'.$i] = $params;
  320. ++$i;
  321. }
  322. }
  323. }
  324. $this->_settings['widgets'] = $widgets;
  325. }
  326. $this->_model->update_member($this->_member_id, $this->_settings);
  327. }
  328. /**
  329. * Load selected widgets for display.
  330. *
  331. * @return array
  332. */
  333. private function _widget_loader(array $widgets)
  334. {
  335. $cols = array(1 => '', 2 => '', 3 => '');
  336. foreach($widgets as $col => $widget)
  337. {
  338. if(is_array($widget))
  339. {
  340. foreach($widget as $id => $params)
  341. {
  342. $obj = $this->_get_widget_object($params['mod'], $params['wgt']);
  343. $class = isset($obj->wclass) ? $obj->wclass : '';
  344. $dash_code = method_exists($obj, 'settings_form') ? 'dashee="dynamic"' : '';
  345. // check widget permissions
  346. if(method_exists($obj, 'permissions') && !$obj->permissions())
  347. {
  348. $content = '<p>'.lang('permission_denied').'</p>';
  349. }
  350. else
  351. {
  352. $this->_add_widget_package_path($params['mod']);
  353. $content = $obj->index(@json_decode($params['stng']));
  354. }
  355. $cols[$col] .= '
  356. <li id="'.$id.'" class="widget '.$class.'" '.$dash_code.'>
  357. <div class="heading">
  358. <h2>'.$obj->title.'</h2>
  359. <div class="buttons"></div>
  360. </div>
  361. <div class="widget-content">'.$content.'</div>
  362. </li>
  363. ';
  364. }
  365. }
  366. $cols[$col] .= '&nbsp;';
  367. }
  368. return $cols;
  369. }
  370. /**
  371. * Require necessary widget class and return instance.
  372. *
  373. * @param $module string Module that requested widget is part of.
  374. * @param $widget string Requested widget.
  375. * @return object
  376. */
  377. private function _get_widget_object($module, $widget)
  378. {
  379. include_once(PATH_THIRD.$module.'/widgets/'.$widget);
  380. $obj = $this->_format_filename($widget, TRUE);
  381. return new $obj();
  382. }
  383. /**
  384. * Format widget names for reference.
  385. *
  386. * @param $name string File name.
  387. * @param $cap bool Capitalize filename?
  388. * @return string
  389. */
  390. private function _format_filename($name, $cap = FALSE)
  391. {
  392. $str = str_replace('.', '_', substr($name, 0, -4));
  393. return $cap ? ucfirst($str) : $str;
  394. }
  395. }
  396. /* End of file mcp.dashee.php */
  397. /* Location: /system/expressionengine/third_party/dashee/mcp.dashee.php */