PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/fields/pick.php

https://github.com/ElmsPark/pods
PHP | 1209 lines | 828 code | 211 blank | 170 comment | 229 complexity | 57feaa4e7f0ac699db1beca35190b5da MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * @package Pods\Fields
  4. */
  5. class PodsField_Pick extends PodsField {
  6. /**
  7. * Field Type Group
  8. *
  9. * @var string
  10. * @since 2.0.0
  11. */
  12. public static $group = 'Relationships / Media';
  13. /**
  14. * Field Type Identifier
  15. *
  16. * @var string
  17. * @since 2.0.0
  18. */
  19. public static $type = 'pick';
  20. /**
  21. * Field Type Label
  22. *
  23. * @var string
  24. * @since 2.0.0
  25. */
  26. public static $label = 'Relationship';
  27. /**
  28. * Available Related Objects
  29. *
  30. * @var array
  31. * @since 2.3.0
  32. */
  33. private static $related_objects = array();
  34. /**
  35. * Setup related objects list
  36. *
  37. * @since 2.0.0
  38. */
  39. public function __construct () {
  40. if ( empty( self::$related_objects ) ) {
  41. $related_objects = get_transient( 'pods_related_objects' );
  42. if ( !empty( $related_objects ) )
  43. self::$related_objects = $related_objects;
  44. else {
  45. // Custom
  46. self::$related_objects[ 'custom-simple' ] = array(
  47. 'label' => __( 'Simple (custom defined list)', 'pods' ),
  48. 'group' => __( 'Custom', 'pods' ),
  49. 'simple' => true
  50. );
  51. // Pods
  52. // @todo Upgrade should convert to proper type selections (pods-pod_name >> post_type-pod_name
  53. $_pods = pods_api()->load_pods( array( 'type' => 'pod' ) );
  54. foreach ( $_pods as $k => $pod ) {
  55. self::$related_objects[ 'pod-' . $pod[ 'name' ] ] = array(
  56. 'label' => $pod[ 'label' ] . ' (' . $pod[ 'name' ] . ')',
  57. 'group' => __( 'Pods', 'pods' )
  58. );
  59. }
  60. // Post Types
  61. $post_types = get_post_types();
  62. asort( $post_types );
  63. $ignore = array( 'attachment', 'revision', 'nav_menu_item' );
  64. foreach ( $post_types as $post_type => $label ) {
  65. if ( in_array( $post_type, $ignore ) || empty( $post_type ) || 0 === strpos( $post_type, '_pods_' ) ) {
  66. unset( $post_types[ $post_type ] );
  67. continue;
  68. }
  69. $post_type = get_post_type_object( $post_type );
  70. self::$related_objects[ 'post_type-' . $post_type->name ] = array(
  71. 'label' => $post_type->label,
  72. 'group' => __( 'Post Types', 'pods' )
  73. );
  74. }
  75. // Taxonomies
  76. $taxonomies = get_taxonomies();
  77. asort( $taxonomies );
  78. $ignore = array( 'nav_menu', 'post_format' );
  79. foreach ( $taxonomies as $taxonomy => $label ) {
  80. if ( in_array( $taxonomy, $ignore ) || empty( $taxonomy ) )
  81. continue;
  82. $taxonomy = get_taxonomy( $taxonomy );
  83. self::$related_objects[ 'taxonomy-' . $taxonomy->name ] = array(
  84. 'label' => $taxonomy->label,
  85. 'group' => __( 'Taxonomies', 'pods' )
  86. );
  87. }
  88. // Other WP Objects
  89. self::$related_objects[ 'user' ] = array(
  90. 'label' => __( 'Users', 'pods' ),
  91. 'group' => __( 'Other WP Objects', 'pods' )
  92. );
  93. self::$related_objects[ 'role' ] = array(
  94. 'label' => __( 'User Roles', 'pods' ),
  95. 'group' => __( 'Other WP Objects', 'pods' ),
  96. 'simple' => true
  97. );
  98. self::$related_objects[ 'comment' ] = array(
  99. 'label' => __( 'Comments', 'pods' ),
  100. 'group' => __( 'Other WP Objects', 'pods' )
  101. );
  102. self::$related_objects[ 'image-size' ] = array(
  103. 'label' => __( 'Image Sizes', 'pods' ),
  104. 'group' => __( 'Other WP Objects', 'pods' ),
  105. 'simple' => true
  106. );
  107. self::$related_objects[ 'nav_menu' ] = array(
  108. 'label' => __( 'Navigation Menus', 'pods' ),
  109. 'group' => __( 'Other WP Objects', 'pods' )
  110. );
  111. self::$related_objects[ 'post_format' ] = array(
  112. 'label' => __( 'Post Formats', 'pods' ),
  113. 'group' => __( 'Other WP Objects', 'pods' )
  114. );
  115. self::$related_objects[ 'post-status' ] = array(
  116. 'label' => __( 'Post Status', 'pods' ),
  117. 'group' => __( 'Other WP Objects', 'pods' ),
  118. 'simple' => true
  119. );
  120. self::$related_objects[ 'sidebar' ] = array(
  121. 'label' => __( 'Sidebars', 'pods' ),
  122. 'group' => __( 'Other WP Objects', 'pods' ),
  123. 'simple' => true
  124. );
  125. // Advanced Objects
  126. self::$related_objects[ 'table' ] = array(
  127. 'label' => __( 'Database Table', 'pods' ),
  128. 'group' => __( 'Advanced Objects', 'pods' )
  129. );
  130. self::$related_objects[ 'site' ] = array(
  131. 'label' => __( 'Multisite Sites', 'pods' ),
  132. 'group' => __( 'Advanced Objects', 'pods' )
  133. );
  134. self::$related_objects[ 'network' ] = array(
  135. 'label' => __( 'Multisite Networks', 'pods' ),
  136. 'group' => __( 'Advanced Objects', 'pods' )
  137. );
  138. self::$related_objects[ 'post-types' ] = array(
  139. 'label' => __( 'Post Types', 'pods' ),
  140. 'group' => __( 'Advanced Objects', 'pods' ),
  141. 'simple' => true
  142. );
  143. self::$related_objects[ 'taxonomies' ] = array(
  144. 'label' => __( 'Taxonomies', 'pods' ),
  145. 'group' => __( 'Advanced Objects', 'pods' ),
  146. 'simple' => true
  147. );
  148. set_transient( 'pods_related_objects', self::$related_objects );
  149. }
  150. }
  151. }
  152. /**
  153. * Add options and set defaults to
  154. *
  155. * @return array
  156. *
  157. * @since 2.0.0
  158. */
  159. public function options () {
  160. $options = array(
  161. 'pick_format_type' => array(
  162. 'label' => __( 'Selection Type', 'pods' ),
  163. 'help' => __( 'help', 'pods' ),
  164. 'default' => 'single',
  165. 'type' => 'pick',
  166. 'data' => array(
  167. 'single' => __( 'Single Select', 'pods' ),
  168. 'multi' => __( 'Multiple Select', 'pods' )
  169. ),
  170. 'dependency' => true
  171. ),
  172. 'pick_format_single' => array(
  173. 'label' => __( 'Format', 'pods' ),
  174. 'help' => __( 'help', 'pods' ),
  175. 'depends-on' => array( 'pick_format_type' => 'single' ),
  176. 'default' => 'dropdown',
  177. 'type' => 'pick',
  178. 'data' => apply_filters(
  179. 'pods_form_ui_field_pick_format_single_options',
  180. array(
  181. 'dropdown' => __( 'Drop Down', 'pods' ),
  182. 'radio' => __( 'Radio Buttons', 'pods' ),
  183. 'autocomplete' => __( 'Autocomplete', 'pods' )
  184. ) + ( ( pods_developer() ) ? array( 'flexible' => __( 'Flexible', 'pods' ) ) : array() )
  185. ),
  186. 'dependency' => true
  187. ),
  188. 'pick_format_multi' => array(
  189. 'label' => __( 'Format', 'pods' ),
  190. 'help' => __( 'help', 'pods' ),
  191. 'depends-on' => array( 'pick_format_type' => 'multi' ),
  192. 'default' => 'checkbox',
  193. 'type' => 'pick',
  194. 'data' => apply_filters(
  195. 'pods_form_ui_field_pick_format_multi_options',
  196. array(
  197. 'checkbox' => __( 'Checkboxes', 'pods' ),
  198. 'multiselect' => __( 'Multi Select', 'pods' ),
  199. 'autocomplete' => __( 'Autocomplete', 'pods' )
  200. ) + ( ( pods_developer() ) ? array( 'flexible' => __( 'Flexible', 'pods' ) ) : array() )
  201. ),
  202. 'dependency' => true
  203. ),
  204. 'pick_limit' => array(
  205. 'label' => __( 'Selection Limit', 'pods' ),
  206. 'help' => __( 'help', 'pods' ),
  207. 'depends-on' => array( 'pick_format_type' => 'multi' ),
  208. 'default' => 0,
  209. 'type' => 'number'
  210. ),
  211. 'pick_table_id' => array(
  212. 'label' => __( 'Table ID Column', 'pods' ),
  213. 'help' => __( 'You must provide the ID column name for the table, this will be used to keep track of the relationship', 'pods' ),
  214. 'depends-on' => array( 'pick_object' => 'table' ),
  215. 'required' => 1,
  216. 'default' => '',
  217. 'type' => 'text'
  218. ),
  219. 'pick_table_index' => array(
  220. 'label' => __( 'Table Index Column', 'pods' ),
  221. 'help' => __( 'You must provide the index column name for the table, this may optionally also be the ID column name', 'pods' ),
  222. 'depends-on' => array( 'pick_object' => 'table' ),
  223. 'required' => 1,
  224. 'default' => '',
  225. 'type' => 'text'
  226. ),
  227. 'pick_display' => array(
  228. 'label' => __( 'Display Field in Selection List', 'pods' ),
  229. 'help' => __( 'Provide the name of a field on the related object to reference, example: {@post_title}', 'pods' ),
  230. 'excludes-on' => array(
  231. 'pick_object' => array_merge(
  232. array( 'site', 'network' ),
  233. self::simple_objects()
  234. )
  235. ),
  236. 'default' => '',
  237. 'type' => 'text'
  238. ),
  239. 'pick_where' => array(
  240. 'label' => __( 'Customized <em>WHERE</em>', 'pods' ),
  241. 'help' => __( 'help', 'pods' ),
  242. 'excludes-on' => array(
  243. 'pick_object' => array_merge(
  244. array( 'site', 'network' ),
  245. self::simple_objects()
  246. )
  247. ),
  248. 'default' => '',
  249. 'type' => 'text'
  250. ),
  251. 'pick_orderby' => array(
  252. 'label' => __( 'Customized <em>ORDER BY</em>', 'pods' ),
  253. 'help' => __( 'help', 'pods' ),
  254. 'excludes-on' => array(
  255. 'pick_object' => array_merge(
  256. array( 'site', 'network' ),
  257. self::simple_objects()
  258. )
  259. ),
  260. 'default' => '',
  261. 'type' => 'text'
  262. ),
  263. 'pick_groupby' => array(
  264. 'label' => __( 'Customized <em>GROUP BY</em>', 'pods' ),
  265. 'help' => __( 'help', 'pods' ),
  266. 'excludes-on' => array(
  267. 'pick_object' => array_merge(
  268. array( 'site', 'network' ),
  269. self::simple_objects()
  270. )
  271. ),
  272. 'default' => '',
  273. 'type' => 'text'
  274. )
  275. /*,
  276. 'pick_size' => array(
  277. 'label' => __( 'Field Size', 'pods' ),
  278. 'default' => 'medium',
  279. 'type' => 'pick',
  280. 'data' => array(
  281. 'small' => __( 'Small', 'pods' ),
  282. 'medium' => __( 'Medium', 'pods' ),
  283. 'large' => __( 'Large', 'pods' )
  284. )
  285. )*/
  286. );
  287. return $options;
  288. }
  289. /**
  290. * Register a related object
  291. *
  292. * @param string $name Object name
  293. * @param string $label Object label
  294. * @param array $options Object options
  295. *
  296. * @return array|boolean Object array or false if unsuccessful
  297. * @since 2.3.0
  298. */
  299. public function register_related_object ( $name, $label, $options = null ) {
  300. if ( empty( $name ) || empty( $label ) )
  301. return false;
  302. $related_object = array(
  303. 'label' => $label,
  304. 'group' => $label,
  305. 'simple' => false,
  306. 'data' => array()
  307. //'data_callback' => false,
  308. //'value_to_label_callback' => false,
  309. //'simple_value_callback' => false
  310. );
  311. $related_object = array_merge( $related_object, $options );
  312. self::$related_objects[ $name ] = $related_object;
  313. return true;
  314. }
  315. /**
  316. * Return available related objects
  317. *
  318. * @return array Field selection array
  319. * @since 2.3.0
  320. */
  321. public function related_objects () {
  322. $related_objects = array();
  323. foreach ( self::$related_objects as $related_object ) {
  324. if ( !isset( $related_objects[ $related_object[ 'group' ] ] ) )
  325. $related_objects[ $related_object[ 'group' ] ] = array();
  326. $related_objects[ $related_object[ 'group' ] ][] = $related_object[ 'label' ];
  327. }
  328. return (array) apply_filters( 'pods_form_ui_field_pick_related_objects', $related_objects );
  329. }
  330. /**
  331. * Return available simple object names
  332. *
  333. * @return array Simple object names
  334. * @since 2.3.0
  335. */
  336. public function simple_objects () {
  337. $simple_objects = array();
  338. foreach ( self::$related_objects as $object => $related_object ) {
  339. if ( !isset( $related_object[ 'simple' ] ) || !$related_object[ 'simple' ] )
  340. continue;
  341. $simple_objects[] = $object;
  342. }
  343. return (array) apply_filters( 'pods_form_ui_field_pick_simple_objects', $simple_objects );
  344. }
  345. /**
  346. * Define the current field's schema for DB table storage
  347. *
  348. * @param array $options
  349. *
  350. * @return array
  351. * @since 2.0.0
  352. */
  353. public function schema ( $options = null ) {
  354. $schema = false;
  355. if ( in_array( pods_var( 'pick_object', $options ), array( 'custom-simple', 'role', 'post-types', 'taxonomies' ) ) )
  356. $schema = 'LONGTEXT';
  357. return $schema;
  358. }
  359. /**
  360. * Change the way the value of the field is displayed with Pods::get
  361. *
  362. * @param mixed $value
  363. * @param string $name
  364. * @param array $options
  365. * @param array $fields
  366. * @param array $pod
  367. * @param int $id
  368. *
  369. * @since 2.0.0
  370. */
  371. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
  372. $fields = null;
  373. if ( is_object( $pod ) && isset( $pod->fields ) )
  374. $fields = $pod->fields;
  375. elseif ( is_array( $pod ) && isset( $pod[ 'fields' ] ) )
  376. $fields = $pod[ 'fields' ];
  377. return pods_serial_comma( $value, $name, $fields );
  378. }
  379. /**
  380. * Customize output of the form field
  381. *
  382. * @param string $name
  383. * @param mixed $value
  384. * @param array $options
  385. * @param array $pod
  386. * @param int $id
  387. *
  388. * @since 2.0.0
  389. */
  390. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
  391. global $wpdb;
  392. $options = (array) $options;
  393. $form_field_type = PodsForm::$field_type;
  394. $options[ 'grouped' ] = 1;
  395. $options[ 'table_info' ] = array();
  396. $custom = pods_var_raw( 'pick_custom', $options, false );
  397. $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id );
  398. $ajax = false;
  399. if ( ( 'custom-simple' != pods_var( 'pick_object', $options ) || empty( $custom ) ) && '' != pods_var( 'pick_object', $options, '', null, true ) ) {
  400. $autocomplete = false;
  401. if ( 'single' == pods_var( 'pick_format_type', $options ) && 'autocomplete' == pods_var( 'pick_format_single', $options ) )
  402. $autocomplete = true;
  403. elseif ( 'multi' == pods_var( 'pick_format_type', $options ) && 'autocomplete' == pods_var( 'pick_format_multi', $options ) )
  404. $autocomplete = true;
  405. $params[ 'limit' ] = -1;
  406. if ( $autocomplete )
  407. $params[ 'limit' ] = apply_filters( 'pods_form_ui_field_pick_autocomplete_limit', 30, $name, $value, $options, $pod, $id );
  408. $ajax = true;
  409. }
  410. if ( 'single' == pods_var( 'pick_format_type', $options ) ) {
  411. if ( 'dropdown' == pods_var( 'pick_format_single', $options ) )
  412. $field_type = 'select';
  413. elseif ( 'radio' == pods_var( 'pick_format_single', $options ) )
  414. $field_type = 'radio';
  415. elseif ( 'autocomplete' == pods_var( 'pick_format_single', $options ) )
  416. $field_type = 'select2';
  417. else {
  418. // Support custom integration
  419. do_action( 'pods_form_ui_field_pick_input_' . pods_var( 'pick_format_type', $options ) . '_' . pods_var( 'pick_format_single', $options ), $name, $value, $options, $pod, $id );
  420. do_action( 'pods_form_ui_field_pick_input', pods_var( 'pick_format_type', $options ), $name, $value, $options, $pod, $id );
  421. return;
  422. }
  423. }
  424. elseif ( 'multi' == pods_var( 'pick_format_type', $options ) ) {
  425. if ( 'checkbox' == pods_var( 'pick_format_multi', $options ) )
  426. $field_type = 'checkbox';
  427. elseif ( 'multiselect' == pods_var( 'pick_format_multi', $options ) )
  428. $field_type = 'select';
  429. elseif ( 'autocomplete' == pods_var( 'pick_format_multi', $options ) )
  430. $field_type = 'select2';
  431. else {
  432. // Support custom integration
  433. do_action( 'pods_form_ui_field_pick_input_' . pods_var( 'pick_format_type', $options ) . '_' . pods_var( 'pick_format_multi', $options ), $name, $value, $options, $pod, $id );
  434. do_action( 'pods_form_ui_field_pick_input', pods_var( 'pick_format_type', $options ), $name, $value, $options, $pod, $id );
  435. return;
  436. }
  437. }
  438. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
  439. }
  440. /**
  441. * Get the data from the field
  442. *
  443. * @param string $name The name of the field
  444. * @param string|array $value The value of the field
  445. * @param array $options
  446. * @param array $pod
  447. * @param int $id
  448. *
  449. * @return array Array of possible field data
  450. *
  451. * @since 2.0.0
  452. */
  453. public function data ( $name, $value = null, $options = null, $pod = null, $id = null ) {
  454. $data = array( '' => pods_var_raw( 'pick_select_text', $options, __( '-- Select One --', 'pods' ), null, true ) );
  455. if ( 'single' != pods_var( 'pick_format_type', $options ) || 'dropdown' != pods_var( 'pick_format_single', $options ) )
  456. $data = array();
  457. if ( isset( $options[ 'data' ] ) && !empty( $options[ 'data' ] ) )
  458. $data = (array) $options[ 'data' ];
  459. $custom = trim( pods_var_raw( 'pick_custom', $options, '' ) );
  460. $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $name, $value, $options, $pod, $id );
  461. if ( 'custom-simple' == pods_var( 'pick_object', $options ) && !empty( $custom ) ) {
  462. if ( !is_array( $custom ) ) {
  463. $custom = explode( "\n", $custom );
  464. foreach ( $custom as $custom_value ) {
  465. $custom_label = explode( '|', $custom_value );
  466. if ( empty( $custom_label ) )
  467. continue;
  468. if ( 1 == count( $custom_label ) )
  469. $custom_label = $custom_value;
  470. else {
  471. $custom_value = $custom_label[ 0 ];
  472. $custom_label = $custom_label[ 1 ];
  473. }
  474. $data[ $custom_value ] = $custom_label;
  475. }
  476. }
  477. else {
  478. foreach ( $custom as $custom_value => $custom_label ) {
  479. $data[ $custom_value ] = $custom_label;
  480. }
  481. }
  482. }
  483. elseif ( '' != pods_var( 'pick_object', $options, '' ) && array() == pods_var_raw( 'data', $options, array(), null, true ) ) {
  484. if ( 'post-status' == $options[ 'pick_object' ] ) {
  485. $post_stati = get_post_stati( array(), 'objects' );
  486. foreach ( $post_stati as $post_status ) {
  487. $data[ $post_status->name ] = $post_status->label;
  488. }
  489. }
  490. elseif ( 'role' == $options[ 'pick_object' ] ) {
  491. global $wp_roles;
  492. foreach ( $wp_roles->role_objects as $key => $role ) {
  493. $data[ $key ] = $wp_roles->role_names[ $key ];
  494. }
  495. }
  496. elseif ( 'sidebar' == $options[ 'pick_object' ] ) {
  497. global $wp_registered_sidebars;
  498. if ( !empty( $wp_registered_sidebars ) ) {
  499. foreach ( $wp_registered_sidebars as $sidebar ) {
  500. $data[ $sidebar[ 'id' ] ] = $sidebar[ 'name' ];
  501. }
  502. }
  503. }
  504. elseif ( 'image-size' == $options[ 'pick_object' ] ) {
  505. $image_sizes = get_intermediate_image_sizes();
  506. foreach ( $image_sizes as $image_size ) {
  507. $data[ $image_size ] = ucwords( str_replace( '-', ' ', $image_size ) );
  508. }
  509. }
  510. elseif ( 'post-types' == $options[ 'pick_object' ] ) {
  511. $post_types = get_post_types( array(), 'objects' );
  512. $ignore = array( 'revision', 'nav_menu_item' );
  513. foreach ( $post_types as $post_type ) {
  514. if ( in_array( $post_type->name, $ignore ) || 0 === strpos( $post_type->name, '_pods_' ) )
  515. continue;
  516. $data[ $post_type->name ] = $post_type->label;
  517. }
  518. }
  519. elseif ( 'taxonomies' == $options[ 'pick_object' ] ) {
  520. $taxonomies = get_taxonomies( array(), 'objects' );
  521. $ignore = array( 'nav_menu', 'post_format' );
  522. foreach ( $taxonomies as $taxonomy ) {
  523. if ( in_array( $taxonomy->name, $ignore ) )
  524. continue;
  525. $data[ $taxonomy->name ] = $taxonomy->label;
  526. }
  527. }
  528. elseif ( isset( self::$related_objects[ $options[ 'pick_object' ] ] ) ) {
  529. if ( !empty( self::$related_objects[ $options[ 'pick_object' ][ 'data' ] ] ) )
  530. $data = self::$related_objects[ $options[ 'pick_object' ] ][ 'data' ];
  531. elseif ( isset( self::$related_objects[ $options[ 'pick_object' ] ][ 'data_callback' ] ) && is_callable( self::$related_objects[ $options[ 'pick_object' ] ][ 'data_callback' ] ) ) {
  532. $data = call_user_func_array(
  533. self::$related_objects[ $options[ 'pick_object' ] ][ 'data_callback' ],
  534. array( compact( array( 'name', 'value', 'options', 'pod', 'id' ) ) )
  535. );
  536. }
  537. }
  538. else {
  539. $pick_val = pods_var( 'pick_val', $options );
  540. if ( 'table' == pods_var( 'pick_object', $options ) )
  541. $pick_val = pods_var( 'pick_table', $options, $pick_val, null, true );
  542. $options[ 'table_info' ] = pods_api()->get_table_info( pods_var( 'pick_object', $options ), $pick_val, null, null, $options );
  543. $search_data = pods_data();
  544. $search_data->table = $options[ 'table_info' ][ 'table' ];
  545. $search_data->join = $options[ 'table_info' ][ 'join' ];
  546. $search_data->field_id = $options[ 'table_info' ][ 'field_id' ];
  547. $search_data->field_index = $options[ 'table_info' ][ 'field_index' ];
  548. $search_data->where = $options[ 'table_info' ][ 'where' ];
  549. $search_data->orderby = $options[ 'table_info' ][ 'orderby' ];
  550. if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'name' ] ) ) {
  551. $search_data->pod = $options[ 'table_info' ][ 'pod' ][ 'name' ];
  552. $search_data->fields = $options[ 'table_info' ][ 'pod' ][ 'fields' ];
  553. }
  554. $params = array(
  555. 'select' => "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`",
  556. 'table' => $search_data->table,
  557. 'where' => pods_var_raw( 'pick_where', $options, (array) $options[ 'table_info' ][ 'where_default' ], null, true ),
  558. 'orderby' => pods_var_raw( 'pick_orderby', $options, null, null, true ),
  559. 'groupby' => pods_var_raw( 'pick_groupby', $options, null, null, true )
  560. );
  561. if ( in_array( $options[ 'pick_object' ], array( 'site', 'network' ) ) )
  562. $params[ 'select' ] .= ', `t`.`path`';
  563. if ( !empty( $params[ 'where' ] ) && (array) $options[ 'table_info' ][ 'where_default' ] != $params[ 'where' ] )
  564. $params[ 'where' ] = pods_evaluate_tags( $params[ 'where' ], true );
  565. /* not needed yet
  566. if ( !empty( $params[ 'orderby' ] ) )
  567. $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true );
  568. if ( !empty( $params[ 'groupby' ] ) )
  569. $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/
  570. $display = trim( pods_var( 'pick_display', $options ), ' {@}' );
  571. if ( 0 < strlen( $display ) ) {
  572. if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) ) {
  573. if ( isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ][ $display ] ) ) {
  574. $search_data->field_index = $display;
  575. $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
  576. }
  577. elseif ( isset( $options[ 'table_info' ][ 'pod' ][ 'fields' ][ $display ] ) ) {
  578. $search_data->field_index = $display;
  579. if ( 'table' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] && !in_array( $options[ 'table_info' ][ 'pod' ][ 'type' ], array( 'pod', 'table' ) ) )
  580. $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `d`.`{$search_data->field_index}`";
  581. else
  582. $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
  583. }
  584. }
  585. elseif ( isset( $options[ 'table_info' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'object_fields' ][ $display ] ) ) {
  586. $search_data->field_index = $display;
  587. $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
  588. }
  589. }
  590. $autocomplete = false;
  591. if ( 'single' == pods_var( 'pick_format_type', $options ) && 'autocomplete' == pods_var( 'pick_format_single', $options ) )
  592. $autocomplete = true;
  593. elseif ( 'multi' == pods_var( 'pick_format_type', $options ) && 'autocomplete' == pods_var( 'pick_format_multi', $options ) )
  594. $autocomplete = true;
  595. if ( $autocomplete )
  596. $params[ 'limit' ] = apply_filters( 'pods_form_ui_field_pick_autocomplete_limit', 30, $name, $value, $options, $pod, $id );
  597. $results = $search_data->select( $params );
  598. if ( !empty( $results ) && ( !$autocomplete || $search_data->total_found() <= $params[ 'limit' ] ) ) {
  599. foreach ( $results as $result ) {
  600. $result = get_object_vars( $result );
  601. $result[ $search_data->field_index ] = trim( $result[ $search_data->field_index ] );
  602. if ( in_array( $options[ 'pick_object' ], array( 'site', 'network' ) ) )
  603. $result[ $search_data->field_index ] = $result[ $search_data->field_index ] . $result[ 'path' ];
  604. elseif ( strlen( $result[ $search_data->field_index ] ) < 1 )
  605. $result[ $search_data->field_index ] = '(No Title)';
  606. $data[ $result[ $search_data->field_id ] ] = $result[ $search_data->field_index ];
  607. }
  608. }
  609. elseif ( !empty( $value ) && $autocomplete && $params[ 'limit' ] < $search_data->total_found() ) {
  610. $ids = $value;
  611. if ( is_array( $ids ) )
  612. $ids = implode( ', ', $ids );
  613. if ( is_array( $params[ 'where' ] ) )
  614. $params[ 'where' ] = implode( ' AND ', $params[ 'where' ] );
  615. if ( !empty( $params[ 'where' ] ) )
  616. $params[ 'where' ] .= ' AND ';
  617. $params[ 'where' ] .= "`t`.`{$search_data->field_id}` IN ( " . $ids . " )";
  618. $results = $search_data->select( $params );
  619. if ( !empty( $results ) ) {
  620. foreach ( $results as $result ) {
  621. $result = get_object_vars( $result );
  622. $result[ $search_data->field_index ] = trim( $result[ $search_data->field_index ] );
  623. if ( strlen( $result[ $search_data->field_index ] ) < 1 )
  624. $result[ $search_data->field_index ] = '(No Title)';
  625. $data[ $result[ $search_data->field_id ] ] = $result[ $search_data->field_index ];
  626. }
  627. }
  628. }
  629. }
  630. }
  631. $data = apply_filters( 'pods_field_pick_data', $data, $name, $value, $options, $pod, $id );
  632. return $data;
  633. }
  634. /**
  635. * Customize the Pods UI manage table column output
  636. *
  637. * @param int $id
  638. * @param mixed $value
  639. * @param string $name
  640. * @param array $options
  641. * @param array $fields
  642. * @param array $pod
  643. *
  644. * @since 2.0.0
  645. */
  646. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
  647. $value = $this->simple_value( $value, $options );
  648. return $this->display( $value, $name, $options, $pod, $id );
  649. }
  650. /**
  651. * Convert a simple value to the correct value
  652. *
  653. * @param mixed $value Value of the field
  654. * @param array $options Field options
  655. * @param boolean $raw Whether to return the raw list of keys (true) or convert to key=>value (false)
  656. */
  657. public function simple_value ( $value, $options, $raw = false ) {
  658. if ( isset( $options[ 'options' ] ) ) {
  659. $options = array_merge( $options[ 'options' ], $options );
  660. unset( $options[ 'options' ] );
  661. }
  662. if ( in_array( pods_var( 'pick_object', $options ), self::simple_objects() ) ) {
  663. $data = array();
  664. if ( 'custom-simple' == $options[ 'pick_object' ] ) {
  665. $custom = trim( pods_var_raw( 'pick_custom', $options, '' ) );
  666. $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, pods_var( 'name', $options ), $value, $options, null, null );
  667. if ( !empty( $custom ) ) {
  668. if ( !is_array( $custom ) ) {
  669. $custom = explode( "\n", $custom );
  670. foreach ( $custom as $custom_value ) {
  671. $custom_label = explode( '|', $custom_value );
  672. if ( empty( $custom_label ) )
  673. continue;
  674. if ( 1 == count( $custom_label ) )
  675. $custom_label = $custom_value;
  676. else {
  677. $custom_value = $custom_label[ 0 ];
  678. $custom_label = $custom_label[ 1 ];
  679. }
  680. $data[ $custom_value ] = $custom_label;
  681. }
  682. }
  683. else
  684. $data = $custom;
  685. }
  686. }
  687. elseif ( 'post-status' == $options[ 'pick_object' ] ) {
  688. $post_stati = get_post_stati( array(), 'objects' );
  689. foreach ( $post_stati as $post_status ) {
  690. $data[ $post_status->name ] = $post_status->label;
  691. }
  692. }
  693. elseif ( 'role' == $options[ 'pick_object' ] ) {
  694. global $wp_roles;
  695. foreach ( $wp_roles->role_objects as $key => $role ) {
  696. $data[ $key ] = $wp_roles->role_names[ $key ];
  697. }
  698. }
  699. elseif ( 'sidebar' == $options[ 'pick_object' ] ) {
  700. global $wp_registered_sidebars;
  701. if ( !empty( $wp_registered_sidebars ) ) {
  702. foreach ( $wp_registered_sidebars as $sidebar ) {
  703. $data[ $sidebar[ 'id' ] ] = $sidebar[ 'name' ];
  704. }
  705. }
  706. }
  707. elseif ( 'image-size' == $options[ 'pick_object' ] ) {
  708. $image_sizes = get_intermediate_image_sizes();
  709. foreach ( $image_sizes as $image_size ) {
  710. $data[ $image_size ] = ucwords( str_replace( '-', ' ', $image_size ) );
  711. }
  712. }
  713. elseif ( 'post-types' == $options[ 'pick_object' ] ) {
  714. $post_types = get_post_types( array(), 'objects' );
  715. $ignore = array( 'revision', 'nav_menu_item' );
  716. foreach ( $post_types as $post_type ) {
  717. if ( in_array( $post_type->name, $ignore ) || 0 === strpos( $post_type->name, '_pods_' ) )
  718. continue;
  719. $data[ $post_type->name ] = $post_type->label;
  720. }
  721. }
  722. elseif ( 'taxonomies' == $options[ 'pick_object' ] ) {
  723. $taxonomies = get_taxonomies( array(), 'objects' );
  724. $ignore = array( 'nav_menu', 'post_format' );
  725. foreach ( $taxonomies as $taxonomy ) {
  726. if ( in_array( $taxonomy->name, $ignore ) )
  727. continue;
  728. $data[ $taxonomy->name ] = $taxonomy->label;
  729. }
  730. }
  731. elseif ( isset( self::$related_objects[ $options[ 'pick_object' ] ] ) ) {
  732. if ( !empty( self::$related_objects[ $options[ 'pick_object' ][ 'data' ] ] ) )
  733. $data = self::$related_objects[ $options[ 'pick_object' ] ][ 'data' ];
  734. elseif ( isset( self::$related_objects[ $options[ 'pick_object' ] ][ 'simple_value_callback' ] ) && is_callable( self::$related_objects[ $options[ 'pick_object' ] ][ 'simple_value_callback' ] ) ) {
  735. $data = call_user_func_array(
  736. self::$related_objects[ $options[ 'pick_object' ] ][ 'simple_value_callback' ],
  737. array( compact( array( 'value', 'options', 'raw' ) ) )
  738. );
  739. }
  740. }
  741. $simple = false;
  742. $key = 0;
  743. if ( !is_array( $value ) && !empty( $value ) )
  744. $simple = @json_decode( $value, true );
  745. if ( is_array( $simple ) )
  746. $value = $simple;
  747. if ( is_array( $value ) ) {
  748. if ( !empty( $data ) ) {
  749. $val = array();
  750. foreach ( $value as $k => $v ) {
  751. if ( isset( $data[ $v ] ) ) {
  752. if ( false === $raw ) {
  753. $k = $v;
  754. $v = $data[ $v ];
  755. }
  756. $val[ $k ] = $v;
  757. }
  758. }
  759. $value = $val;
  760. }
  761. }
  762. elseif ( isset( $data[ $value ] ) && false === $raw ) {
  763. $key = $value;
  764. $value = $data[ $value ];
  765. }
  766. $single_multi = pods_var( 'pick_format_type', $options, 'single' );
  767. if ( 'multi' == $single_multi )
  768. $limit = (int) pods_var( 'pick_limit', $options, 0 );
  769. else
  770. $limit = 1;
  771. if ( is_array( $value ) && 0 < $limit ) {
  772. if ( 1 == $limit )
  773. $value = current( $value );
  774. else
  775. $value = array_slice( $value, 0, $limit, true );
  776. }
  777. elseif ( !is_array( $value ) && null !== $value && 0 < strlen( $value ) ) {
  778. if ( 1 != $limit || ( true === $raw && 'multi' == $single_multi ) ) {
  779. $value = array(
  780. $key => $value
  781. );
  782. }
  783. }
  784. }
  785. return $value;
  786. }
  787. /**
  788. * Get the label from a pick value
  789. *
  790. * @param string|array $pod An array of Pod data or Pod name
  791. * @param string|array $field An array of field data or field name
  792. * @param string|array $value The value to return label(s) for
  793. *
  794. * @return string
  795. *
  796. * @since 2.2
  797. */
  798. public function value_to_label ( $pod, $field, $value ) {
  799. if ( !is_array( $pod ) && !empty( $pod ) )
  800. $pod = pods_api()->load_pod( array( 'name' => $pod ) );
  801. if ( empty( $pod ) )
  802. return false;
  803. if ( !is_array( $field ) && !empty( $field ) )
  804. $field = pods_api()->load_field( array( 'name' => $field, 'pod' => $pod[ 'name' ] ) );
  805. if ( empty( $field ) )
  806. return false;
  807. $options = array_merge( $field[ 'options' ], $field );
  808. $custom = trim( pods_var_raw( 'pick_custom', $options, '' ) );
  809. $custom = apply_filters( 'pods_form_ui_field_pick_custom_values', $custom, $field[ 'name' ], $value, $options, $pod, 0 );
  810. $data = pods_var_raw( 'data', $options, array(), null, true );
  811. if ( 'custom-simple' == pods_var( 'pick_object', $options ) && !empty( $custom ) ) {
  812. if ( !is_array( $custom ) ) {
  813. $custom = explode( "\n", $custom );
  814. foreach ( $custom as $custom_value ) {
  815. $custom_label = explode( '|', $custom_value );
  816. if ( empty( $custom_label ) )
  817. continue;
  818. if ( 1 == count( $custom_label ) )
  819. $custom_label = $custom_value;
  820. else {
  821. $custom_value = $custom_label[ 0 ];
  822. $custom_label = $custom_label[ 1 ];
  823. }
  824. if ( $value == $custom_value ) {
  825. $data = $custom_label;
  826. break;
  827. }
  828. }
  829. }
  830. else {
  831. foreach ( $custom as $custom_value => $custom_label ) {
  832. if ( $value == $custom_value ) {
  833. $data = $custom_label;
  834. break;
  835. }
  836. }
  837. }
  838. }
  839. elseif ( '' != pods_var( 'pick_object', $options, '' ) && empty( $data ) ) {
  840. if ( 'post-status' == $options[ 'pick_object' ] ) {
  841. $post_stati = get_post_stati( array(), 'objects' );
  842. foreach ( $post_stati as $post_status ) {
  843. $data[ $post_status->name ] = $post_status->label;
  844. }
  845. }
  846. elseif ( 'role' == $options[ 'pick_object' ] ) {
  847. global $wp_roles;
  848. foreach ( $wp_roles->role_objects as $key => $role ) {
  849. $data[ $key ] = $wp_roles->role_names[ $key ];
  850. }
  851. }
  852. elseif ( 'sidebar' == $options[ 'pick_object' ] ) {
  853. global $wp_registered_sidebars;
  854. if ( !empty( $wp_registered_sidebars ) ) {
  855. foreach ( $wp_registered_sidebars as $sidebar ) {
  856. $data[ $sidebar[ 'id' ] ] = $sidebar[ 'name' ];
  857. }
  858. }
  859. }
  860. elseif ( 'image-size' == $options[ 'pick_object' ] ) {
  861. $image_sizes = get_intermediate_image_sizes();
  862. foreach ( $image_sizes as $image_size ) {
  863. $data[ $image_size ] = ucwords( str_replace( '-', ' ', $image_size ) );
  864. }
  865. }
  866. elseif ( 'post-types' == $options[ 'pick_object' ] ) {
  867. $post_types = get_post_types( array(), 'objects' );
  868. $ignore = array( 'revision', 'nav_menu_item' );
  869. foreach ( $post_types as $post_type ) {
  870. if ( in_array( $post_type->name, $ignore ) || 0 === strpos( $post_type->name, '_pods_' ) )
  871. continue;
  872. $data[ $post_type->name ] = $post_type->label;
  873. }
  874. }
  875. elseif ( 'taxonomies' == $options[ 'pick_object' ] ) {
  876. $taxonomies = get_taxonomies( array(), 'objects' );
  877. $ignore = array( 'nav_menu', 'post_format' );
  878. foreach ( $taxonomies as $taxonomy ) {
  879. if ( in_array( $taxonomy->name, $ignore ) )
  880. continue;
  881. $data[ $taxonomy->name ] = $taxonomy->label;
  882. }
  883. }
  884. elseif ( isset( self::$related_objects[ $options[ 'pick_object' ] ] ) ) {
  885. if ( !empty( self::$related_objects[ $options[ 'pick_object' ][ 'data' ] ] ) )
  886. $data = self::$related_objects[ $options[ 'pick_object' ] ][ 'data' ];
  887. elseif ( isset( self::$related_objects[ $options[ 'pick_object' ] ][ 'value_to_label_callback' ] ) && is_callable( self::$related_objects[ $options[ 'pick_object' ] ][ 'value_to_label_callback' ] ) ) {
  888. $data = call_user_func_array(
  889. self::$related_objects[ $options[ 'pick_object' ] ][ 'value_to_label_callback' ],
  890. array( compact( array( 'pod', 'field', 'value' ) ) )
  891. );
  892. }
  893. }
  894. else {
  895. $pick_val = pods_var( 'pick_val', $options );
  896. if ( 'table' == pods_var( 'pick_object', $options ) )
  897. $pick_val = pods_var( 'pick_table', $options, $pick_val, null, true );
  898. $options[ 'table_info' ] = pods_api()->get_table_info( pods_var( 'pick_object', $options ), $pick_val, null, null, $options );
  899. $search_data = pods_data();
  900. $search_data->table = $options[ 'table_info' ][ 'table' ];
  901. $search_data->join = $options[ 'table_info' ][ 'join' ];
  902. $search_data->field_id = $options[ 'table_info' ][ 'field_id' ];
  903. $search_data->field_index = $options[ 'table_info' ][ 'field_index' ];
  904. $search_data->where = $options[ 'table_info' ][ 'where' ];
  905. $search_data->orderby = $options[ 'table_info' ][ 'orderby' ];
  906. if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'name' ] ) ) {
  907. $search_data->pod = $options[ 'table_info' ][ 'pod' ][ 'name' ];
  908. $search_data->fields = $options[ 'table_info' ][ 'pod' ][ 'fields' ];
  909. }
  910. $params = array(
  911. 'select' => "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`",
  912. 'table' => $search_data->table,
  913. 'where' => pods_var_raw( 'pick_where', $options, (array) $options[ 'table_info' ][ 'where_default' ], null, true ),
  914. 'orderby' => pods_var_raw( 'pick_orderby', $options, null, null, true ),
  915. 'groupby' => pods_var_raw( 'pick_groupby', $options, null, null, true )
  916. );
  917. if ( in_array( $options[ 'pick_object' ], array( 'site', 'network' ) ) )
  918. $params[ 'select' ] .= ', `t`.`path`';
  919. if ( !empty( $params[ 'where' ] ) && (array) $options[ 'table_info' ][ 'where_default' ] != $params[ 'where' ] )
  920. $params[ 'where' ] = pods_evaluate_tags( $params[ 'where' ], true );
  921. if ( empty( $params[ 'where' ] ) )
  922. $params[ 'where' ] = array();
  923. if ( !is_array( $params[ 'where' ] ) )
  924. $params[ 'where' ] = (array) $params[ 'where' ];
  925. $params[ 'where' ][] = "`t`.`{$search_data->field_id}` = " . number_format( $value, 0, '', '' );
  926. /* not needed yet
  927. if ( !empty( $params[ 'orderby' ] ) )
  928. $params[ 'orderby' ] = pods_evaluate_tags( $params[ 'orderby' ], true );
  929. if ( !empty( $params[ 'groupby' ] ) )
  930. $params[ 'groupby' ] = pods_evaluate_tags( $params[ 'groupby' ], true );*/
  931. $display = trim( pods_var( 'pick_display', $options ), ' {@}' );
  932. if ( 0 < strlen( $display ) ) {
  933. if ( isset( $options[ 'table_info' ][ 'pod' ] ) && !empty( $options[ 'table_info' ][ 'pod' ] ) ) {
  934. if ( isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'pod' ][ 'object_fields' ][ $display ] ) ) {
  935. $search_data->field_index = $display;
  936. $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
  937. }
  938. elseif ( isset( $options[ 'table_info' ][ 'pod' ][ 'fields' ][ $display ] ) ) {
  939. $search_data->field_index = $display;
  940. if ( 'table' == $options[ 'table_info' ][ 'pod' ][ 'storage' ] && !in_array( $options[ 'table_info' ][ 'pod' ][ 'type' ], array( 'pod', 'table' ) ) )
  941. $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `d`.`{$search_data->field_index}`";
  942. else
  943. $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
  944. }
  945. }
  946. elseif ( isset( $options[ 'table_info' ][ 'object_fields' ] ) && isset( $options[ 'table_info' ][ 'object_fields' ][ $display ] ) ) {
  947. $search_data->field_index = $display;
  948. $params[ 'select' ] = "`t`.`{$search_data->field_id}`, `t`.`{$search_data->field_index}`";
  949. }
  950. }
  951. $autocomplete = false;
  952. if ( 'single' == pods_var( 'pick_format_type', $options ) && 'autocomplete' == pods_var( 'pick_format_single', $options ) )
  953. $autocomplete = true;
  954. elseif ( 'multi' == pods_var( 'pick_format_type', $options ) && 'autocomplete' == pods_var( 'pick_format_multi', $options ) )
  955. $autocomplete = true;
  956. if ( $autocomplete )
  957. $params[ 'limit' ] = apply_filters( 'pods_form_ui_field_pick_autocomplete_limit', 30, $field[ 'name' ], $value, $options, $pod, 0 );
  958. $results = $search_data->select( $params );
  959. if ( !empty( $results ) && ( !$autocomplete || $search_data->total_found() <= $params[ 'limit' ] ) ) {
  960. foreach ( $results as $result ) {
  961. $result = get_object_vars( $result );
  962. $result[ $search_data->field_index ] = trim( $result[ $search_data->field_index ] );
  963. if ( in_array( $options[ 'pick_object' ], array( 'site', 'network' ) ) )
  964. $result[ $search_data->field_index ] = $result[ $search_data->field_index ] . $result[ 'path' ];
  965. elseif ( strlen( $result[ $search_data->field_index ] ) < 1 )
  966. $result[ $search_data->field_index ] = '(No Title)';
  967. $data = $result[ $search_data->field_index ];
  968. }
  969. }
  970. elseif ( !empty( $value ) && $autocomplete && $params[ 'limit' ] < $search_data->total_found() ) {
  971. $ids = $value;
  972. if ( is_array( $ids ) )
  973. $ids = implode( ', ', $ids );
  974. if ( is_array( $params[ 'where' ] ) )
  975. $params[ 'where' ] = implode( ' AND ', $params[ 'where' ] );
  976. if ( !empty( $params[ 'where' ] ) )
  977. $params[ 'where' ] .= ' AND ';
  978. $params[ 'where' ] .= "`t`.`{$search_data->field_id}` IN ( " . $ids . " )";
  979. $results = $search_data->select( $params );
  980. if ( !empty( $results ) ) {
  981. foreach ( $results as $result ) {
  982. $result = get_object_vars( $result );
  983. $result[ $search_data->field_index ] = trim( $result[ $search_data->field_index ] );
  984. if ( strlen( $result[ $search_data->field_index ] ) < 1 )
  985. $result[ $search_data->field_index ] = '(No Title)';
  986. $data = $result[ $search_data->field_index ];
  987. }
  988. }
  989. }
  990. }
  991. }
  992. $data = apply_filters( 'pods_field_pick_value_data', $data, $pod, $field, $value );
  993. $data = pods_serial_comma( $data );
  994. return $data;
  995. }
  996. }