PageRenderTime 207ms CodeModel.GetById 115ms RepoModel.GetById 2ms app.codeStats 1ms

/fuel/modules/fuel/libraries/Fuel_custom_fields.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 2257 lines | 1653 code | 291 blank | 313 comment | 271 complexity | 38a9cb98362f316185e27d624e5aa3e8 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. <?php
  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. * FUEL custom fields object
  18. *
  19. * @package FUEL CMS
  20. * @subpackage Libraries
  21. * @category Libraries
  22. * @author David McReynolds @ Daylight Studio
  23. * @link http://docs.getfuelcms.com/libraries/form_builder
  24. * @autodoc FALSE
  25. */
  26. // --------------------------------------------------------------------
  27. class Fuel_custom_fields {
  28. protected $CI;
  29. protected $fuel;
  30. // --------------------------------------------------------------------
  31. /**
  32. * Constructor
  33. *
  34. * @access public
  35. * @return void
  36. */
  37. public function __construct()
  38. {
  39. $this->CI =& get_instance();
  40. $this->fuel =& $this->CI->fuel;
  41. }
  42. // --------------------------------------------------------------------
  43. /**
  44. * Creates a richer text editor for textarea fields
  45. *
  46. * @access public
  47. * @param array Fields parameters
  48. * @return string
  49. */
  50. public function wysiwyg($params)
  51. {
  52. $form_builder =& $params['instance'];
  53. if (isset($params['editor']))
  54. {
  55. if ($params['editor'] === FALSE)
  56. {
  57. $params['class'] = 'no_editor';
  58. }
  59. else if (strtolower($params['editor']) == 'ckeditor' OR strtolower($params['editor']) == 'wysiwyg')
  60. {
  61. $params['class'] = 'wysiwyg '.$params['class'];
  62. }
  63. else if (strtolower($params['editor']) == 'markitup')
  64. {
  65. $params['class'] = 'markitup '.$params['class'];
  66. }
  67. }
  68. else
  69. {
  70. $params['editor'] = $this->fuel->config('text_editor');
  71. }
  72. if (!isset($params['data']))
  73. {
  74. $params['data'] = $params['data'] = array();
  75. }
  76. if (isset($params['preview']))
  77. {
  78. $params['data']['preview'] = $params['preview'];
  79. }
  80. // the dimensions for the preview window
  81. $params['data']['preview_options'] = (!empty($params['preview_options'])) ? $params['preview_options'] : 'width=1024,height=768';
  82. // set the image folder for inserting assets
  83. if (isset($params['img_folder']))
  84. {
  85. $params['data']['img_folder'] = $params['img_folder'];
  86. }
  87. // set the image order in the dropdown select (name or last_updated)
  88. if (isset($params['img_order']))
  89. {
  90. $params['data']['img_order'] = $params['img_order'];
  91. }
  92. // adds list of PDFs when selecting a PDF link
  93. if (isset($params['link_pdfs']))
  94. {
  95. $params['data']['link_pdfs'] = 1;
  96. }
  97. // set the image folder for inserting assets
  98. if (isset($params['link_filter']))
  99. {
  100. $params['data']['link_filter'] = $params['link_filter'];
  101. }
  102. // adds markdown controlls to the markItUp! editor
  103. if (isset($params['markdown']) AND $params['markdown'] === TRUE AND empty($params['editor_config']))
  104. {
  105. $params['editor_config'] = 'markdown';
  106. }
  107. static $markitup_config;
  108. static $ckeditor_config;
  109. if (empty($markitup_config) OR empty($ckeditor_config))
  110. {
  111. include(APPPATH.'config/editors.php');
  112. $markitup_config = $config['markitup'];
  113. $ckeditor_config = $config['ckeditor'];
  114. }
  115. $editor_set = ($params['editor'] == 'ckeditor') ? $ckeditor_config : $markitup_config;
  116. if (!empty($params['editor_config']))
  117. {
  118. if (is_string($params['editor_config']))
  119. {
  120. if (strpos($params['editor_config'], '{') === FALSE)
  121. {
  122. $editor_config = $editor_set[$params['editor_config']];
  123. $params['data']['editor_set'] = $params['editor_config'];
  124. }
  125. else
  126. {
  127. $params['data']['editor_set'] = 'default';
  128. $editor_config = $params['editor_config'];
  129. }
  130. }
  131. elseif (is_array($params['editor_config']))
  132. {
  133. //$params['editor_config'] = array_merge($editor_config['default'], $params['editor_config']);
  134. $params['data']['editor_set'] = 'default';
  135. $editor_config = $params['editor_config'];
  136. }
  137. }
  138. else
  139. {
  140. $params['data']['editor_set'] = 'default';
  141. $editor_config = $editor_set[$params['data']['editor_set']];
  142. }
  143. if (is_array($editor_config))
  144. {
  145. if (!empty($params['editor_append_toolbar']) AND is_array($editor_config['toolbar']))
  146. {
  147. $first_item = current($params['editor_append_toolbar']);
  148. if (is_array($first_item))
  149. {
  150. $editor_config['toolbar'] = array_merge($editor_config['toolbar'], $params['editor_append_toolbar']);
  151. }
  152. else
  153. {
  154. $editor_config['toolbar'][] = $params['editor_append_toolbar'];
  155. }
  156. }
  157. if ($params['editor'] == 'markitup' AND !empty($editor_config['toolbar']))
  158. {
  159. if (empty($editor_config['markupSet']))
  160. {
  161. $editor_config['markupSet'] = array();
  162. }
  163. if (is_array($editor_config['markupSet']) AND is_array($editor_config['toolbar']))
  164. {
  165. $editor_config['markupSet'] = array_merge($editor_config['toolbar'], $editor_config['markupSet']);
  166. }
  167. unset($editor_config['toolbar']);
  168. }
  169. }
  170. if (is_array($editor_config))
  171. {
  172. $editor_config = json_encode($editor_config);
  173. }
  174. $js = '<script>jQuery(function(){ fuel.fields.setElementData("'.$params['name'].'", "editor", '.$editor_config.');});</script>';
  175. $form_builder->add_js($js, 'editor_config_'.$params['name']);
  176. return $form_builder->create_textarea($params);
  177. }
  178. // --------------------------------------------------------------------
  179. /**
  180. * Creates a file upload field but has the option to allow multiple fields
  181. *
  182. * @access public
  183. * @param array Fields parameters
  184. * @return string
  185. */
  186. public function file($params)
  187. {
  188. $this->CI->load->helper('file');
  189. $form_builder =& $params['instance'];
  190. $file_params = $params;
  191. if (!empty($params['multiple']))
  192. {
  193. $file_params['class'] = 'multifile '.$params['class'];
  194. }
  195. $file_params['name'] = str_replace(array('[', ']', '__'), array('_', '', '_'), $params['name']);
  196. $file_params['id'] = $file_params['name'].'_upload';
  197. $str = '';
  198. $preview = '';
  199. $asset_folder = '';
  200. if ((!isset($params['display_preview']) OR $params['display_preview'] === TRUE))
  201. {
  202. // set the image preview containing class
  203. if (empty($params['img_container_styles']))
  204. {
  205. $params['img_container_styles'] = 'overflow: auto; height: 200px; width: 400px; margin-top: 5px;';
  206. }
  207. // set the styles specific to the image
  208. if (!isset($params['img_styles']))
  209. {
  210. $params['img_styles'] = 'float: left; width: 100px;';
  211. }
  212. if (empty($params['value']))
  213. {
  214. $params['value'] = '';
  215. }
  216. if (isset($params['folder']) OR isset($params['upload_path']))
  217. {
  218. if (isset($params['preview_path']))
  219. {
  220. $asset_folder = pathinfo($params['preview_path'], PATHINFO_DIRNAME);
  221. $asset_path = $params['preview_path'];
  222. }
  223. elseif (isset($params['folder']))
  224. {
  225. $asset_folder = trim($params['folder'], '/').'/';
  226. $asset_path = $asset_folder.$params['value'];
  227. $asset_path = assets_path($asset_path);
  228. }
  229. else
  230. {
  231. $asset_folder = assets_server_to_web_path($params['upload_path']).'/';
  232. $asset_path = $asset_folder.$params['value'];
  233. }
  234. if (!empty($params['replace_values']))
  235. {
  236. foreach($params['replace_values'] as $key => $val)
  237. {
  238. if (is_string($val))
  239. {
  240. $asset_path = str_replace('{'.$key.'}', $val, $asset_path);
  241. $asset_folder = str_replace('{'.$key.'}', $val, $asset_folder);
  242. }
  243. }
  244. }
  245. }
  246. $preview = '';
  247. if (!empty($asset_path) AND !empty($params['value']))
  248. {
  249. $preview .= ' ';
  250. $preview .= '<div class="asset_upload_preview deletable noclone"><a href="#'.$params['key'].'" class="asset_delete"></a><a href="'.$asset_path.'" target="_blank">';
  251. if (isset($params['is_image']) OR (!isset($params['is_image']) AND is_image_file($asset_path)))
  252. {
  253. $preview .= '<br><img src="'.$asset_path.'" style="'.$params['img_styles'].'" class="img_bg">';
  254. }
  255. else
  256. {
  257. $preview .= (isset($params['preview_label'])) ? $params['preview_label'] : $asset_path;
  258. }
  259. $preview .= '</a></div>';
  260. }
  261. }
  262. $params['after_html'] = $preview;
  263. $str .= $form_builder->create_file($file_params);
  264. if (!empty($params['display_input']))
  265. {
  266. $params['data'] = array(
  267. 'folder' => $asset_folder,
  268. );
  269. $asset_class = '';
  270. if (!isset($params['select']) OR (isset($params['select']) AND $params['select'] !== FALSE))
  271. {
  272. $asset_class = 'asset_select';
  273. }
  274. $params['class'] = (!empty($params['class'])) ? $params['class'].' '.$asset_class : $asset_class;
  275. $params['type'] = '';
  276. $str .= '<br><br>'.$form_builder->create_field($params);
  277. }
  278. else
  279. {
  280. $params['type'] = 'hidden';
  281. $str .= $form_builder->create_field($params);
  282. }
  283. // add image altering hidden field values
  284. $additional_params = array('create_thumb',
  285. 'thumb_marker',
  286. 'maintain_ratio',
  287. 'master_dim',
  288. 'width',
  289. 'height',
  290. 'resize_and_crop',
  291. 'remove_spaces',
  292. 'resize_method',
  293. 'upscale'
  294. );
  295. foreach($additional_params as $p)
  296. {
  297. if (isset($params[$p]))
  298. {
  299. $str .= $this->CI->form->hidden($file_params['name'].'_'.$p, $params[$p], 'class="noclear"');
  300. }
  301. }
  302. return $str;
  303. }
  304. // --------------------------------------------------------------------
  305. /**
  306. * Creates an asset select/upload field
  307. *
  308. * @access public
  309. * @param array Fields parameters
  310. * @return string
  311. */
  312. public function asset($params)
  313. {
  314. $this->CI->load->helper('file');
  315. $this->CI->load->helper('html');
  316. $form_builder =& $params['instance'];
  317. if (empty($params['folder']))
  318. {
  319. $params['folder'] = 'images';
  320. }
  321. if (empty($params['subfolder']))
  322. {
  323. $params['subfolder'] = '';
  324. }
  325. if (empty($params['remove_subfolder']))
  326. {
  327. $params['remove_subfolder'] = FALSE;
  328. }
  329. $asset_class = '';
  330. if (!isset($params['select']) OR (isset($params['select']) AND $params['select'] !== FALSE))
  331. {
  332. $asset_class .= ' asset_select';
  333. }
  334. if (!isset($params['upload']) OR (isset($params['upload']) AND $params['upload'] !== FALSE))
  335. {
  336. $asset_class .= ' asset_upload';
  337. }
  338. $asset_class .= ' '.$params['folder'];
  339. $params['class'] = (!empty($params['class'])) ? $params['class'].' '.$asset_class : $asset_class;
  340. // set the image preview containing class
  341. if (empty($params['img_container_styles']))
  342. {
  343. $params['img_container_styles'] = 'overflow: auto; height: 200px; width: 400px; margin-top: 5px;';
  344. }
  345. // set the styles specific to the image
  346. if (!isset($params['img_styles']))
  347. {
  348. $params['img_styles'] = 'float: left; width: 100px;';
  349. }
  350. // folders and intended contents
  351. $editable_filetypes = $this->fuel->config('editable_asset_filetypes');
  352. // set data parameters so that we can use them with the JS
  353. // set multiple and separator data attributes so can be used by javascript
  354. if (!isset($params['multiple']))
  355. {
  356. $multiple = !empty($params['multiple']) OR strpos($params['value'], ',') !== FALSE;
  357. }
  358. else
  359. {
  360. $multiple = $params['multiple'];
  361. }
  362. // set the separator based on if it is multiple lines or just a single line
  363. $separator = (isset($params['multiline']) AND $params['multiline'] === TRUE) ? "\n" : ', ';
  364. $params['data'] = array(
  365. 'multiple' => $multiple,
  366. 'separator' => $separator,
  367. 'folder' => $params['folder'],
  368. 'subfolder' => $params['subfolder'],
  369. 'remove_subfolder' => $params['remove_subfolder'],
  370. 'orig' => $params['value'],
  371. );
  372. // NO LONGER NEEDED BECAUSE IT'S DONE DYNAMICALLY WITH JAVASCRIPT
  373. // if (!empty($params['value']))
  374. // {
  375. // if (is_string($params['value']))
  376. // {
  377. // // unserialize if it is a serialized string
  378. // if (is_json_str($params['value']))
  379. // // if (is_serialized_str($params['value']))
  380. // {
  381. // $assets = json_decode($params['value'], TRUE);
  382. // //$assets = unserialize($params['value']);
  383. // }
  384. // else if ($multiple)
  385. // {
  386. // // create assoc array with key being the image and the value being either the image name again or the caption
  387. // $assets = preg_split('#\s*,\s*|\n#', $params['value']);
  388. // }
  389. // else
  390. // {
  391. // $assets = array($params['value']);
  392. // }
  393. // $preview_str = '';
  394. // // loop through all the assets and concatenate them
  395. // foreach($assets as $asset)
  396. // {
  397. // if (!empty($asset))
  398. // {
  399. // $asset_path = '';
  400. // foreach($editable_filetypes as $folder => $regex)
  401. // {
  402. // if (!is_http_path($asset))
  403. // {
  404. // if (preg_match('#'.$regex.'#i', $asset))
  405. // {
  406. // $path = trim($params['folder'], '/').'/'.$asset;
  407. // $asset_path = assets_path($path);
  408. // break;
  409. // }
  410. // }
  411. // else
  412. // {
  413. // $asset_path = $asset;
  414. // }
  415. // }
  416. // // NO LONGER NEEDED BECAUSE IT IS DONE DYNAMICALLY
  417. // if (!empty($asset_path))
  418. // {
  419. // $preview_str .= '<a href="'.$asset_path.'" target="_blank">';
  420. // if (isset($params['is_image']) OR (!isset($params['is_image']) AND is_image_file($asset)))
  421. // {
  422. // $preview_str .= '<img src="'.$asset_path.'" style="'.$params['img_styles'].'/>';
  423. // }
  424. // else
  425. // {
  426. // $preview_str .= $asset;
  427. // }
  428. // $preview_str .= '</a>';
  429. // }
  430. // }
  431. // }
  432. // }
  433. // $preview = '';
  434. // // if (!empty($preview_str))
  435. // // {
  436. // $img_container_styles = $params['img_container_styles'];
  437. // if ($multiple == FALSE AND !empty($params['img_styles']))
  438. // {
  439. // $img_container_styles = $params['img_styles'];
  440. // }
  441. // $preview = '<br /><div class="img_preview noclone" style="'.$img_container_styles.'" data-imgStyles="'.$params['img_styles'].'">';
  442. // // $preview .= $preview_str;
  443. // $preview .= '</div><div class="clear"></div>';
  444. // // }
  445. // $params['after_html'] = $preview;
  446. // }
  447. //
  448. $params['type'] = '';
  449. if ($multiple)
  450. {
  451. $process_key = (isset($params['subkey'])) ? $params['subkey'] : $params['key'];
  452. // create an array with the key being the image name and the value being the caption (if it exists... otherwise the image name is used again)
  453. $func = function($value) use ($process_key, $params) {
  454. if (is_array($value))
  455. {
  456. foreach($value as $key => $val)
  457. {
  458. if (isset($val[$process_key]))
  459. {
  460. $z = "";
  461. if (is_string($val[$process_key]))
  462. {
  463. $z = $val[$process_key];
  464. }
  465. else if (is_array($val[$process_key]) AND isset($val[$process_key][$params['name']]))
  466. {
  467. $z = $val[$process_key][$params['name']];
  468. }
  469. $z = trim($z);
  470. $assets = array();
  471. $assets_arr = preg_split("#\s*,\s*|\n#", $z);
  472. if (is_string($val[$process_key]))
  473. {
  474. if (count($assets_arr) > 1)
  475. {
  476. $value[$key][$process_key] = json_encode($assets_arr);
  477. }
  478. else
  479. {
  480. $value[$key][$process_key] = $z;
  481. }
  482. }
  483. else if (is_array($val[$process_key]) AND isset($val[$process_key][$params['name']]))
  484. {
  485. if (count($assets_arr) > 1)
  486. {
  487. $value[$key][$process_key][$params['name']] = json_encode($assets_arr);
  488. }
  489. else
  490. {
  491. $value[$key][$process_key][$params['name']] = $z;
  492. }
  493. }
  494. }
  495. }
  496. return $value;
  497. }
  498. else
  499. {
  500. $value = trim($value);
  501. $assets_arr = preg_split("#\s*,\s*|\n#", $value);
  502. if (count($assets_arr) > 1)
  503. {
  504. return json_encode($assets_arr);
  505. }
  506. else
  507. {
  508. return $value;
  509. }
  510. }
  511. };
  512. $form_builder->set_post_process($params['key'], $func);
  513. }
  514. // unserialize value if it's serialized
  515. //$value = (is_serialized_str($params['value'])) ? unserialize($params['value']) : $params['value'];
  516. $value = (is_string($params['value']) AND is_json_str($params['value'])) ? json_decode($params['value'], TRUE) : $params['value'];
  517. if (is_array($value))
  518. {
  519. $params['value'] = '';
  520. foreach($value as $key => $val)
  521. {
  522. if (!empty($val))
  523. {
  524. $params['value'] .= $val.$separator;
  525. }
  526. }
  527. }
  528. $params['value'] = trim($params['value'], ",\n ");
  529. //$params['comment'] = 'Add a caption value to your image by inserting a colon after the image name and then enter your caption like so: my_img.jpg:My caption goes here.';
  530. // data params
  531. $data_params['asset_folder'] = $params['folder'];
  532. $data_params['subfolder'] = (isset($params['subfolder'])) ? $params['subfolder'] : '';
  533. $data_params['userfile_file_name'] = (isset($params['file_name'])) ? $params['file_name'] : '';
  534. $data_params['overwrite'] = (isset($params['overwrite'])) ? (bool)$params['overwrite'] : TRUE;
  535. $data_params['unzip'] = (isset($params['unzip'])) ? (bool)$params['unzip'] : TRUE;
  536. $data_params['create_thumb'] = (isset($params['create_thumb'])) ? (bool)$params['create_thumb'] : FALSE;
  537. $data_params['maintain_ratio'] = (isset($params['maintain_ratio'])) ? (bool)$params['maintain_ratio'] : FALSE;
  538. $data_params['width'] = (isset($params['width'])) ? (int)$params['width'] : '';
  539. $data_params['height'] = (isset($params['height'])) ? (int)$params['height'] : '';
  540. $data_params['master_dim'] = (isset($params['master_dim'])) ? $params['master_dim'] : '';
  541. $data_params['resize_and_crop'] = (isset($params['resize_and_crop'])) ? $params['resize_and_crop'] : '';
  542. $data_params['resize_method'] = (isset($params['resize_method'])) ? $params['resize_method'] : 'maintain_ratio';
  543. $data_params['hide_options'] = (isset($params['hide_options'])) ? (bool)$params['hide_options'] : FALSE;
  544. $data_params['accept'] = (isset($params['accept'])) ? $params['accept'] : '';
  545. $data_params['multiple'] = (isset($params['multiple'])) ? (bool)$params['multiple'] : FALSE;
  546. $data_params['remove_subfolder'] = (isset($params['remove_subfolder'])) ? (bool)$params['remove_subfolder'] : FALSE;
  547. $data_params['upscale'] = (isset($params['upscale'])) ? (bool)$params['upscale'] : TRUE;
  548. if (isset($params['hide_image_options']))
  549. {
  550. $data_params['hide_image_options'] = (isset($params['hide_image_options'])) ? (bool)$params['hide_image_options'] : FALSE;
  551. }
  552. else if (!isset($params['hide_image_options']) AND !preg_match('#^images#', $params['folder']))
  553. {
  554. $data_params['hide_image_options'] = TRUE;
  555. }
  556. $params['data']['params'] = http_build_query($data_params);
  557. if (!empty($params['multiline']))
  558. {
  559. $params['class'] = 'no_editor '.$params['class'];
  560. if (empty($params['style']))
  561. {
  562. $params['style'] = 'float: left; width: 400px; height: 60px';
  563. }
  564. $str = $form_builder->create_textarea($params);
  565. }
  566. else
  567. {
  568. $str = $form_builder->create_text($params);
  569. }
  570. $preview = '';
  571. $img_container_styles = $params['img_container_styles'];
  572. if ($multiple == FALSE AND !empty($params['img_styles']))
  573. {
  574. $img_container_styles = $params['img_styles'];
  575. }
  576. $preview = '<br /><div class="img_preview" style="'.$img_container_styles.'" data-imgstyles="'.$params['img_styles'].'">';
  577. $preview .= '</div><div class="clear"></div>';
  578. $str .= $preview;
  579. return $str;
  580. }
  581. // --------------------------------------------------------------------
  582. /**
  583. * Creates an inline edit field type useful for drop down selects that reference data from another module
  584. *
  585. * @access public
  586. * @param array Fields parameters
  587. * @return string
  588. */
  589. public function inline_edit($params)
  590. {
  591. $form_builder =& $params['instance'];
  592. if (!empty($params['multiple']))
  593. {
  594. $field = $this->multi($params);
  595. }
  596. else
  597. {
  598. if (!empty($params['module']))
  599. {
  600. // hackalicious... used to check for a model's module
  601. $modules = $this->CI->fuel->modules->get(NULL, FALSE);
  602. foreach($modules as $key => $mod)
  603. {
  604. $mod_name = preg_replace('#(\w+)_model$#', '$1', strtolower($mod->info('model_name')));
  605. if (strtolower($params['module']) == $mod_name)
  606. {
  607. $params['module'] = $key;
  608. break;
  609. }
  610. }
  611. if (strpos($params['module'], '/') === FALSE)
  612. {
  613. $CI =& get_instance();
  614. $module = $CI->fuel->modules->get($params['module'], FALSE);
  615. $uri = (!empty($module)) ? $module->info('module_uri') : '';
  616. }
  617. else
  618. {
  619. $uri = $params['module'];
  620. }
  621. $permission = (!empty($module)) ? $module->permission : $uri;
  622. if ($this->fuel->auth->has_permission($permission))
  623. {
  624. $inline_class = 'add_edit '.$uri;
  625. $params['class'] = (!empty($params['class'])) ? $params['class'].' '.$inline_class : $inline_class;
  626. $params['data']['module'] = $uri;
  627. $params['data']['add_params'] = (!empty($params['add_params'])) ? $params['add_params'] : '';
  628. $params['data']['fields'] = (!empty($params['fields'])) ? $params['fields'] : '';
  629. }
  630. }
  631. $field = $form_builder->create_select($params);
  632. }
  633. return $field;
  634. }
  635. // --------------------------------------------------------------------
  636. /**
  637. * Creates a linked field which will use another fields value as the basis for it's own and will usually apply some sort of transform (e.g. url_title, lowercase, etc)
  638. *
  639. * @access public
  640. * @param array Fields parameters
  641. * @return string
  642. */
  643. public function linked($params)
  644. {
  645. $form_builder =& $params['instance'];
  646. $str = '';
  647. if (!empty($params['linked_to']))
  648. {
  649. $linked_class = 'linked';
  650. $params['class'] = (!empty($params['class'])) ? $params['class'].' '.$linked_class : $linked_class;
  651. $fields = $form_builder->fields();
  652. $linked_to_field = (is_array($params['linked_to'])) ? key($params['linked_to']) : $params['linked_to'];
  653. if ($form_builder->is_nested())
  654. {
  655. $linked_to = $fields[$linked_to_field]['key'];
  656. $linked_to_parts = explode('vars--', $linked_to);
  657. $linked_to = end($linked_to_parts);
  658. }
  659. else
  660. {
  661. $linked_to = (is_string($params['linked_to']) AND !empty($fields[$linked_to_field])) ? Form::create_id($fields[$linked_to_field]['name']) : $params['linked_to'];
  662. }
  663. unset($fields);
  664. $str .= "<div class=\"linked_info\" style=\"display: none;\">";
  665. $str .= json_encode($linked_to);
  666. $str .= "</div>\n";
  667. }
  668. $params['type'] = 'text';
  669. if (!empty($params['formatter']))
  670. {
  671. $params['data'] = array('formatter' => $params['formatter']);
  672. }
  673. $str .= $form_builder->create_text($params);
  674. return $str;
  675. }
  676. // --------------------------------------------------------------------
  677. /**
  678. * Creates a a template field type... This baby has a ton of options including repeatable and sortable fields. Get's stored as JSON string
  679. *
  680. * @access public
  681. * @param array Fields parameters
  682. * @return string
  683. */
  684. public function template($params, $return_fields = FALSE)
  685. {
  686. $this->CI->load->library('parser');
  687. $form_builder =& $params['instance'];
  688. if (!empty($params['module']) && empty($params['fields']))
  689. {
  690. $adv_module = '';
  691. $module = $params['module'];
  692. if (is_array($module))
  693. {
  694. // Advanced Module
  695. $adv_module = key($module);
  696. $module = current($module);
  697. $module_url = "{$adv_module}/{$module}";
  698. }
  699. $module_model = "{$module}_model";
  700. $this->CI->load->module_model($adv_module, $module_model);
  701. $module_form_fields = $this->CI->$module_model->form_fields();
  702. if (!empty($module_form_fields)) {
  703. $params['fields'] = $module_form_fields;
  704. }
  705. $module_data = $this->CI->$module_model->find_all_array(array('module_id' => $params['module_id']));
  706. if (!empty($module_data)) {
  707. $params['value'] = $module_data;
  708. }
  709. }
  710. $str = '';
  711. if (empty($params['fields']) AND empty($params['view']))
  712. {
  713. return $str;
  714. }
  715. // set the ID to have a placehoder that the js can handle
  716. $repeatable = (isset($params['repeatable']) AND $params['repeatable'] === TRUE) ? TRUE : FALSE;
  717. $add_extra = (isset($params['add_extra']) AND $params['add_extra'] === TRUE) ? TRUE : FALSE;
  718. $fields = array();
  719. $i = 0;
  720. if (!isset($params['depth']))
  721. {
  722. $params['depth'] = 0;
  723. }
  724. // set the value
  725. if (is_string($params['value']) AND is_json_str($params['value']))
  726. {
  727. $params['value'] = json_decode($params['value'], TRUE);
  728. }
  729. // set maximum limit
  730. if (!isset($params['max']))
  731. {
  732. $params['max'] = NULL;
  733. }
  734. // set minimum limit
  735. if (!isset($params['min']))
  736. {
  737. $params['min'] = NULL;
  738. }
  739. if (!is_array($params['value']))
  740. {
  741. $params['value'] = array();
  742. }
  743. if ($params['value'] == '')
  744. {
  745. $params['value'] = array();
  746. }
  747. $num = ($add_extra) ? count($params['value']) + 1 : count($params['value']);
  748. if (isset($params['min']) AND $num < $params['min'])
  749. {
  750. $num = $params['min'];
  751. }
  752. if (!isset($params['display_sub_label']))
  753. {
  754. $params['display_sub_label'] = TRUE;
  755. }
  756. if (!isset($params['removeable']))
  757. {
  758. $params['removeable'] = TRUE;
  759. }
  760. if ($num == 0) $num = 1;
  761. $_f = array();
  762. if (empty($params['fields']))
  763. {
  764. $params['fields'] = array();
  765. }
  766. for ($i = 0; $i < $num; $i++)
  767. {
  768. $value = (isset($params['value'][$i])) ? $params['value'][$i] : $params['value'];
  769. foreach($params['fields'] as $key => $field)
  770. {
  771. if (!is_array($field))
  772. {
  773. continue;
  774. }
  775. if (isset($field['type']) AND $field['type'] == 'checkbox')
  776. {
  777. $checked_value = (!empty($field['value'])) ? $field['value'] : 1;
  778. $field['checked'] = (!empty($value[$key]) AND $value[$key] == $checked_value) ? TRUE : FALSE;
  779. }
  780. if (!empty($value[$key]))
  781. {
  782. $field['value'] = $value[$key];
  783. }
  784. else if (empty($field['value']))
  785. {
  786. $field['value'] = '';
  787. }
  788. // Sorry... template can only be nested once... which should be all you need
  789. if (isset($field['type']) AND $field['type'] == 'template' AND $params['depth'] > 1)
  790. {
  791. continue;
  792. }
  793. if (empty($field['label']))
  794. {
  795. if ($lang = $form_builder->label_lang($key))
  796. {
  797. $field['label'] = $lang;
  798. }
  799. else
  800. {
  801. $field['label'] = ucfirst(str_replace('_', ' ', $key));
  802. }
  803. }
  804. if ($repeatable)
  805. {
  806. $field_name_key = (!empty($form_builder->name_array)) ? 'name' : 'orig_name';
  807. $index = (!isset($params['index'])) ? $i : $params['index'];
  808. // set file name field types to not use array syntax for name so they can be processed automagically
  809. // if (isset($field['type']) AND $field['type'] == 'file')
  810. // {
  811. // $field['name'] = $params[$field_name_key].'_'.$index.'_'.$key;
  812. // }
  813. // else
  814. // {
  815. // $field['name'] = $params[$field_name_key].'['.$index.']['.$key.']';
  816. // }
  817. $field['name'] = $params[$field_name_key].'['.$index.']['.$key.']';
  818. // set the key to be the same of the parent... so post processing will work
  819. $field['key'] = $params['key'];
  820. $field['subkey'] = $key;
  821. $field['depth'] = $params['depth'] + 1;
  822. $depth_css_class = ' field_depth_'.$params['depth'];
  823. $field['class'] = (!empty($field['class'])) ? $field['class'].' '.$depth_css_class : $depth_css_class;
  824. // set placeholders in field ids for javascript to translate... must be last occurence of the digit
  825. $field['id'] = preg_replace('#([-_a-zA-Z0-9\[\]]+)\[\d+\](\[[-_a-zA-Z0-9]+\])$#U', '$1[{index}]$2', $field['name']);
  826. $field['id'] = $field['name'];
  827. $field['id'] = Form::create_id($field['id']);
  828. $field['display_label'] = $params['display_sub_label'];
  829. $field['data']['orig_name'] = $params['name'];
  830. $field['data']['index'] = $index;
  831. $field['data']['key'] = $key;
  832. $field['data']['field_name'] = $params['key'];
  833. if (empty($field['replace_values']))
  834. {
  835. $field['replace_values'] = $value;
  836. }
  837. // need IDS for some plugins like CKEditor... not sure yet how to clone an element with a different ID
  838. //$field['id'] = FALSE;
  839. $_f[$i][$key] = $field;
  840. $fields[$i][$key] = $form_builder->create_field($field);
  841. $fields[$i]['__index__'] = $i;
  842. $fields[$i]['__num__'] = $i + 1;
  843. $fields[$i]['__title__'] = (isset($params['title_field']) AND !empty($value[$params['title_field']]) AND is_string($value[$params['title_field']])) ? strip_tags($value[$params['title_field']]) : '';
  844. }
  845. else
  846. {
  847. if (empty($params['ignore_name_array']))
  848. {
  849. if (!empty($form_builder->name_array))
  850. {
  851. $field['name'] = $params['name'].'['.$key.']';
  852. }
  853. else
  854. {
  855. $field['name'] = $params['orig_name'].'['.$key.']';
  856. }
  857. }
  858. $field['display_label'] = $params['display_sub_label'];
  859. $_f[$key] = $field;
  860. $fields[$key] = $form_builder->create_field($field);
  861. }
  862. }
  863. }
  864. // if just return FIELDs then do it...
  865. if ($return_fields OR !empty($params['return_fields']))
  866. {
  867. return $fields;
  868. }
  869. $vars = !empty($params['vars']) ? $params['vars'] : array();
  870. $vars['values'] = $params['value'];
  871. $vars['fields_config'] = $params['fields'];
  872. if ($repeatable)
  873. {
  874. $vars['fields'] = $fields;
  875. }
  876. else
  877. {
  878. $vars = array_merge($vars, $fields);
  879. }
  880. // must set $_POST parameter below or else the post_process won't run the serialization'
  881. if (!isset($_POST[$params['key']]))
  882. {
  883. $_POST[$params['key']] = '';
  884. }
  885. if (!empty($params['serialize']))
  886. {
  887. $func = function($value) use ($params) {
  888. $CI =& get_instance();
  889. $val = $CI->input->post($params['key']);
  890. if (isset($_POST[$params['key']]) AND is_array($val))
  891. {
  892. //return serialize($val); // issues with multibyte characters
  893. // foreach($_POST["'.$params['key'].'"] as $key => $val)
  894. // {
  895. // $CI->form_builder->post_process_field_values($val);
  896. // }
  897. return json_encode($val);
  898. }
  899. else
  900. {
  901. $_POST[$params['key']] = "";
  902. return "";
  903. }
  904. };
  905. $form_builder->set_post_process($params['key'], $func);
  906. }
  907. if (empty($params['template']) AND !empty($params['view']))
  908. {
  909. $module = 'app';
  910. if (is_array($params['view']))
  911. {
  912. $module = key($params['view']);
  913. $view = current($params['view']);
  914. }
  915. else
  916. {
  917. $view = $params['view'];
  918. }
  919. $str = $this->CI->load->module_view($module, $view, $vars, TRUE, '__TEMPLATE_FIELD__');
  920. }
  921. else if (!empty($params['template']))
  922. {
  923. $str = $params['template'];
  924. }
  925. else
  926. {
  927. $form_params['init'] = (!empty($params['form_builder_params'])) ? $params['form_builder_params'] : array();
  928. // auto fill properties from parent form builder
  929. $init = array('name_array', 'name_prefix');
  930. foreach($init as $in)
  931. {
  932. if (!isset($form_params['init'][$in]))
  933. {
  934. $form_params['init'][$in] = $form_builder->$in;
  935. }
  936. }
  937. if ($repeatable)
  938. {
  939. $container_class = array('repeatable_container');
  940. $container_class[] = (!empty($params['depth'])) ? ' child' : '';
  941. if (!empty($params['container_class']))
  942. {
  943. if (is_array($params['container_class']))
  944. {
  945. $container_class = $container_class + $params['container_class'];
  946. }
  947. else
  948. {
  949. $container_class[] = $params['container_class'];
  950. }
  951. }
  952. $container_class[] = (!empty($params['condensed']) AND $params['condensed']) ? 'repeatable_container_condensed' : '';
  953. $container_class[] = (!empty($params['non_sortable']) AND $params['non_sortable']) ? 'non_sortable' : '';
  954. $dblclick = (!empty($params['dblclick'])) ? $params['dblclick'] : 0;
  955. $init_display = (!empty($params['init_display'])) ? $params['init_display'] : '';
  956. $title_field = (!empty($params['title_field'])) ? $params['title_field'] : '';
  957. $str .= '<div class="'.implode(' ', $container_class).'" data-depth="'.$params['depth'].'" data-max="'.$params['max'].'" data-min="'.$params['min'].'" data-dblclick="'.$dblclick.'" data-init_display="'.$init_display.'" data-title_field="'.$title_field.'" data-removeable="'.$params['removeable'].'">';
  958. $i = 0;
  959. foreach($_f as $k => $f)
  960. {
  961. $heading_tag = 'h3';
  962. foreach($f as $kk => $ff)
  963. {
  964. if (isset($ff['type']) AND $ff['type'] == 'section')
  965. {
  966. if (!empty($ff['tag'])) $heading_tag = $ff['tag'];
  967. $value = $form_builder->simple_field_value($ff);
  968. if (isset($params['title_field'], $f[$params['title_field']]))
  969. {
  970. if (is_array($f[$params['title_field']]['value']))
  971. {
  972. // specific to block field types
  973. if (isset($f[$params['title_field']]['value']['block_name']))
  974. {
  975. $header_value = $f[$params['title_field']]['value']['block_name'];
  976. }
  977. else
  978. {
  979. $header_value = current($f[$params['title_field']]['value']);
  980. }
  981. }
  982. else
  983. {
  984. $header_value = $f[$params['title_field']]['value'];
  985. }
  986. if (is_string($header_value))
  987. {
  988. $heading = str_replace('{__title__}', $header_value, $value);
  989. }
  990. }
  991. else
  992. {
  993. $heading = $value;
  994. }
  995. unset($f[$kk]);
  996. //$f[$kk]['label'] = $heading;
  997. }
  998. }
  999. $form_params['fields'] = $f;
  1000. $form_params['value'] = '';
  1001. if ( ! empty($params['value'][$k]))
  1002. {
  1003. $form_params['value'] = $params['value'][$k];
  1004. }
  1005. $form_obj = $form_builder->create_nested($form_params, TRUE);
  1006. $form = $form_obj->render();
  1007. $css_class = ($i > 0) ? ' noclone' : '';
  1008. if (!empty($params['float']) AND $params['depth'] == 0)
  1009. {
  1010. $css_class = ' float_left';
  1011. }
  1012. $depth_suffix = ($params['depth'] > 0) ? '_'.$params['depth'] : '';
  1013. $style = (!empty($params['style'])) ? ' style="'.$params['style'].'"' : '';
  1014. $str .= '<div class="repeatable'.$css_class.'" data-index="'.$i.'"'.$style.'>';
  1015. $sortable_class = (!empty($params['non_sortable'])) ? 'nonsortable' : 'grabber';
  1016. $str .= '<'.$heading_tag.' class="'.$sortable_class.'" title="'.lang('tooltip_dbl_click_to_open').'">';
  1017. if (!empty($heading))
  1018. {
  1019. $str .= '<span class="title'.$depth_suffix.'">'.$heading.'</span>';
  1020. }
  1021. $str .= '</'.$heading_tag.'>';
  1022. $str .= '<div class="repeatable_content">';
  1023. $str .= $form;
  1024. $str .= '</div>';
  1025. $str .= '</div>';
  1026. $i++;
  1027. }
  1028. if (empty($params['float']))
  1029. {
  1030. $str .= '<div class="clear"></div></div>';
  1031. }
  1032. }
  1033. else
  1034. {
  1035. $form_params['fields'] = $_f;
  1036. $form = $form_builder->create_nested($form_params);
  1037. $str .= $form;
  1038. $str .= '</div></div>';
  1039. }
  1040. return $str;
  1041. }
  1042. // parse the string
  1043. if (!isset($params['parse']) OR $params['parse'] === TRUE)
  1044. {
  1045. if ($vars['fields_config'] instanceof Base_model_fields)
  1046. {
  1047. $vars['fields_config'] = $vars['fields_config']->get_fields();
  1048. }
  1049. $str = parse_template_syntax($str, $vars, 'ci');
  1050. }
  1051. return $str;
  1052. }
  1053. // --------------------------------------------------------------------
  1054. /**
  1055. * Creates a currency field type
  1056. *
  1057. * @access public
  1058. * @param array Fields parameters
  1059. * @return string
  1060. */
  1061. public function currency($params)
  1062. {
  1063. $this->CI->load->helper('format');
  1064. $form_builder =& $params['instance'];
  1065. if ( ! empty($params['value']))
  1066. {
  1067. $params['value'] = str_replace(',', '', $params['value']);
  1068. }
  1069. if (empty($params['size']))
  1070. {
  1071. $params['size'] = '10';
  1072. }
  1073. if (empty($params['max_length']))
  1074. {
  1075. $params['max_length'] = '10';
  1076. }
  1077. if (empty($params['separator']))
  1078. {
  1079. $params['separator'] = ',';
  1080. }
  1081. if (empty($params['decimal']))
  1082. {
  1083. $params['decimal'] = '.';
  1084. }
  1085. $currency = (isset($params['currency'])) ? $params['currency'] : '$';
  1086. $data_vals = array('separator', 'decimal', 'grouping', 'min', 'max');
  1087. if (!isset($params['data']))
  1088. {
  1089. $params['data'] = array();
  1090. }
  1091. foreach($data_vals as $val)
  1092. {
  1093. if (isset($params[$val]))
  1094. {
  1095. $params['data'][$val] = $params[$val];
  1096. }
  1097. }
  1098. $params['class'] = (!empty($params['class'])) ? 'currency '.$params['class'] : 'currency';
  1099. $params['type'] = 'text';
  1100. if (!isset($_POST[$params['key']]))
  1101. {
  1102. $_POST[$params['key']] = '';
  1103. }
  1104. $process_key = (isset($params['subkey'])) ? $params['subkey'] : $params['key'];
  1105. // unformat number
  1106. $func = function($value) use ($process_key, $params) {
  1107. // check if it's a nested form
  1108. if (is_array($value))
  1109. {
  1110. foreach($value as $key => $val)
  1111. {
  1112. if (isset($val[$process_key]))
  1113. {
  1114. $z = "";
  1115. if (is_string($val[$process_key]))
  1116. {
  1117. $z = $val[$process_key];
  1118. }
  1119. else if (is_array($val[$process_key]) AND isset($val[$process_key][$params['name']]))
  1120. {
  1121. $z = $val[$process_key][$params['name']];
  1122. }
  1123. if ($z == "")
  1124. {
  1125. $val = NULL;
  1126. }
  1127. else
  1128. {
  1129. $value_parts = explode($params['decimal'], $z);
  1130. $curval = current($value_parts);
  1131. $decimal = "00";
  1132. if (count($value_parts) > 1)
  1133. {
  1134. $decimal = end($value_parts);
  1135. }
  1136. $curval = str_replace($params['separator'], "", $curval);
  1137. $curval = (float) $curval.".".$decimal;
  1138. }
  1139. if (is_string($val[$process_key]))
  1140. {
  1141. $value[$key][$process_key] = $curval;
  1142. }
  1143. else if (is_array($val[$process_key]) AND isset($val[$process_key][$params['name']]))
  1144. {
  1145. $value[$key][$process_key][$params['name']] = $curval;
  1146. }
  1147. }
  1148. }
  1149. return $value;
  1150. }
  1151. else
  1152. {
  1153. if ($value == "")
  1154. {
  1155. $value = NULL;
  1156. }
  1157. else
  1158. {
  1159. $value_parts = explode($params['decimal'], $value);
  1160. $value = current($value_parts);
  1161. $decimal = "00";
  1162. if (count($value_parts) > 1)
  1163. {
  1164. $decimal = end($value_parts);
  1165. }
  1166. $value = str_replace($params['separator'], "", $value);
  1167. $value = (float) $value.".".$decimal;
  1168. }
  1169. return $value;
  1170. }
  1171. };
  1172. $form_builder->set_post_process($params['key'], $func);
  1173. // preformat the currency
  1174. $params['value'] = (!isset($params['value'])) ? NULL : currency($params['value'], '', TRUE, $params['decimal'], $params['separator']);
  1175. //$params['value'] = (!isset($params['value'])) ? NULL : $params['value'];
  1176. // set data values for jquery plugin to use
  1177. return $currency.' '.$form_builder->create_text($params);
  1178. }
  1179. // --------------------------------------------------------------------
  1180. /**
  1181. * Creates a state field type
  1182. *
  1183. * @access public
  1184. * @param array Fields parameters
  1185. * @return string
  1186. */
  1187. public function state($params)
  1188. {
  1189. include(APPPATH.'config/states.php');
  1190. $form_builder =& $params['instance'];
  1191. if (isset($params['format']))
  1192. {
  1193. if (strtolower($params['format']) == 'short')
  1194. {
  1195. $abbrs = array_keys($states);
  1196. $states = array_combine($abbrs, $abbrs);
  1197. }
  1198. else if (strtolower($params['format']) == 'long')
  1199. {
  1200. $names = array_values($states);
  1201. $states = array_combine($names, $names);
  1202. }
  1203. }
  1204. if (empty($params['options']))
  1205. {
  1206. $params['options'] = $states;
  1207. }
  1208. // set data values for jquery plugin to use
  1209. return $form_builder->create_select($params);
  1210. }
  1211. // --------------------------------------------------------------------
  1212. /**
  1213. * Creates a slug field type that has the url_title function applied to it
  1214. *
  1215. * @access public
  1216. * @param array Fields parameters
  1217. * @return string
  1218. */
  1219. public function slug($params)
  1220. {
  1221. $form_builder =& $params['instance'];
  1222. // assume a default is either name or title
  1223. if (empty($params['linked_to']))
  1224. {
  1225. $fields = $form_builder->fields();
  1226. if (isset($fields['title']))
  1227. {
  1228. $params['linked_to'] = 'title';
  1229. }
  1230. else if (isset($fields['name']))
  1231. {
  1232. $params['linked_to'] = 'name';
  1233. }
  1234. else
  1235. {
  1236. $params['linked_to'] = NULL;
  1237. }
  1238. }
  1239. $process_key = (isset($params['subkey'])) ? $params['subkey'] : $params['key'];
  1240. $func = function($value) use ($process_key, $params) {
  1241. if (is_array($value))
  1242. {
  1243. foreach($value as $key => $val)
  1244. {
  1245. if (isset($val[$params['linked_to']]))
  1246. {
  1247. $v = url_title($val[$params['linked_to']], "dash", TRUE);
  1248. if (is_string($val[$process_key]))
  1249. {
  1250. $value[$key][$process_key] = $v;
  1251. }
  1252. else if (is_array($val[$process_key]) AND isset($val[$process_key][$params['name']]))
  1253. {
  1254. $value[$key][$process_key][$params['name']] = $v;
  1255. }
  1256. }
  1257. }
  1258. return $value;
  1259. }
  1260. else
  1261. {
  1262. $CI =& get_instance();
  1263. $slug_val = $CI->input->post($params['name']);
  1264. $linked_value = $CI->input->post($params['linked_to']);
  1265. if ( ! $slug_val AND $linked_value)
  1266. {
  1267. return url_title($linked_value, "dash", TRUE);
  1268. }
  1269. return $slug_val;
  1270. }
  1271. };
  1272. $form_builder->set_post_process($params['key'], $func);
  1273. $params['type'] = 'text';
  1274. // set data values for jquery plugin to use
  1275. return $this->linked($params);
  1276. }
  1277. // --------------------------------------------------------------------
  1278. /**
  1279. * Creates a text area that will automatically create unordered list items off of each new line
  1280. *
  1281. * @access public
  1282. * @param array Fields parameters
  1283. * @return string
  1284. */
  1285. public function list_items($params)
  1286. {
  1287. $form_builder =& $params['instance'];
  1288. // ugly... just strips the whitespace on multilines ... http://stackoverflow.com/questions/1655159/php-how-to-trim-each-line-in-a-heredoc-long-string
  1289. $params['value'] = trim_multiline(strip_tags($params['value']));
  1290. $output_class = (!empty($params['output_class'])) ? 'class="'.$params['output_class'].'"' : '';
  1291. $process_key = (isset($params['subkey'])) ? $params['subkey'] : $params['key'];
  1292. $list_type = (!empty($params['list_type']) AND $params['list_type'] == 'ol') ? 'ol' : 'ul';
  1293. $func = function($value) use ($process_key, $params, $output_class, $list_type){
  1294. if (is_array($value))
  1295. {
  1296. foreach($value as $key => $val)
  1297. {
  1298. if (isset($val[$process_key]))
  1299. {
  1300. $z = "";
  1301. if (is_string($val[$process_key]))
  1302. {
  1303. $z = $val[$process_key];
  1304. }
  1305. else if (is_array($val[$process_key]) AND isset($val[$process_key][$params['name']]))
  1306. {
  1307. $z = $val[$process_key][$params['name']];
  1308. }
  1309. $lis = explode("\n", $z);
  1310. $lis = array_map("trim", $lis);
  1311. $newval = $list_type($lis, $output_class);
  1312. if (is_string($val[$process_key]))
  1313. {
  1314. $value[$key][$process_key] = $newval;
  1315. }
  1316. else if (is_array($val[$process_key]) AND isset($val[$process_key][$params['name']]))
  1317. {
  1318. $value[$key][$process_key][$params['name']] = $newval;
  1319. }
  1320. }
  1321. }
  1322. return $value;
  1323. }
  1324. else
  1325. {
  1326. $lis = explode("\n", $value);
  1327. $lis = array_map("trim", $lis);
  1328. return $list_type($lis, $output_class);
  1329. }
  1330. };
  1331. $form_builder->set_post_process($params['key'], $func);
  1332. $params['class'] = 'no_editor';
  1333. return $form_builder->create_textarea($params);
  1334. }
  1335. // --------------------------------------------------------------------
  1336. /**
  1337. * Creates the multi select input for the form
  1338. *
  1339. * @access public
  1340. * @param array Fields parameters
  1341. * @return string
  1342. */
  1343. public function multi($params)
  1344. {
  1345. $form_builder =& $params['instance'];
  1346. $defaults = array(
  1347. 'sorting' => NULL,
  1348. 'options' => array(),
  1349. 'mode' => NULL,
  1350. 'model' => NULL,
  1351. 'model_params' => NULL,
  1352. 'wrapper_tag' => 'span',// for checkboxes
  1353. 'wrapper_class' => 'multi_field',
  1354. 'module' => NULL,
  1355. 'spacer' => "&nbsp;&nbsp;&nbsp;",
  1356. );
  1357. $params = $form_builder->normalize_params($params, $defaults);
  1358. // force to multi if sorting is selected
  1359. if ($params['sorting'] === TRUE)
  1360. {
  1361. $params['mode'] = 'multi';
  1362. }
  1363. // grab options from a model if a model is specified
  1364. if (!empty($params['model']))
  1365. {
  1366. $model_params = (!empty($params['model_params'])) ? $params['model_params'] : array();
  1367. $params['options'] = $form_builder->options_from_model($params['model'], $model_params);
  1368. }
  1369. if (!empty($params['module']))
  1370. {
  1371. // // hackalicious... used to check for a model's module
  1372. $modules = $this->CI->fuel->modules->get(NULL, FALSE);
  1373. foreach($modules as $key => $mod)
  1374. {
  1375. $mod_name = preg_replace('#(\w+)_model$#', '$1', strtolower($mod->info('model_name')));
  1376. if (strtolower($params['module']) == $mod_name)
  1377. {
  1378. $params['module'] = $key;
  1379. break;
  1380. }
  1381. }
  1382. if (strpos($params['module'], '/') === FALSE)
  1383. {
  1384. $module = $this->CI->fuel->modules->get($params['module'], FALSE);
  1385. $uri = (!empty($module)) ? $module->info('module_uri') : '';
  1386. }
  1387. else
  1388. {
  1389. $uri = $params['module'];
  1390. }
  1391. // check for modules with fuel_ prefix
  1392. $permission = (!empty($module)) ? $module->permission : $uri;
  1393. if (!empty($params['module']) AND $this->fuel->auth->has_permission($permission))
  1394. {
  1395. $inline_class = 'add_edit '.$uri;
  1396. $params['class'] = (!empty($params['class'])) ? $params['class'].' '.$inline_class : $inline_class;
  1397. $params['data']['module'] = $uri;
  1398. $params['data']['add_params'] = (!empty($params['add_params'])) ? $params['add_params'] : '';
  1399. $params['data']['fields'] = (!empty($params['fields'])) ? $params['fields'] : '';
  1400. }
  1401. }
  1402. $str = '';
  1403. $mode = (!empty($params['mode'])) ? $params['mode'] : $form_builder->multi_select_mode;
  1404. if ($mode == 'checkbox' OR ($mode == 'auto' AND (isset($params['options']) AND count($params['options']) <= 5)))
  1405. {
  1406. $value = (!empty($params['value'])) ? (array)$params['value'] : array();
  1407. $params['name'] = $params['name'].'[]';
  1408. $i = 1;
  1409. if (!empty($params['options']))
  1410. {
  1411. if (!empty($inline_class))
  1412. {
  1413. $data_value = (is_array($params['value'])) ? implode(',', $params['value']) : $params['value'];
  1414. $str .= ' <span class="'.$inline_class.'" data-module="'.$uri.'" id="'.$params['orig_name'].'" data-value="'.$data_value.'">';
  1415. }
  1416. foreach($params['options'] as $key => $val)
  1417. {
  1418. $str .= '<'.$params['wrapper_tag'].' class="'.$params['wrapper_class'].'">';
  1419. $attrs = array(
  1420. 'readonly' => $params['readonly'],
  1421. 'disabled' => $params['disabled'],
  1422. 'id' => Form::create_id($params['name']).$i,
  1423. 'style' => '' // to overwrite any input width styles
  1424. );
  1425. $v = (!empty($params['equalize_key_value']) AND is_int($key)) ? $val : $key;
  1426. if (in_array($v, $value))
  1427. {
  1428. $attrs['checked'] = 'checked';
  1429. }
  1430. $str .= $form_builder->form->checkbox($params['name'], $v, $attrs);
  1431. $label = ($lang = $form_builder->label_lang($attrs['id'])) ? $lang : $val;
  1432. $enum_params = array('label' => $label, 'name' => $attrs['id']);
  1433. $str .= ' '.$form_builder->create_label($enum_params);
  1434. $str .= $params['spacer'];
  1435. $str .= '</'.$params['wrapper_tag'].'>';
  1436. $i++;
  1437. }
  1438. if (!empty($inline_class))
  1439. {
  1440. $str .= '</span>';
  1441. }
  1442. }
  1443. }
  1444. else
  1445. {
  1446. $params['multiple'] = TRUE;
  1447. $str .= $form_builder->create_select($params);
  1448. if (!empty($params['sorting']))
  1449. {
  1450. if ($params['sorting'] === TRUE AND is_array($params['value']))
  1451. {
  1452. $params['sorting'] = $params['value'];
  1453. }
  1454. $sorting_params['name'] = 'sorting_'.$params['orig_name'];
  1455. $sorting_params['value'] = rawurlencode(json_encode($params['sorting']));
  1456. $sorting_params['class'] = 'sorting';
  1457. $str .= $form_builder->create_hidden($sorting_params);
  1458. }
  1459. }
  1460. // needed to detect when none exists
  1461. $exists_params['name'] = 'exists_'.$params['orig_name'];
  1462. $exists_params['value'] = 1;
  1463. $exists_params['type'] = 'hidden';
  1464. $exists_params['ignore_representative'] = TRUE;
  1465. $str .= $form_builder->create_field($exists_params);
  1466. return $str;
  1467. }
  1468. // --------------------------------------------------------------------
  1469. /**
  1470. * Creates the url input select
  1471. *
  1472. * @access public
  1473. * @param array Fields parameters
  1474. * @return string
  1475. */
  1476. public function url($params)
  1477. {
  1478. $form_builder =& $params['instance'];
  1479. $url_class = 'url_select';
  1480. $params['class'] = (!empty($params['class'])) ? $params['class'].' '.$url_class : $url_class;
  1481. if (empty($params['size']))
  1482. {
  1483. $params['size'] = '90';
  1484. }
  1485. $data = array();
  1486. if (isset($params['input']))
  1487. {
  1488. $data['input'] = $params['input'];
  1489. }
  1490. if (isset($params['target']))
  1491. {
  1492. $data['target'] = $params['target'];
  1493. }
  1494. if (isset($params['title']))
  1495. {
  1496. $data['title'] = $params['title'];
  1497. }
  1498. if (isset($params['pdfs']))
  1499. {
  1500. $data['pdfs'] = 1;
  1501. }
  1502. if (isset($params['filter']))
  1503. {
  1504. $data['filter'] = rawurlencode($params['filter']);
  1505. }
  1506. if (!empty($params['data']))
  1507. {
  1508. $params['data'] = array_merge($params['data'], $data);
  1509. }
  1510. else
  1511. {
  1512. $params['data'] = $data;
  1513. }
  1514. $params['type'] = 'text';
  1515. return $form_builder->create_text($params);
  1516. }
  1517. // --------------------------------------------------------------------
  1518. /**
  1519. * Creates a dropdown select of languages identified in the MY_fuel.php file
  1520. *
  1521. * @access public
  1522. * @param array Fields parameters
  1523. * @return string
  1524. */
  1525. public function language($params)
  1526. {
  1527. $form_builder =& $params['instance'];
  1528. if ((isset($this->CI->language_col) AND $params['key'] == $this->CI->language_col)
  1529. OR
  1530. (!isset($this->CI->language_col) AND $params['key'] == 'language')
  1531. )
  1532. {
  1533. $params['type'] = 'select';
  1534. $params['options'] = $this->CI->fuel->language->options();
  1535. }
  1536. return $form_builder->create_select($params);
  1537. }
  1538. // --------------------------------------------------------------------
  1539. /**
  1540. * Creates a key / value associative array
  1541. *
  1542. * @access public
  1543. * @param array Fields parameters
  1544. * @return string
  1545. */
  1546. public function keyval($params)
  1547. {
  1548. $form_builder =& $params['instance'];
  1549. if (!isset($params['delimiter']))
  1550. {
  1551. $params['delimiter'] = ":";
  1552. }
  1553. if (!isset($params['row_delimiter']))
  1554. {
  1555. $params['row_delimiter'] = "\r\n|\n|,";
  1556. }
  1557. if (!isset($params['allow_numeric_indexes']))
  1558. {
  1559. $params['allow_numeric_indexes'] = FALSE;
  1560. }
  1561. if (!isset($params['allow_empty_values']))
  1562. {
  1563. $params['allow_empty_values'] = FALSE;
  1564. }
  1565. $row_delimiter = "\s*".$params['row_delimiter']."\s*";
  1566. $split_delimiter = "\s*".$params['delimiter']."\s*";
  1567. $process_key = (isset($params['subkey'])) ? $params['subkey'] : $params['key'];
  1568. // create an array with the key being the image name and the value being the caption (if it exists... otherwise the image name is used again)
  1569. $func = function($value) use ($process_key, $params, $row_delimiter, $split_delimiter) {
  1570. if (is_array($value))
  1571. {

Large files files are truncated, but you can click here to view the full file