PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/modules/fuel/models/Fuel_tags_model.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 264 lines | 129 code | 40 blank | 95 comment | 26 complexity | f9e0ab9100f56d8aed289da19b764fe0 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. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Extends Base_module_model
  18. *
  19. * <strong>Fuel_tags_model</strong> is used for managing FUEL tags in the CMS
  20. *
  21. * @package FUEL CMS
  22. * @subpackage Models
  23. * @category Models
  24. * @author David McReynolds @ Daylight Studio
  25. * @link http://docs.getfuelcms.com/models/fuel_tags_model
  26. */
  27. require_once('Base_module_model.php');
  28. class Fuel_tags_model extends Base_module_model {
  29. public $filters = array('name', 'slug'); // Allows for filtering on both the name and the slug
  30. public $unique_fields = array('slug'); // The slug value must be unique
  31. public $linked_fields = array('name' => 'slug'); // the slug value should be the name field's value with the url_title function applied to it if there is no value specified
  32. public $foreign_keys = array('category_id' => array(FUEL_FOLDER => 'fuel_categories_model')); // to create the foreign key association with the fuel_categories model
  33. protected $friendly_name = 'Tags';
  34. protected $singular_name = 'Tag';
  35. // --------------------------------------------------------------------
  36. /**
  37. * Constructor.
  38. *
  39. * @access public
  40. * @return void
  41. */
  42. public function __construct()
  43. {
  44. parent::__construct('fuel_tags'); // table name
  45. $this->init_relationships();
  46. }
  47. // --------------------------------------------------------------------
  48. /**
  49. * Lists the module's items
  50. *
  51. * @access public
  52. * @param int The limit value for the list data (optional)
  53. * @param int The offset value for the list data (optional)
  54. * @param string The field name to order by (optional)
  55. * @param string The sorting order (optional)
  56. * @param boolean Determines whether the result is just an integer of the number of records or an array of data (optional)
  57. * @return mixed If $just_count is true it will return an integer value. Otherwise it will return an array of data (optional)
  58. */
  59. public function list_items($limit = NULL, $offset = NULL, $col = 'name', $order = 'asc', $just_count = FALSE)
  60. {
  61. $table = $this->table_name();
  62. $categories_table = $this->_tables['fuel_categories'];
  63. $CI =& get_instance();
  64. $this->db->join($categories_table, $categories_table.'.id = '.$table.'.category_id', 'LEFT');
  65. if ($CI->fuel->language->has_multiple())
  66. {
  67. $this->db->select($table.'.id, '.$table.'.name, '.$table.'.slug, '.$categories_table.'.name as category, SUBSTRING('.$table.'.description, 1, 50) as description, '.$table.'.context, '.$table.'.language, '.$table.'.precedence, '.$table.'.published', FALSE);
  68. }
  69. else
  70. {
  71. $this->db->select($table.'.id, '.$table.'.name, '.$table.'.slug, '.$categories_table.'.name as category, SUBSTRING('.$table.'.description, 1, 50) as description, '.$table.'.context, '.$table.'.precedence, '.$table.'.published', FALSE);
  72. }
  73. $data = parent::list_items($limit, $offset, $col, $order, $just_count);
  74. if (empty($just_count))
  75. {
  76. foreach($data as $key => $val)
  77. {
  78. $data[$key]['description'] = htmlentities($val['description'], ENT_QUOTES, 'UTF-8');
  79. }
  80. }
  81. return $data;
  82. }
  83. // --------------------------------------------------------------------
  84. /**
  85. * Initializes belongs_to relationship
  86. *
  87. * @access public
  88. * @return void
  89. */
  90. public function init_relationships()
  91. {
  92. $CI =& get_instance();
  93. $modules = $CI->fuel->modules->get(NULL, FALSE);
  94. $belongs_to = array();
  95. // loop through all the modules to check for has_many relationships
  96. unset($modules['tags']);
  97. foreach($modules as $module)
  98. {
  99. // grab each model
  100. $model = $module->model();
  101. if (!empty($model->has_many))
  102. {
  103. // loop through the has_many relationships to see if any have a "tags" relationship
  104. foreach($model->has_many as $key => $rel)
  105. {
  106. $mod_name = $module->name();
  107. $model_name = $module->info('model_name');
  108. $module_location = $module->info('model_location');
  109. if (is_array($rel))
  110. {
  111. if (isset($rel['model']) AND (($rel['model'] == 'tags' OR $rel['model'] == array(FUEL_FOLDER => 'tags'))
  112. OR ($rel['model'] == 'fuel_tags_model' OR $rel['model'] == array(FUEL_FOLDER => 'fuel_tags_model'))))
  113. {
  114. $belongs_to[$mod_name] = array('model' => $model_name, 'module' => $module_location);
  115. }
  116. else if (current($rel) == 'tags' OR current($rel) == 'fuel_tags_model')
  117. {
  118. $belongs_to[$mod_name] = array('model' => $model_name, 'module' => $module_location);
  119. }
  120. else if (!empty($rel['model']))
  121. {
  122. $rel_model_name = $this->load_model($rel['model']);
  123. $rel_model = $this->CI->$rel_model_name;
  124. // test if the instantiated model uses the fuel_tags table or not
  125. if (method_exists($rel_model, 'table_name') AND $rel_model->table_name() == $rel_model->tables('fuel_tags'))
  126. {
  127. $belongs_to[$mod_name] = array('model' => $model_name, 'module' => $module_location);
  128. }
  129. }
  130. }
  131. else if (is_string($rel) AND ($rel == 'tags' OR $rel == 'fuel_tags_model'))
  132. {
  133. $belongs_to[$mod_name] = array('model' => $model_name, 'module' => $module_location);
  134. }
  135. }
  136. }
  137. }
  138. // set the belongs_to
  139. $this->belongs_to = $belongs_to;
  140. }
  141. /**
  142. * Tree view that puts tags in a hierarchy based on their category
  143. *
  144. * @access public
  145. * @param boolean Determines whether to return just published pages or not (optional... and ignored in the admin)
  146. * @return array An array that can be used by the Menu class to create a hierarchical structure
  147. */
  148. public function tree($just_published = FALSE)
  149. {
  150. $return = array();
  151. $where = ($just_published) ? array('published' => 'yes') : array();
  152. $categories = $this->fuel_categories_model->find_all_array($where);
  153. foreach($categories as $category)
  154. {
  155. $return[] = array('id' =>$category['id'], 'label' => $category['name'], 'parent_id' => $category['parent_id'], 'location' => '');
  156. $tags = $this->find_all_array( array('category_id'=>$category['id']), 'name asc' );
  157. foreach($tags as $tag){
  158. $return[] = array('id' => $tag['name'], 'label' => $tag['name'], 'parent_id' => $category['id'], 'location' => fuel_url('tags/edit/'.$tag['id']));
  159. }
  160. }
  161. return $return;
  162. }
  163. // --------------------------------------------------------------------
  164. /**
  165. * Creates the form_fields array to be used with Form_builder
  166. *
  167. * @access public
  168. * @param array an array of values to pass to the form fields
  169. * @param array related field information
  170. * @return array
  171. */
  172. public function form_fields($values = array(), $related = array())
  173. {
  174. $fields = parent::form_fields($values, $related);
  175. $fields['language'] = array('type' => 'select', 'options' => $this->fuel->language->options(), 'value' => $this->fuel->language->default_option(), 'hide_if_one' => TRUE, 'first_option' => lang('label_select_one'));
  176. return $fields;
  177. }
  178. // --------------------------------------------------------------------
  179. /**
  180. * Overwrites the parent option_list method
  181. *
  182. * @access public
  183. * @param string the name of the field to be used as the key (optional)
  184. * @param string the name of the filed to be used as the value (optional)
  185. * @param mixed the where condition to apply (optional)
  186. * @param mixed the order in which to return the results (optional)
  187. * @return array
  188. */
  189. public function options_list($key = 'id', $val = 'name', $where = array(), $order = TRUE)
  190. {
  191. $this->db->join($this->_tables['fuel_categories'], $this->_tables['fuel_categories'].'.id = '.$this->_tables['fuel_tags'].'.category_id', 'LEFT');
  192. // if (empty($key)) $key = $this->_tables['fuel_categories'].'.id';
  193. // if (empty($val)) $val = $this->_tables['fuel_categories'].'.name';
  194. if (empty($key)) $key = $this->_tables['fuel_tags'].'.id';
  195. if (empty($val)) $val = $this->_tables['fuel_tags'].'.name';
  196. // needed to prevent ambiguity
  197. if (strpos($key, '.') === FALSE AND strpos($key, '(') === FALSE)
  198. {
  199. $key = $this->_tables['fuel_tags'].'.'.$key;
  200. }
  201. // needed to prevent ambiguity
  202. if (strpos($val, '.') === FALSE AND strpos($val, '(') === FALSE)
  203. {
  204. $val = $this->_tables['fuel_tags'].'.'.$val;
  205. }
  206. $options = parent::options_list($key, $val, $where, $order);
  207. return $options;
  208. }
  209. // --------------------------------------------------------------------
  210. /**
  211. * Common query to automatically join the categories table
  212. *
  213. * @access public
  214. * @param mixed parameter to pass to common query (optional)
  215. * @return void
  216. */
  217. public function _common_query($params = NULL)
  218. {
  219. parent::_common_query();
  220. $this->db->join($this->_tables['fuel_categories'], $this->_tables['fuel_categories'].'.id = '.$this->_tables['fuel_tags'].'.category_id', 'LEFT');
  221. }
  222. }
  223. class Fuel_tag_model extends Base_module_record {
  224. }