/techne/expressionengine/fieldtypes/ft.multi_select.php

https://github.com/mondomon916/LYBC · PHP · 220 lines · 146 code · 37 blank · 37 comment · 16 complexity · dcfdae771bdc2b39c21956d8fe2e5d78 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 - 2010, 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. * ExpressionEngine Multi-Select Fieldtype Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Fieldtypes
  19. * @category Fieldtypes
  20. * @author ExpressionEngine Dev Team
  21. * @link http://expressionengine.com
  22. */
  23. class Multi_select_ft extends EE_Fieldtype {
  24. var $info = array(
  25. 'name' => 'Multi Select',
  26. 'version' => '1.0'
  27. );
  28. var $has_array_data = TRUE;
  29. /**
  30. * Constructor
  31. *
  32. * @access public
  33. */
  34. function Multi_select_ft()
  35. {
  36. parent::EE_Fieldtype();
  37. }
  38. // --------------------------------------------------------------------
  39. function display_field($data)
  40. {
  41. $this->EE->load->helper('custom_field');
  42. $values = decode_multi_field($data);
  43. $field_options = $this->_get_field_options($data);
  44. return form_multiselect($this->field_name.'[]', $field_options, $values, 'dir="'.$this->settings['field_text_direction'].'" id="'.$this->field_id.'"');
  45. }
  46. // --------------------------------------------------------------------
  47. function replace_tag($data, $params = array(), $tagdata = FALSE)
  48. {
  49. $this->EE->load->helper('custom_field');
  50. $data = decode_multi_field($data);
  51. if ($tagdata)
  52. {
  53. return $this->_parse_multi($data, $params, $tagdata);
  54. }
  55. else
  56. {
  57. return $this->_parse_single($data, $params);
  58. }
  59. }
  60. // --------------------------------------------------------------------
  61. function _parse_single($data, $params)
  62. {
  63. if (isset($params['limit']))
  64. {
  65. $limit = intval($params['limit']);
  66. if (count($data) > $limit)
  67. {
  68. $data = array_slice($data, 0, $limit);
  69. }
  70. }
  71. if (isset($params['markup']) && ($params['markup'] == 'ol' OR $params['markup'] == 'ul'))
  72. {
  73. $entry = '<'.$params['markup'].'>';
  74. foreach($data as $dv)
  75. {
  76. $entry .= '<li>';
  77. $entry .= $dv;
  78. $entry .= '</li>';
  79. }
  80. $entry .= '</'.$params['markup'].'>';
  81. }
  82. else
  83. {
  84. $entry = implode(', ', $data);
  85. }
  86. return $this->EE->typography->parse_type(
  87. $this->EE->functions->encode_ee_tags($entry),
  88. array(
  89. 'text_format' => $this->row['field_ft_'.$this->field_id],
  90. 'html_format' => $this->row['channel_html_formatting'],
  91. 'auto_links' => $this->row['channel_auto_link_urls'],
  92. 'allow_img_url' => $this->row['channel_allow_img_urls']
  93. )
  94. );
  95. }
  96. // --------------------------------------------------------------------
  97. function _parse_multi($data, $params, $tagdata)
  98. {
  99. $chunk = '';
  100. $limit = FALSE;
  101. // Limit Parameter
  102. if (is_array($params) AND isset($params['limit']))
  103. {
  104. $limit = $params['limit'];
  105. }
  106. foreach($data as $key => $item)
  107. {
  108. if ( ! $limit OR $key < $limit)
  109. {
  110. $vars['item'] = $item;
  111. $vars['count'] = $key + 1; // {count} parameter
  112. $tmp = $this->EE->functions->prep_conditionals($tagdata, $vars);
  113. $chunk .= $this->EE->functions->var_swap($tmp, $vars);
  114. }
  115. else
  116. {
  117. break;
  118. }
  119. }
  120. // Everybody loves backspace
  121. if (isset($params['backspace']))
  122. {
  123. $chunk = substr($chunk, 0, - $params['backspace']);
  124. }
  125. // Typography!
  126. return $this->EE->typography->parse_type(
  127. $this->EE->functions->encode_ee_tags($chunk),
  128. array(
  129. 'text_format' => $this->row['field_ft_'.$this->field_id],
  130. 'html_format' => $this->row['channel_html_formatting'],
  131. 'auto_links' => $this->row['channel_auto_link_urls'],
  132. 'allow_img_url' => $this->row['channel_allow_img_urls']
  133. )
  134. );
  135. }
  136. function display_settings($data)
  137. {
  138. $this->field_formatting_row($data, 'multi_select');
  139. $this->multi_item_row($data, 'multi_select');
  140. }
  141. function _get_field_options($data)
  142. {
  143. $field_options = array();
  144. if ($this->settings['field_pre_populate'] == 'n')
  145. {
  146. if ( ! is_array($this->settings['field_list_items']))
  147. {
  148. foreach (explode("\n", trim($this->settings['field_list_items'])) as $v)
  149. {
  150. $v = trim($v);
  151. $field_options[form_prep($v)] = form_prep($v);
  152. }
  153. }
  154. else
  155. {
  156. $field_options = $this->settings['field_list_items'];
  157. }
  158. }
  159. else
  160. {
  161. // We need to pre-populate this menu from an another channel custom field
  162. $this->EE->db->select('field_id_'.$this->settings['field_pre_field_id']);
  163. $this->EE->db->where('channel_id', $this->settings['field_pre_channel_id']);
  164. $pop_query = $this->EE->db->get('channel_data');
  165. $field_options[''] = '--';
  166. if ($pop_query->num_rows() > 0)
  167. {
  168. foreach ($pop_query->result_array() as $prow)
  169. {
  170. $selected = ($prow['field_id_'.$this->settings['field_pre_field_id']] == $data) ? 1 : '';
  171. $pretitle = substr($prow['field_id_'.$this->settings['field_pre_field_id']], 0, 110);
  172. $pretitle = str_replace(array("\r\n", "\r", "\n", "\t"), " ", $pretitle);
  173. $pretitle = form_prep($pretitle);
  174. $field_options[form_prep($prow['field_id_'.$this->settings['field_pre_field_id']])] = $pretitle;
  175. }
  176. }
  177. }
  178. return $field_options;
  179. }
  180. }
  181. // END Multi_select_ft class
  182. /* End of file ft.multi_select.php */
  183. /* Location: ./system/expressionengine/fieldtypes/ft.multi_select.php */