PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/git_src/wp-content/plugins/custom-content-type-manager/fields/directory.php

https://bitbucket.org/murtuza88/carolina-home-stone
PHP | 275 lines | 160 code | 41 blank | 74 comment | 15 complexity | e3ed3435b6f42defced5c3b07a70e9a5 MD5 | raw file
  1. <?php
  2. /**
  3. * CCTM_directory
  4. *
  5. * Lists the contents of a directory (an optionally all sub-dirs) for selection in a dropdown.
  6. *
  7. * @package CCTM_FormElement
  8. */
  9. class CCTM_directory extends CCTM_FormElement
  10. {
  11. public $props = array(
  12. 'label' => '',
  13. 'name' => '',
  14. 'description' => '',
  15. 'class' => '',
  16. 'extra' => '',
  17. 'default_value' => '',
  18. 'required' => '',
  19. 'source_dir' => '',
  20. 'pattern' => '',
  21. 'traverse_dirs' => 0,
  22. // 'type' => '', // auto-populated: the name of the class, minus the CCTM_ prefix.
  23. );
  24. //------------------------------------------------------------------------------
  25. /**
  26. * This function provides a name for this type of field. This should return plain
  27. * text (no HTML). The returned value should be localized using the __() function.
  28. *
  29. * @return string
  30. */
  31. public function get_name() {
  32. return __('Directory', CCTM_TXTDOMAIN);
  33. }
  34. //------------------------------------------------------------------------------
  35. /**
  36. * This function gives a description of this type of field so users will know
  37. * whether or not they want to add this type of field to their custom content
  38. * type. The returned value should be localized using the __() function.
  39. *
  40. * @return string text description
  41. */
  42. public function get_description() {
  43. return __('Lists the contents of a directory (an optionally all sub-dirs) for selection in a dropdown. Output is relative to the defined source directory.', CCTM_TXTDOMAIN);
  44. }
  45. //------------------------------------------------------------------------------
  46. /**
  47. * This function should return the URL where users can read more information about
  48. * the type of field that they want to add to their post_type. The string may
  49. * be localized using __() if necessary (e.g. for language-specific pages)
  50. *
  51. * @return string e.g. http://www.yoursite.com/some/page.html
  52. */
  53. public function get_url() {
  54. return 'http://code.google.com/p/wordpress-custom-content-type-manager/wiki/Directory';
  55. }
  56. //------------------------------------------------------------------------------
  57. /**
  58. * Get an instance of this field (used when you are creating or editing a post
  59. * that uses this type of custom field).
  60. *
  61. * @param string $current_value of the field for the current post
  62. * @return string
  63. */
  64. public function get_edit_field_instance($current_value) {
  65. // Format for multi-select
  66. if ($this->is_repeatable) {
  67. $current_value = $this->get_value($current_value, 'to_array');
  68. $optiontpl = CCTM::load_tpl(
  69. array('fields/options/'.$this->name.'.tpl'
  70. , 'fields/options/_option.tpl'
  71. )
  72. );
  73. $fieldtpl = CCTM::load_tpl(
  74. array('fields/elements/'.$this->name.'.tpl'
  75. , 'fields/elements/_multiselect.tpl'
  76. , 'fields/elements/_default.tpl'
  77. )
  78. );
  79. $wrappertpl = CCTM::load_tpl(
  80. array('fields/wrappers/'.$this->name.'.tpl'
  81. , 'fields/wrappers/_multiselect.tpl'
  82. , 'fields/wrappers/_default.tpl'
  83. )
  84. );
  85. }
  86. // For regular dropdowns
  87. else {
  88. $current_value = $this->get_value($current_value, 'to_string');
  89. $optiontpl = CCTM::load_tpl(
  90. array('fields/options/'.$this->name.'.tpl'
  91. , 'fields/options/_option.tpl'
  92. )
  93. );
  94. $fieldtpl = CCTM::load_tpl(
  95. array('fields/elements/'.$this->name.'.tpl'
  96. , 'fields/elements/_dropdown.tpl'
  97. , 'fields/elements/_default.tpl'
  98. )
  99. );
  100. $wrappertpl = CCTM::load_tpl(
  101. array('fields/wrappers/'.$this->name.'.tpl'
  102. , 'fields/wrappers/_default.tpl'
  103. )
  104. );
  105. }
  106. // Get the options. This currently is not skinnable.
  107. $this->all_options = '';
  108. if (!isset($this->required) || !$this->required) {
  109. $hash['value'] = '';
  110. $hash['option'] = '';
  111. $this->all_options .= CCTM::parse($optiontpl, $hash); // '<option value="">'.__('Pick One').'</option>';
  112. }
  113. // Substitutions
  114. $this->source_dir = str_replace('[+ABSPATH+]', ABSPATH, $this->source_dir);
  115. $this->source_dir = preg_replace('#/$#','',$this->source_dir); // strip trailing slash
  116. // Generate the regex pattern
  117. $exts = explode(',',$this->pattern);
  118. $exts = array_map('trim', $exts);
  119. $exts = array_map('preg_quote', $exts);
  120. $pattern = implode('|',$exts);
  121. // Get the files
  122. $options = array();
  123. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->source_dir)) as $filename) {
  124. if (preg_match('/('.$pattern.')$/i',$filename)) {
  125. // Make the stored file relative to the source_dir
  126. $options[] = preg_replace('#^'.$this->source_dir.'/#','',$filename);
  127. }
  128. }
  129. $this->options = $options;
  130. $opt_cnt = count($this->options);
  131. $i = 1;
  132. // Populate the options
  133. foreach ( $this->options as $o ) {
  134. //die(print_r($o, true));
  135. $hash = $this->get_props();
  136. $hash['option'] = $o;
  137. $hash['value'] = $o;
  138. $hash['is_checked'] = '';
  139. if ($this->is_repeatable) {
  140. if ( in_array(trim($hash['value']), $current_value )) {
  141. $hash['is_selected'] = 'selected="selected"';
  142. }
  143. }
  144. else {
  145. if ( trim($current_value) == trim($hash['value']) ) {
  146. $hash['is_selected'] = 'selected="selected"';
  147. }
  148. }
  149. $hash['i'] = $i;
  150. $hash['id'] = $this->name;
  151. $this->all_options .= CCTM::parse($optiontpl, $hash);
  152. }
  153. // Populate the values (i.e. properties) of this field
  154. $this->id = str_replace(array('[',']',' '), '_', $this->name);
  155. // wrap
  156. $this->content = CCTM::parse($fieldtpl, $this->get_props());
  157. return CCTM::parse($wrappertpl, $this->get_props());
  158. }
  159. //------------------------------------------------------------------------------
  160. /**
  161. * Note that the HTML in $option_html should match the JavaScript version of
  162. * the same HTML in js/dropdown.js (see the append_dropdown_option() function).
  163. * I couldn't think of a clean way to do this, but the fundamental problem is
  164. * that both PHP and JS need to draw the same HTML into this form:
  165. * PHP draws it when an existing definition is *edited*, whereas JS draws it
  166. * when you dynamically *create* new dropdown options.
  167. *
  168. * @param array $def nested array of existing definition.
  169. * @return string
  170. */
  171. public function get_edit_field_definition($def) {
  172. // Standard
  173. $out = $this->format_standard_fields($def);
  174. // Options
  175. $is_checked = '';
  176. if (isset($def['traverse_dirs']) && $def['traverse_dirs']==1) {
  177. $is_checked = 'checked="checked"';
  178. }
  179. // Source Directory
  180. $out .= '
  181. <div class="postbox">
  182. <div class="handlediv" title="Click to toggle"><br /></div>
  183. <h3 class="hndle"><span>'. __('Options', CCTM_TXTDOMAIN).'</span></h3>
  184. <div class="inside">';
  185. $out .= '<div class="'.self::wrapper_css_class .'" id="source_dir_wrapper">
  186. <label for="source_dir" class="cctm_label cctm_text_label" id="source_dir_label">'
  187. . __('Source Directory', CCTM_TXTDOMAIN) .
  188. '</label>
  189. <input type="text" name="source_dir" class="cctm_text_short" size="50" id="source_dir" value="'.htmlspecialchars($def['source_dir']) .'"/><span class="cctm_description">'.
  190. __('Full path to a directory without the trailing slash, e.g. <code>/home/my_user/dir</code>. Use <code>[+ABSPATH+]</code> as a placeholder for your WordPress root directory.',CCTM_TXTDOMAIN).'</span>
  191. </div>';
  192. // pattern
  193. $out .= '<div class="'.self::wrapper_css_class .'" id="pattern_wrapper">
  194. <label for="pattern" class="cctm_label cctm_text_label" id="pattern_label">'
  195. . __('Extensions', CCTM_TXTDOMAIN) .
  196. '</label>
  197. <input type="text" name="pattern" class="cctm_text_short" id="pattern" value="'.htmlspecialchars($def['pattern']) .'"/> <span class="cctm_description">'
  198. . __('File extensions you want returned. Use comas to separate possible matches, e.g. <code>.jpg,.jpeg</code>. The case does not matter.',CCTM_TXTDOMAIN) .'</span>
  199. </div>';
  200. // Traverse Directories?
  201. $out .= '<div class="'.self::wrapper_css_class .'" id="traverse_dirs_wrapper">
  202. <label for="traverse_dirs" class="cctm_label cctm_checkbox_label" id="traverse_dirs_label">'
  203. . __('Traverse Directories?', CCTM_TXTDOMAIN) .
  204. '</label>
  205. <br />
  206. <input type="checkbox" name="traverse_dirs" class="cctm_checkbox" id="traverse_dirs" value="1" '. $is_checked.'/> <span>'.__('When checked, the contents of sub-directories will also be listed.',CCTM_TXTDOMAIN).'</span>
  207. </div>';
  208. $out .= '</div><!-- /inside -->
  209. </div><!-- /postbox -->';
  210. // Validations / Required
  211. $out .= $this->format_validators($def,false);
  212. // Output Filter
  213. $out .= $this->format_available_output_filters($def);
  214. return $out;
  215. }
  216. //------------------------------------------------------------------------------
  217. /**
  218. * Shows the selected directory and extensions
  219. */
  220. public function get_options_desc() {
  221. $out = '';
  222. if (!empty($this->props['source_dir'])) {
  223. $out .= __('Source Directory', CCTM_TXTDOMAIN) . ': '. $this->props['source_dir'] .'<br/>';
  224. $out .= __('Extensions', CCTM_TXTDOMAIN) . ': '. $this->props['pattern'] .'<br/>';
  225. $is_checked = '';
  226. if (isset($def['traverse_dirs']) && $def['traverse_dirs']==1) {
  227. $is_checked = 'checked="checked"';
  228. }
  229. $out .= __('Traverse Directories?', CCTM_TXTDOMAIN) . sprintf(' <input type="checkbox" disabled="disabled" %s/>',$is_checked);
  230. }
  231. return $out;
  232. }
  233. }
  234. /*EOF*/