PodsBuilderModuleField.php
138 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package Pods\Components |
| 4 | * @subpackage Builder |
| 5 | */ |
| 6 | if ( ! class_exists( 'LayoutModule' ) ) { |
| 7 | return; |
| 8 | } |
| 9 | |
| 10 | if ( ! class_exists( 'PodsBuilderModuleField' ) ) { |
| 11 | /** |
| 12 | * Class PodsBuilderModuleField |
| 13 | */ |
| 14 | class PodsBuilderModuleField extends LayoutModule { |
| 15 | |
| 16 | public $_name = ''; |
| 17 | public $_var = 'pods-builder-field'; |
| 18 | public $_description = ''; |
| 19 | public $_editor_width = 500; |
| 20 | public $_can_remove_wrappers = true; |
| 21 | |
| 22 | /** |
| 23 | * Register the Module |
| 24 | */ |
| 25 | public function __construct() { |
| 26 | |
| 27 | $this->_name = __( 'Pods - Field Value', 'pods' ); |
| 28 | $this->_description = __( "Display a single Pod item's field value", 'pods' ); |
| 29 | $this->module_path = dirname( __FILE__ ); |
| 30 | |
| 31 | $this->LayoutModule(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Set default variables |
| 36 | * |
| 37 | * @param $defaults |
| 38 | * |
| 39 | * @return mixed |
| 40 | */ |
| 41 | public function _get_defaults( $defaults ) { |
| 42 | |
| 43 | $new_defaults = array( |
| 44 | 'pod_type' => '', |
| 45 | 'slug' => '', |
| 46 | 'field' => '', |
| 47 | 'sidebar' => 'none', |
| 48 | ); |
| 49 | |
| 50 | return ITUtility::merge_defaults( $new_defaults, $defaults ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Output something before the table form |
| 55 | * |
| 56 | * @param object $form Form class |
| 57 | * @param bool $results |
| 58 | */ |
| 59 | public function _before_table_edit( $form, $results = true ) { |
| 60 | |
| 61 | ?> |
| 62 | <p><?php echo $this->_description; ?></p> |
| 63 | <?php |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Output something at the start of the table form |
| 68 | * |
| 69 | * @param object $form Form class |
| 70 | * @param bool $results |
| 71 | */ |
| 72 | public function _start_table_edit( $form, $results = true ) { |
| 73 | |
| 74 | $api = pods_api(); |
| 75 | $all_pods = $api->load_pods( array( 'names' => true ) ); |
| 76 | |
| 77 | $pod_types = array(); |
| 78 | |
| 79 | foreach ( $all_pods as $pod_name => $pod_label ) { |
| 80 | $pod_types[ $pod_name ] = $pod_label . ' (' . $pod_name . ')'; |
| 81 | } |
| 82 | ?> |
| 83 | <tr> |
| 84 | <td valign="top"> |
| 85 | <label for="pod_type"><?php _e( 'Pod', 'pods' ); ?></label> |
| 86 | </td> |
| 87 | <td> |
| 88 | <?php |
| 89 | if ( 0 < count( $all_pods ) ) { |
| 90 | $form->add_drop_down( 'pod_type', $pod_types ); |
| 91 | } else { |
| 92 | echo '<strong class="red">' . __( 'None Found', 'pods' ) . '</strong>'; |
| 93 | } |
| 94 | ?> |
| 95 | </td> |
| 96 | </tr> |
| 97 | <tr> |
| 98 | <td valign="top"> |
| 99 | <label for="slug"><?php _e( 'Slug or ID', 'pods' ); ?></label> |
| 100 | </td> |
| 101 | <td> |
| 102 | <?php $form->add_text_box( 'slug' ); ?> |
| 103 | </td> |
| 104 | </tr> |
| 105 | <tr> |
| 106 | <td valign="top"> |
| 107 | <label for="field"><?php _e( 'Field', 'pods' ); ?></label> |
| 108 | </td> |
| 109 | <td> |
| 110 | <?php $form->add_text_box( 'field' ); ?> |
| 111 | </td> |
| 112 | </tr> |
| 113 | <?php |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Module Output |
| 118 | * |
| 119 | * @param $fields |
| 120 | */ |
| 121 | public function _render( $fields ) { |
| 122 | |
| 123 | $args = array( |
| 124 | 'name' => trim( (string) pods_var_raw( 'pod_type', $fields['data'], '' ) ), |
| 125 | 'slug' => trim( (string) pods_var_raw( 'slug', $fields['data'], '' ) ), |
| 126 | 'field' => trim( (string) pods_var_raw( 'field', $fields['data'], '' ) ), |
| 127 | ); |
| 128 | |
| 129 | if ( 0 < strlen( $args['name'] ) && 0 < strlen( $args['slug'] ) && 0 < strlen( $args['field'] ) ) { |
| 130 | echo pods_shortcode( $args, ( isset( $content ) ? $content : null ) ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | }//end if |
| 136 | |
| 137 | new PodsBuilderModuleField(); |
| 138 |