PodsBuilderModuleSingle.php
190 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( 'PodsBuilderModuleSingle' ) ) { |
| 11 | /** |
| 12 | * Class PodsBuilderModuleSingle |
| 13 | */ |
| 14 | class PodsBuilderModuleSingle extends LayoutModule { |
| 15 | |
| 16 | public $_name = ''; |
| 17 | public $_var = 'pods-builder-single'; |
| 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 - Single Item', 'pods' ); |
| 28 | $this->_description = __( 'Display a single Pod item', '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 | 'template' => '', |
| 47 | 'template_custom' => '', |
| 48 | 'sidebar' => 'none', |
| 49 | ); |
| 50 | |
| 51 | return ITUtility::merge_defaults( $new_defaults, $defaults ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Output something before the table form |
| 56 | * |
| 57 | * @param object $form Form class |
| 58 | * @param bool $results |
| 59 | */ |
| 60 | public function _before_table_edit( $form, $results = true ) { |
| 61 | |
| 62 | ?> |
| 63 | <p><?php echo $this->_description; ?></p> |
| 64 | <?php |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Output something at the start of the table form |
| 69 | * |
| 70 | * @param object $form Form class |
| 71 | * @param bool $results |
| 72 | */ |
| 73 | public function _start_table_edit( $form, $results = true ) { |
| 74 | |
| 75 | $api = pods_api(); |
| 76 | $all_pods = $api->load_pods( array( 'names' => true ) ); |
| 77 | |
| 78 | $pod_types = array(); |
| 79 | |
| 80 | foreach ( $all_pods as $pod_name => $pod_label ) { |
| 81 | $pod_types[ $pod_name ] = $pod_label . ' (' . $pod_name . ')'; |
| 82 | } |
| 83 | ?> |
| 84 | <tr> |
| 85 | <td valign="top"> |
| 86 | <label for="pod_type"><?php _e( 'Pod', 'pods' ); ?></label> |
| 87 | </td> |
| 88 | <td> |
| 89 | <?php |
| 90 | if ( 0 < count( $all_pods ) ) { |
| 91 | $form->add_drop_down( 'pod_type', $pod_types ); |
| 92 | } else { |
| 93 | echo '<strong class="red">' . __( 'None Found', 'pods' ) . '</strong>'; |
| 94 | } |
| 95 | ?> |
| 96 | </td> |
| 97 | </tr> |
| 98 | <tr> |
| 99 | <td valign="top"> |
| 100 | <label for="slug"><?php _e( 'Slug or ID', 'pods' ); ?></label> |
| 101 | </td> |
| 102 | <td> |
| 103 | <?php $form->add_text_box( 'slug' ); ?> |
| 104 | </td> |
| 105 | </tr> |
| 106 | |
| 107 | <?php |
| 108 | if ( class_exists( 'Pods_Templates' ) ) { |
| 109 | $all_templates = (array) $api->load_templates( array() ); |
| 110 | |
| 111 | $templates = array( |
| 112 | '' => '- ' . __( 'Custom Template', 'pods' ) . ' -', |
| 113 | ); |
| 114 | |
| 115 | foreach ( $all_templates as $template ) { |
| 116 | $templates[ $template['name'] ] = $template['name']; |
| 117 | } |
| 118 | ?> |
| 119 | <tr> |
| 120 | <td valign="top"> |
| 121 | <label for="template"><?php _e( 'Template', 'pods' ); ?></label> |
| 122 | </td> |
| 123 | <td> |
| 124 | <?php |
| 125 | if ( 0 < count( $all_templates ) ) { |
| 126 | $form->add_drop_down( 'template', $templates ); |
| 127 | } else { |
| 128 | echo '<strong class="red">' . __( 'None Found', 'pods' ) . '</strong>'; |
| 129 | } |
| 130 | ?> |
| 131 | </td> |
| 132 | </tr> |
| 133 | <?php |
| 134 | } else { |
| 135 | ?> |
| 136 | <tr> |
| 137 | <td valign="top"> |
| 138 | <label for="template"><?php _e( 'Template', 'pods' ); ?></label> |
| 139 | </td> |
| 140 | <td> |
| 141 | <?php $form->add_text_box( 'template' ); ?> |
| 142 | </td> |
| 143 | </tr> |
| 144 | <?php |
| 145 | }//end if |
| 146 | ?> |
| 147 | |
| 148 | <tr> |
| 149 | <td valign="top"> |
| 150 | <label for="template_custom"><?php _e( 'Custom Template', 'pods' ); ?></label> |
| 151 | </td> |
| 152 | <td> |
| 153 | <?php |
| 154 | $form->add_text_area( |
| 155 | 'template_custom', array( |
| 156 | 'style' => 'width:90%; max-width:100%; min-height:100px;', |
| 157 | 'rows' => '8', |
| 158 | ) |
| 159 | ); |
| 160 | ?> |
| 161 | </td> |
| 162 | </tr> |
| 163 | <?php |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Module Output |
| 168 | * |
| 169 | * @param $fields |
| 170 | */ |
| 171 | public function _render( $fields ) { |
| 172 | |
| 173 | $args = array( |
| 174 | 'name' => trim( (string) pods_var_raw( 'pod_type', $fields['data'], '' ) ), |
| 175 | 'slug' => trim( (string) pods_var_raw( 'slug', $fields['data'], '' ) ), |
| 176 | 'template' => trim( (string) pods_var_raw( 'template', $fields['data'], '' ) ), |
| 177 | ); |
| 178 | |
| 179 | $content = trim( (string) pods_var_raw( 'template_custom', $fields['data'], '' ) ); |
| 180 | |
| 181 | if ( 0 < strlen( $args['name'] ) && 0 < strlen( $args['slug'] ) && ( 0 < strlen( $args['template'] ) || 0 < strlen( $content ) ) ) { |
| 182 | echo pods_shortcode( $args, ( isset( $content ) ? $content : null ) ); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | }//end if |
| 188 | |
| 189 | new PodsBuilderModuleSingle(); |
| 190 |