PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/modules/fuel/libraries/Fuel_modules.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 1173 lines | 716 code | 134 blank | 323 comment | 94 complexity | 624d4ab3e9f5f8de29d33c5f1a306e59 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * FUEL CMS
  4. * http://www.getfuelcms.com
  5. *
  6. * An open source Content Management System based on the
  7. * Codeigniter framework (http://codeigniter.com)
  8. *
  9. * @package FUEL CMS
  10. * @author David McReynolds @ Daylight Studio
  11. * @copyright Copyright (c) 2018, Daylight Studio LLC.
  12. * @license http://docs.getfuelcms.com/general/license
  13. * @link http://www.getfuelcms.com
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * FUEL modules object
  19. *
  20. * @package FUEL CMS
  21. * @subpackage Libraries
  22. * @category Libraries
  23. * @author David McReynolds @ Daylight Studio
  24. * @link http://docs.getfuelcms.com/modules/fuel/fuel_modules
  25. */
  26. // --------------------------------------------------------------------
  27. require_once('Fuel_installer.php');
  28. class Fuel_modules extends Fuel_base_library {
  29. protected $_modules = array();
  30. protected $_advanced = array();
  31. protected $_inited = FALSE;
  32. static protected $_module_init = array();
  33. static protected $_modules_grouped = array();
  34. static protected $_overwrites = NULL;
  35. // --------------------------------------------------------------------
  36. /**
  37. * Constructor
  38. *
  39. * Accepts an associative array as input, containing preferences (optional)
  40. *
  41. * @access public
  42. * @param array config preferences
  43. * @return void
  44. */
  45. public function __construct($params = array())
  46. {
  47. parent::__construct();
  48. $this->initialize($params);
  49. }
  50. // --------------------------------------------------------------------
  51. /**
  52. * Initialize the object and set module initialization parameters
  53. *
  54. * Accepts an associative array as input, containing module initialization preferences.
  55. *
  56. * @access public
  57. * @param array Array of additional module initialization parameters (optional)
  58. * @return void
  59. */
  60. public function initialize($params = array(), $add = TRUE)
  61. {
  62. if ($this->is_inited())
  63. {
  64. return;
  65. }
  66. $module_init = self::get_all_module_configs();
  67. self::$_module_init = $module_init;
  68. if ($add)
  69. {
  70. foreach($module_init as $mod => $init)
  71. {
  72. $this->add($mod, $init);
  73. }
  74. }
  75. $this->_inited = TRUE;
  76. }
  77. static public function get_all_module_configs()
  78. {
  79. // get simple module init values. Must use require here because of the construct
  80. //require_once(MODULES_PATH.FUEL_FOLDER.'/libraries/fuel_modules.php');
  81. $FUEL = FUEL();
  82. $allowed = $FUEL->config('modules_allowed');
  83. $module_init = array();
  84. // load the application modules first
  85. $my_module_init = (array)self::get_module_config('app');
  86. self::$_modules_grouped['app'] = $my_module_init;
  87. $fuel_module_init = (array)self::get_module_config('fuel');
  88. self::$_modules_grouped['fuel'] = $module_init;
  89. $module_init = array_merge($my_module_init, $fuel_module_init);
  90. // no longer need these so we get rid of them
  91. unset($my_module_init, $fuel_module_init);
  92. // then get the allowed modules initialization information
  93. foreach($allowed as $mod)
  94. {
  95. $mod_config = self::get_module_config($mod);
  96. if (!empty($mod_config))
  97. {
  98. self::$_modules_grouped[$mod] = $mod_config;
  99. $module_init = array_merge($module_init, $mod_config);
  100. }
  101. }
  102. // now must loop through the array and overwrite any values... array_merge_recursive won't work'
  103. $overwrites = self::overwrites();
  104. if (!empty($overwrites) AND is_array($overwrites))
  105. {
  106. foreach($overwrites as $module => $val)
  107. {
  108. if (isset($module_init[$module]))
  109. {
  110. $module_init[$module] = array_merge($module_init[$module], $val);
  111. }
  112. }
  113. }
  114. return $module_init;
  115. }
  116. // --------------------------------------------------------------------
  117. /**
  118. * Returns an advanced module's simple module config information (sounds strange I know)
  119. *
  120. * @access public
  121. * @param string Advanced module folder name
  122. * @return array
  123. */
  124. static public function get_module_config($module)
  125. {
  126. switch($module)
  127. {
  128. case 'fuel':
  129. $file_path = FUEL_PATH.'/config/fuel_modules.php';
  130. break;
  131. case 'application': case 'app':
  132. $file_path = APPPATH.'/config/MY_fuel_modules.php';
  133. break;
  134. default:
  135. $file_path = MODULES_PATH.$module.'/config/'.$module.'_fuel_modules.php';
  136. }
  137. if (file_exists($file_path))
  138. {
  139. include($file_path);
  140. if (!empty($config['modules']))
  141. {
  142. // add folder value to the module init
  143. foreach($config['modules'] as $key => $val)
  144. {
  145. if (!isset($config['modules'][$key]['folder']))
  146. {
  147. $config['modules'][$key]['folder'] = $module;
  148. }
  149. }
  150. return $config['modules'];
  151. }
  152. else
  153. {
  154. return array();
  155. }
  156. }
  157. }
  158. // --------------------------------------------------------------------
  159. /**
  160. * Add a module
  161. *
  162. * @access public
  163. * @param string Module name
  164. * @param array Module initialization parameters
  165. * @return void
  166. */
  167. public function add($mod, $init)
  168. {
  169. // check for specific module overwrites like for Fuel_navigation and Fuel_block
  170. if (isset($init['folder']))
  171. {
  172. $class_name = 'Fuel_'.strtolower($mod);
  173. $file_path = MODULES_PATH.$init['folder'].'/libraries/'.$class_name.EXT;
  174. if (file_exists($file_path))
  175. {
  176. // class must extend the fuel_module class to be legit
  177. if (strtolower(get_parent_class($class_name)) == 'fuel_module')
  178. {
  179. $this->CI->load->module_library($init['folder'], strtolower($class_name));
  180. $fuel_module =& $this->CI->$class_name;
  181. }
  182. }
  183. }
  184. if (!isset($fuel_module))
  185. {
  186. $fuel_module = new Fuel_module();
  187. }
  188. $fuel_module->initialize($mod, $init);
  189. if (empty($init['disabled']))
  190. {
  191. $this->_modules[$mod] = $fuel_module;
  192. }
  193. }
  194. // --------------------------------------------------------------------
  195. /**
  196. * Returns a module object with the specified key name
  197. *
  198. * If no module parameter is passed, then an array of all simple modules will be returned
  199. *
  200. * @access public
  201. * @param string Module name (optional)
  202. * @param boolean Whether to include advanced modules in the search. Default is TRUE. (optional)
  203. * @return object Fuel_module object
  204. */
  205. public function get($module = NULL, $include_advanced = TRUE)
  206. {
  207. // used to extract model name when there is an array with the key being the advanced module folder
  208. if (is_array($module))
  209. {
  210. $module = current($module);
  211. }
  212. // allows you to get a module based on the model name
  213. if (!empty($module) AND is_string($module) AND preg_match('#\w+_model$#', $module) OR $has_uri = (strpos($module, '/') !== FALSE))
  214. {
  215. $modules = $this->get(NULL, FALSE);
  216. foreach($modules as $key => $mod)
  217. {
  218. if (strtolower($mod->info('model_name')) == $module OR (!empty($has_uri) AND $mod->info('module_uri') == $module))
  219. {
  220. $module = $key;
  221. break;
  222. }
  223. }
  224. }
  225. if (!empty($module))
  226. {
  227. if ($module == 'fuel')
  228. {
  229. return $this->fuel;
  230. }
  231. // must check advanced modules first in case there is a submodule with the same name...
  232. // the advanced module has access to the submodule so it is more convenient
  233. if ($this->allowed($module) AND $include_advanced)
  234. {
  235. return $this->fuel->$module;
  236. }
  237. else if (!empty($this->_modules[$module]))
  238. {
  239. return $this->_modules[$module];
  240. }
  241. return FALSE;
  242. }
  243. else
  244. {
  245. return $this->_modules;
  246. }
  247. }
  248. // --------------------------------------------------------------------
  249. /**
  250. * Module overwrites
  251. *
  252. * Used to overwrite existing module configurations (e.g. pages, blocks, etc)
  253. *
  254. * @access public
  255. * @return string
  256. */
  257. static public function overwrites()
  258. {
  259. if (isset(self::$_overwrites))
  260. {
  261. return self::$_overwrites;
  262. }
  263. @include(APPPATH.'config/MY_fuel_modules.php');
  264. if (isset($config['module_overwrites']))
  265. {
  266. self::$_overwrites = $config['module_overwrites'];
  267. }
  268. else
  269. {
  270. self::$_overwrites = array();
  271. }
  272. return self::$_overwrites;
  273. }
  274. // --------------------------------------------------------------------
  275. /**
  276. * Returns an array of all the pages
  277. *
  278. * @access public
  279. * @return array
  280. */
  281. public function pages($include_pages_module = FALSE)
  282. {
  283. $all_pages = array();
  284. foreach($this->_modules as $module)
  285. {
  286. if ($include_pages_module == TRUE OR ($include_pages_module == FALSE AND $module->name() != 'pages'))
  287. {
  288. $pages = $module->pages();
  289. $all_pages = array_merge($all_pages, $pages);
  290. }
  291. }
  292. return $all_pages;
  293. }
  294. // --------------------------------------------------------------------
  295. /**
  296. * Determine whether a module exists or not
  297. *
  298. * @access public
  299. * @param string Module name
  300. * @param boolean Whether to include advanced modules in the search
  301. * @return boolean
  302. */
  303. public function exists($module, $include_advanced = TRUE)
  304. {
  305. $module = $this->get($module, $include_advanced);
  306. return $module !== FALSE;
  307. }
  308. // --------------------------------------------------------------------
  309. /**
  310. * Returns whether a module is advanced or not
  311. *
  312. * @access public
  313. * @param string Module name
  314. * @return boolean
  315. */
  316. public function is_advanced($module)
  317. {
  318. return is_a($module, 'Fuel_advanced_module');
  319. }
  320. // --------------------------------------------------------------------
  321. /**
  322. * Returns an array of all the advanced module objects
  323. *
  324. * @access public
  325. * @param boolean Determines whether to include the "fuel" module with the return value
  326. * @return array An array of Fuel_advanced_module objects
  327. */
  328. public function advanced($include_fuel = FALSE)
  329. {
  330. $advanced = array();
  331. if ($include_fuel)
  332. {
  333. $advanced['fuel'] =& $this->fuel;
  334. }
  335. foreach($this->fuel->config('modules_allowed') as $module)
  336. {
  337. $advanced[$module] =& $this->fuel->$module;
  338. }
  339. return $advanced;
  340. }
  341. // --------------------------------------------------------------------
  342. /**
  343. * Determine whether a module is allowed in the MY_config
  344. *
  345. * @access public
  346. * @param string Module name
  347. * @return boolean
  348. */
  349. public function allowed($module)
  350. {
  351. $allowed = $this->fuel->config('modules_allowed');
  352. $allowed[] = 'fuel';
  353. return (in_array($module, $allowed));
  354. }
  355. // --------------------------------------------------------------------
  356. /**
  357. * Returns the initialization parameters for a module if the module parameter is passed. No parameter passed then all initialization parameters are returned
  358. *
  359. * @access public
  360. * @param string Module name
  361. * @return array
  362. */
  363. public function module_init($module = array())
  364. {
  365. if (!empty($module))
  366. {
  367. if (isset(self::$_module_init[$module]))
  368. {
  369. return self::$_module_init[$module];
  370. }
  371. else
  372. {
  373. return FALSE;
  374. }
  375. }
  376. else
  377. {
  378. return self::$_module_init;
  379. }
  380. }
  381. // --------------------------------------------------------------------
  382. /**
  383. * Returns the initialization parameters for a module if the module parameter is passed. No parameter passed then all initialization parameters are returned
  384. *
  385. * @access public
  386. * @param string Module name
  387. * @return array
  388. */
  389. public function module_grouped_init($adv_module = array())
  390. {
  391. if (!empty($adv_module))
  392. {
  393. if (isset(self::$_modules_grouped[$adv_module]))
  394. {
  395. return self::$_modules_grouped[$adv_module];
  396. }
  397. else
  398. {
  399. return FALSE;
  400. }
  401. }
  402. else
  403. {
  404. return $this->_modules_grouped;
  405. }
  406. }
  407. // --------------------------------------------------------------------
  408. /**
  409. * Installs a module
  410. *
  411. * @access public
  412. * @param string Module name
  413. * @return boolean
  414. */
  415. public function install($module)
  416. {
  417. if (is_string($module))
  418. {
  419. $module = $this->get($module);
  420. }
  421. $key = Fuel_installer::INSTALLED_SETTINGS_KEY;
  422. $installed = $this->fuel->settings->get($module->name(), $key);
  423. if (empty($installed))
  424. {
  425. $installed = array();
  426. }
  427. $installed[$module->name()] = TRUE;
  428. $this->fuel->settings->save($module->name(), $key, $installed);
  429. // $this->CI->fuel_settings_model->debug_query();
  430. //$this->fuel->modules->install($module);
  431. }
  432. // --------------------------------------------------------------------
  433. /**
  434. * Uninstalls a module
  435. *
  436. * @access public
  437. * @param string Module name
  438. * @return boolean
  439. */
  440. public function uninstall($module)
  441. {
  442. if (is_string($module))
  443. {
  444. $module = $this->get($module);
  445. }
  446. $key = Fuel_installer::INSTALLED_SETTINGS_KEY;
  447. $installed = $this->fuel->settings->get($module->name(), $key);
  448. if (empty($installed))
  449. {
  450. $installed = array();
  451. }
  452. $installed[$module] = TRUE;
  453. $this->fuel->settings->save($module->name(), $key, $installed);
  454. }
  455. // --------------------------------------------------------------------
  456. /**
  457. * Options list for simple modules
  458. *
  459. * @access public
  460. * @return array
  461. */
  462. public function options_list($advanced = FALSE)
  463. {
  464. if ($advanced)
  465. {
  466. $modules = array_keys($this->advanced(FALSE));
  467. }
  468. else
  469. {
  470. $modules = array_keys(self::get_all_module_configs());
  471. }
  472. $options = array_combine($modules, $modules);
  473. return $options;
  474. }
  475. }
  476. // ------------------------------------------------------------------------
  477. /**
  478. * FUEL module object.
  479. *
  480. * Can be retrieved by $this->fuel->modules->get('{module_name}')
  481. *
  482. * @package FUEL CMS
  483. * @subpackage Libraries
  484. * @category Libraries
  485. * @author David McReynolds @ Daylight Studio
  486. * @prefix $module->
  487. */
  488. class Fuel_module extends Fuel_base_library {
  489. protected $module = '';
  490. protected $_init = array();
  491. protected $_info = array();
  492. public function __construct($params = array())
  493. {
  494. parent::__construct($params);
  495. // if the module name is still empty, then we will grab it from the class name
  496. if (empty($this->module))
  497. {
  498. $this->module = substr(get_class($this), 5);
  499. }
  500. }
  501. // --------------------------------------------------------------------
  502. /**
  503. * Initialize the user preferences
  504. *
  505. * Accepts an associative array as input, containing display preferences
  506. * as well as an array for simple module configuration parameters
  507. *
  508. * @access public
  509. * @param array config preferences (optional)
  510. * @param array simple module initialization parameters (optional)
  511. * @return void
  512. */
  513. public function initialize($params = array(), $init = array())
  514. {
  515. // setup any initialized variables
  516. if (is_array($params))
  517. {
  518. if (!empty($params['module']))
  519. {
  520. $this->module = $params['module'];
  521. }
  522. if (!empty($params['init']))
  523. {
  524. $this->_init = $params['init'];
  525. }
  526. }
  527. else
  528. {
  529. $this->module = $params;
  530. $this->_init = $init;
  531. }
  532. }
  533. // --------------------------------------------------------------------
  534. /**
  535. * Returns the name of the module
  536. *
  537. * @access public
  538. * @return string
  539. */
  540. public function name()
  541. {
  542. return $this->module;
  543. }
  544. // --------------------------------------------------------------------
  545. /**
  546. * Retrieve the info for a module
  547. *
  548. * @access public
  549. * @param string module name (optional)
  550. * @return array
  551. */
  552. public function info($prop = NULL)
  553. {
  554. if (empty($this->_init))
  555. {
  556. $inits = Fuel_modules::get_all_module_configs();
  557. if (isset($inits[$this->module]))
  558. {
  559. $this->_init = $inits[$this->module];
  560. }
  561. }
  562. if (empty($this->_info))
  563. {
  564. $this->CI->load->helper('inflector');
  565. $this->CI->load->helper('string');
  566. $defaults = array(
  567. 'module_name' => humanize($this->module),
  568. 'module_uri' => $this->module,
  569. 'model_name' => $this->module.'_model',
  570. 'model_location' => '',
  571. 'view_location' => '',
  572. 'display_field' => '',
  573. 'preview_path' => '',
  574. 'views' => array(
  575. 'list' => 'modules/module_list',
  576. 'create_edit' => 'modules/module_create_edit',
  577. 'delete' => 'modules/module_delete'),
  578. 'permission' => array($this->module, 'create', 'edit', 'publish', 'delete', 'export'),
  579. 'js_controller' => 'fuel.controller.BaseFuelController',
  580. 'js_controller_path' => '',
  581. 'js_controller_params' => array(),
  582. 'js_localized' => array(),
  583. 'js' => '',
  584. 'edit_method' => 'find_one_array',
  585. 'instructions' => lang('module_instructions_default', strtolower(humanize($this->module))),
  586. 'filters' => array(),
  587. 'archivable' => TRUE,
  588. 'table_headers' => array(),
  589. 'table_actions' => array('EDIT', 'VIEW', 'DELETE'),
  590. 'item_actions' => array('save', 'view', 'publish', 'activate', 'delete', 'duplicate', 'replace', 'create'),
  591. 'list_actions' => array(),
  592. 'rows_selectable' => TRUE,
  593. 'precedence_col' => 'precedence',
  594. 'clear_cache_on_save' => TRUE,
  595. 'create_action_name' => lang('btn_create'),
  596. 'configuration' => '',
  597. 'nav_selected' => NULL,
  598. 'default_col' => NULL,
  599. 'default_order' => NULL,
  600. 'sanitize_input' => TRUE,
  601. 'sanitize_files' => FALSE,
  602. 'displayonly' => FALSE,
  603. 'language' => '',
  604. 'language_col' => 'language',
  605. 'hidden' => FALSE,
  606. 'disabled' => FALSE,
  607. 'icon_class' => '',
  608. 'folder' => '',
  609. 'exportable' => FALSE,
  610. 'limit_options' => array('50' => '50', '100' => '100', '200' => '200'),
  611. 'advanced_search' => FALSE,
  612. 'disable_heading_sort' => FALSE,
  613. 'description' => '',
  614. 'search_field' => '',
  615. 'single_item_navigate' => FALSE,
  616. 'pages' => array(),
  617. );
  618. $info = array();
  619. foreach ($defaults as $key => $val)
  620. {
  621. if (isset($this->_init[$key]))
  622. {
  623. $info[$key] = $this->_init[$key];
  624. }
  625. else
  626. {
  627. $info[$key] = $val;
  628. }
  629. }
  630. // icon class for module
  631. if (empty($info['icon_class']))
  632. {
  633. $info['icon_class'] = 'ico_'.url_title(str_replace('/', '_', $info['module_uri']),'_', TRUE);
  634. }
  635. // localize certain fields
  636. if (empty($info['module_name']) AND $module_name = lang('module_'.$this->module))
  637. {
  638. $info['module_name'] = $module_name;
  639. }
  640. // set proper jqxController name
  641. if (is_array($info['js_controller']))
  642. {
  643. if (empty($info['js_controller_path']))
  644. {
  645. $info['js_controller_path'] = js_path('', key($info['js_controller']));
  646. }
  647. $info['js_controller'] = current($info['js_controller']);
  648. }
  649. else if (is_string($info['js_controller']) AND strpos($info['js_controller'], '.') === FALSE)
  650. {
  651. //$info['js_controller'] = 'fuel.controller.'.$info['js_controller'];
  652. $info['js_controller'] = $info['js_controller'];
  653. }
  654. // convert slashes to jqx object periods
  655. $info['js_controller'] = str_replace('/', '.', $info['js_controller']);
  656. // set the base path to the controller file if still empty
  657. if (empty($info['js_controller_path']))
  658. {
  659. $info['js_controller_path'] = js_path('', FUEL_FOLDER);
  660. }
  661. if ($create_action_name = lang('module_'.$this->module.'_create'))
  662. {
  663. $info['create_action_name'] = $create_action_name;
  664. }
  665. $this->_info = $info;
  666. // must be done after the above
  667. if (empty($this->_info['display_field']))
  668. {
  669. $model = $this->model();
  670. if ($model)
  671. {
  672. $fields = $model->fields();
  673. // loop through the fields and find the first column that doesn't have id or _id at the end of it
  674. for ($i = 1; $i < count($fields); $i++)
  675. {
  676. if (substr($fields[$i], -3) != '_id')
  677. {
  678. $this->_info['display_field'] = $fields[$i];
  679. break;
  680. }
  681. }
  682. if (empty($this->_info['display_field'])) $this->_info['display_field'] = $fields[1]; // usually the second field is the display_field... first is the id
  683. }
  684. }
  685. }
  686. if (empty($prop))
  687. {
  688. return $this->_info;
  689. }
  690. else if (isset($this->_info[$prop]))
  691. {
  692. return $this->_info[$prop];
  693. }
  694. else
  695. {
  696. return FALSE;
  697. }
  698. }
  699. // --------------------------------------------------------------------
  700. /**
  701. * Sets a simple module's property
  702. *
  703. * @access public
  704. * @return array
  705. */
  706. public function set_info($key, $prop)
  707. {
  708. $info = $this->info();
  709. if (is_array($key))
  710. {
  711. foreach($key as $k => $v)
  712. {
  713. if (isset($this->_info[$k]))
  714. {
  715. $this->_info[$k] = $v;
  716. }
  717. else
  718. {
  719. throw new Exception(lang('error_class_property_does_not_exist', $k));
  720. }
  721. }
  722. }
  723. else
  724. {
  725. if (isset($this->_info[$key]))
  726. {
  727. return $this->_info[$key];
  728. }
  729. else
  730. {
  731. throw new Exception(lang('error_class_property_does_not_exist', $key));
  732. }
  733. }
  734. }
  735. // --------------------------------------------------------------------
  736. /**
  737. * Get the pages of a module
  738. *
  739. * @access public
  740. * @return array
  741. */
  742. public function pages()
  743. {
  744. $pages = array();
  745. $info = $this->info();
  746. // if no preview path, then just ignore
  747. if (empty($info['preview_path']))
  748. {
  749. return array();
  750. }
  751. if (!empty($info['model_location']))
  752. {
  753. $this->CI->load->module_model($info['model_location'], $info['model_name']);
  754. }
  755. else
  756. {
  757. $this->CI->load->model($info['model_name']);
  758. }
  759. $model = $info['model_name'];
  760. $records = array();
  761. if (method_exists($model, 'find_all_array'))
  762. {
  763. $records = $this->CI->$model->find_all_array();
  764. }
  765. foreach($records as $record)
  766. {
  767. if (is_callable($info['preview_path']))
  768. {
  769. $pages[$page] = $info['preview_path']($record);
  770. }
  771. else
  772. {
  773. preg_match_all('#{(\w+)}#', $info['preview_path'], $matches);
  774. $page = $info['preview_path'];
  775. $replaced = FALSE;
  776. if (!empty($matches[1]))
  777. {
  778. foreach($matches[1] as $match)
  779. {
  780. if (!empty($record[$match]))
  781. {
  782. $page = str_replace('{'.$match.'}', $record[$match], $page);
  783. $replaced = TRUE;
  784. }
  785. }
  786. }
  787. if (!empty($replaced))
  788. {
  789. $pages[$page] = $page;
  790. }
  791. }
  792. }
  793. return $pages;
  794. }
  795. // --------------------------------------------------------------------
  796. /**
  797. * Returns the url based on the preview_path configuration of the simple module
  798. *
  799. * @access public
  800. * @param array data to be merged in with preview path URL
  801. * @return string
  802. */
  803. public function url($data = array())
  804. {
  805. $preview_path = $this->info('preview_path');
  806. if (empty($preview_path))
  807. {
  808. return FALSE;
  809. }
  810. // check if it is a callable function first
  811. if (is_callable($preview_path))
  812. {
  813. $preview_path = $preview_path($data);
  814. }
  815. else
  816. {
  817. // substitute data values into preview path
  818. preg_match_all('#\{(.+)\}+#U', $preview_path, $matches);
  819. if (!empty($matches[1]))
  820. {
  821. foreach($matches[1] as $match)
  822. {
  823. if (!empty($data[$match]))
  824. {
  825. $preview_path = str_replace('{'.$match.'}', $data[$match], $preview_path);
  826. }
  827. }
  828. }
  829. }
  830. return $preview_path;
  831. }
  832. // --------------------------------------------------------------------
  833. /**
  834. * The server path to a module
  835. *
  836. * @access public
  837. * @return string
  838. */
  839. public function module_path()
  840. {
  841. return MODULES_PATH.$this->module.'/';
  842. }
  843. // --------------------------------------------------------------------
  844. /**
  845. * Returns the model of the module
  846. *
  847. * @access public
  848. * @return string
  849. */
  850. public function model()
  851. {
  852. $model = $this->info('model_name');
  853. $module = $this->info('model_location');
  854. if (empty($module))
  855. {
  856. $module = 'app';
  857. }
  858. if ($model !== FALSE AND !isset($this->CI->$model) AND !empty($module))
  859. {
  860. $this->CI->load->module_model($module, $model);
  861. }
  862. if ($model)
  863. {
  864. return $this->CI->$model;
  865. }
  866. }
  867. // --------------------------------------------------------------------
  868. /**
  869. * Loads a module model and creates a variable in the view that you can use to merge data
  870. *
  871. * @access public
  872. * @param mixed A string value of "all", "one", "key", "find" or "by" OR A key value array of options which include "find", "select", "where", "order", "limit", "offset", "return_method", "assoc_key", "var", "module", and "params"(optional)
  873. * @param mixed Where condition (since it's most common parameter) (optional)
  874. * @return string
  875. */
  876. public function find($params = array(), $where = NULL)
  877. {
  878. $valid = array( 'find' => 'all',
  879. 'select' => NULL,
  880. 'where' => '',
  881. 'order' => '',
  882. 'limit' => NULL,
  883. 'offset' => 0,
  884. 'return_method' => 'auto',
  885. 'assoc_key' => '',
  886. 'var' => '',
  887. 'module' => '',
  888. 'params' => array(),
  889. );
  890. $model = $this->model();
  891. $native = TRUE;
  892. if (is_string($params))
  893. {
  894. if (preg_match('#^(all|one|key|find|by)#', $params))
  895. {
  896. $find = $params;
  897. $params = array();
  898. $params['find'] = $find;
  899. $params['where'] = $where;
  900. }
  901. elseif (method_exists($model, 'find_'.$params))
  902. {
  903. $find = $params;
  904. $args = $where;
  905. $native = FALSE;
  906. }
  907. else
  908. {
  909. $this->CI->load->helper('array');
  910. $params = parse_string_to_array($params);
  911. }
  912. }
  913. if ($native)
  914. {
  915. foreach($valid as $p => $default)
  916. {
  917. $$p = (isset($params[$p])) ? $params[$p] : $default;
  918. }
  919. }
  920. // to get around escaping issues we need to add spaces after =
  921. if (is_string($where))
  922. {
  923. $where = preg_replace('#([^>|<|!])=#', '$1 = ', $where);
  924. }
  925. // run select statement before the find
  926. if (!empty($select))
  927. {
  928. $model->db()->select($select, FALSE);
  929. }
  930. // retrieve data based on the method
  931. if ($native)
  932. {
  933. $data = $model->find($find, $where, $order, $limit, $offset, $return_method, $assoc_key);
  934. }
  935. else
  936. {
  937. $args = array_merge(array($find), $args);
  938. $data = call_user_func_array(array($model, 'find'), $args);
  939. }
  940. if ($data !== FALSE)
  941. {
  942. if (is_array($data) AND key($data) === 0)
  943. {
  944. $var = $model->friendly_name(TRUE);
  945. }
  946. else
  947. {
  948. $var = $model->singular_name(TRUE);
  949. }
  950. }
  951. $vars[$var] = $data;
  952. // load the variable for the view to use
  953. $this->CI->load->vars($vars);
  954. // set the model to readonly so no data manipulation can occur
  955. $model->readonly = TRUE;
  956. return $data;
  957. }
  958. // --------------------------------------------------------------------
  959. /**
  960. * An alias to the modules model to save
  961. *
  962. * @access public
  963. * @param array An array of values to save to the module's model
  964. * @return boolean
  965. */
  966. public function save($values)
  967. {
  968. return $this->model()->save($values);
  969. }
  970. // --------------------------------------------------------------------
  971. /**
  972. * An alias to the module's model t create a new record
  973. *
  974. * @access public
  975. * @param array An array of values to save to the module's model (optional)
  976. * @return object Record_class object
  977. */
  978. public function create($values = array())
  979. {
  980. return $this->model()->create($values);
  981. }
  982. // --------------------------------------------------------------------
  983. /**
  984. * An alias to the module's model t delete a record
  985. *
  986. * @access public
  987. * @param array Where condition to use for deleting record
  988. * @return boolean
  989. */
  990. public function delete($where)
  991. {
  992. return $this->model()->delete($where);
  993. }
  994. // --------------------------------------------------------------------
  995. /**
  996. * Magic method to find records on the module's model
  997. *
  998. * @access private
  999. * @param string Method name
  1000. * @param array Method arguments
  1001. * @return array
  1002. */
  1003. public function __call($name, $args)
  1004. {
  1005. if (preg_match('#^find_#', $name))
  1006. {
  1007. //$find = preg_replace('#^find_#', '', $name);
  1008. //$params['find'] = $find;
  1009. $params['params'] = $args;
  1010. return $this->find($params);
  1011. }
  1012. else
  1013. {
  1014. throw new Exception(lang('error_class_method_does_not_exist', $name));
  1015. }
  1016. }
  1017. // --------------------------------------------------------------------
  1018. /**
  1019. * Magic method to get value of a module property
  1020. *
  1021. * @access public
  1022. * @param string Module property
  1023. * @return array
  1024. */
  1025. public function __get($var)
  1026. {
  1027. $info = $this->info();
  1028. if (isset($info[$var]))
  1029. {
  1030. return $info[$var];
  1031. }
  1032. else
  1033. {
  1034. throw new Exception(lang('error_class_property_does_not_exist', $var));
  1035. }
  1036. }
  1037. }
  1038. /* End of file Fuel_modules.php */
  1039. /* Location: ./modules/fuel/libraries/fuel/Fuel_modules.php */