PodsBuilderModuleList.php
239 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( 'PodsBuilderModuleList' ) ) { |
| 11 | /** |
| 12 | * Class PodsBuilderModuleList |
| 13 | */ |
| 14 | class PodsBuilderModuleList extends LayoutModule { |
| 15 | |
| 16 | public $_name = ''; |
| 17 | public $_var = 'pods-builder-list'; |
| 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 - List Items', 'pods' ); |
| 28 | $this->_description = __( 'Display multiple Pod items', '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 | 'template' => '', |
| 46 | 'template_custom' => '', |
| 47 | 'limit' => 15, |
| 48 | 'orderby' => '', |
| 49 | 'where' => '', |
| 50 | 'expires' => ( 60 * 5 ), |
| 51 | 'cache_mode' => 'transient', |
| 52 | 'sidebar' => 'none', |
| 53 | ); |
| 54 | |
| 55 | return ITUtility::merge_defaults( $new_defaults, $defaults ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Output something before the table form |
| 60 | * |
| 61 | * @param object $form Form class |
| 62 | * @param bool $results |
| 63 | */ |
| 64 | public function _before_table_edit( $form, $results = true ) { |
| 65 | |
| 66 | ?> |
| 67 | <p><?php echo $this->_description; ?></p> |
| 68 | <?php |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Output something at the start of the table form |
| 73 | * |
| 74 | * @param object $form Form class |
| 75 | * @param bool $results |
| 76 | */ |
| 77 | public function _start_table_edit( $form, $results = true ) { |
| 78 | |
| 79 | $api = pods_api(); |
| 80 | $all_pods = $api->load_pods( array( 'names' => true ) ); |
| 81 | |
| 82 | $pod_types = array(); |
| 83 | |
| 84 | foreach ( $all_pods as $pod_name => $pod_label ) { |
| 85 | $pod_types[ $pod_name ] = $pod_label . ' (' . $pod_name . ')'; |
| 86 | } |
| 87 | ?> |
| 88 | <tr> |
| 89 | <td valign="top"> |
| 90 | <label for="pod_type"><?php _e( 'Pod', 'pods' ); ?></label> |
| 91 | </td> |
| 92 | <td> |
| 93 | <?php |
| 94 | if ( 0 < count( $all_pods ) ) { |
| 95 | $form->add_drop_down( 'pod_type', $pod_types ); |
| 96 | } else { |
| 97 | echo '<strong class="red">' . __( 'None Found', 'pods' ) . '</strong>'; |
| 98 | } |
| 99 | ?> |
| 100 | </td> |
| 101 | </tr> |
| 102 | |
| 103 | <?php |
| 104 | if ( class_exists( 'Pods_Templates' ) ) { |
| 105 | $all_templates = (array) $api->load_templates( array() ); |
| 106 | |
| 107 | $templates = array( |
| 108 | '' => '- ' . __( 'Custom Template', 'pods' ) . ' -', |
| 109 | ); |
| 110 | |
| 111 | foreach ( $all_templates as $template ) { |
| 112 | $templates[ $template['name'] ] = $template['name']; |
| 113 | } |
| 114 | ?> |
| 115 | <tr> |
| 116 | <td valign="top"> |
| 117 | <label for="template"><?php _e( 'Template', 'pods' ); ?></label> |
| 118 | </td> |
| 119 | <td> |
| 120 | <?php |
| 121 | if ( 0 < count( $all_templates ) ) { |
| 122 | $form->add_drop_down( 'template', $templates ); |
| 123 | } else { |
| 124 | echo '<strong class="red">' . __( 'None Found', 'pods' ) . '</strong>'; |
| 125 | } |
| 126 | ?> |
| 127 | </td> |
| 128 | </tr> |
| 129 | <?php |
| 130 | } else { |
| 131 | ?> |
| 132 | <tr> |
| 133 | <td valign="top"> |
| 134 | <label for="template"><?php _e( 'Template', 'pods' ); ?></label> |
| 135 | </td> |
| 136 | <td> |
| 137 | <?php $form->add_text_box( 'template' ); ?> |
| 138 | </td> |
| 139 | </tr> |
| 140 | <?php |
| 141 | }//end if |
| 142 | ?> |
| 143 | |
| 144 | <tr> |
| 145 | <td valign="top"> |
| 146 | <label for="template_custom"><?php _e( 'Custom Template', 'pods' ); ?></label> |
| 147 | </td> |
| 148 | <td> |
| 149 | <?php |
| 150 | $form->add_text_area( |
| 151 | 'template_custom', array( |
| 152 | 'style' => 'width:90%; max-width:100%; min-height:100px;', |
| 153 | 'rows' => '8', |
| 154 | ) |
| 155 | ); |
| 156 | ?> |
| 157 | </td> |
| 158 | </tr> |
| 159 | <tr> |
| 160 | <td valign="top"> |
| 161 | <label for="limit"><?php _e( 'Limit', 'pods' ); ?></label> |
| 162 | </td> |
| 163 | <td> |
| 164 | <?php $form->add_text_box( 'limit' ); ?> |
| 165 | </td> |
| 166 | </tr> |
| 167 | <tr> |
| 168 | <td valign="top"> |
| 169 | <label for="orderby"><?php _e( 'Order By', 'pods' ); ?></label> |
| 170 | </td> |
| 171 | <td> |
| 172 | <?php $form->add_text_box( 'orderby' ); ?> |
| 173 | </td> |
| 174 | </tr> |
| 175 | <tr> |
| 176 | <td valign="top"> |
| 177 | <label for="where"><?php _e( 'Where', 'pods' ); ?></label> |
| 178 | </td> |
| 179 | <td> |
| 180 | <?php $form->add_text_box( 'where' ); ?> |
| 181 | </td> |
| 182 | </tr> |
| 183 | <tr> |
| 184 | <td valign="top"> |
| 185 | <label for="cache_mode"><?php _e( 'Cache Type', 'pods' ); ?></label> |
| 186 | </td> |
| 187 | <td> |
| 188 | <?php |
| 189 | $cache_modes = array( |
| 190 | 'none' => __( 'Disable Caching', 'pods' ), |
| 191 | 'cache' => __( 'Object Cache', 'pods' ), |
| 192 | 'transient' => __( 'Transient', 'pods' ), |
| 193 | 'site-transient' => __( 'Site Transient', 'pods' ), |
| 194 | ); |
| 195 | |
| 196 | $form->add_drop_down( 'cache_mode', $cache_modes ); |
| 197 | ?> |
| 198 | </td> |
| 199 | </tr> |
| 200 | <tr> |
| 201 | <td valign="top"> |
| 202 | <label for="expires"><?php _e( 'Cache Expiration (in seconds)', 'pods' ); ?></label> |
| 203 | </td> |
| 204 | <td> |
| 205 | <?php $form->add_text_box( 'expires' ); ?> |
| 206 | </td> |
| 207 | </tr> |
| 208 | <?php |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Module Output |
| 213 | * |
| 214 | * @param $fields |
| 215 | */ |
| 216 | public function _render( $fields ) { |
| 217 | |
| 218 | $args = array( |
| 219 | 'name' => trim( (string) pods_var_raw( 'pod_type', $fields['data'], '' ) ), |
| 220 | 'template' => trim( (string) pods_var_raw( 'template', $fields['data'], '' ) ), |
| 221 | 'limit' => (int) pods_var_raw( 'limit', $fields['data'], 15, null, true ), |
| 222 | 'orderby' => trim( (string) pods_var_raw( 'orderby', $fields['data'], '' ) ), |
| 223 | 'where' => trim( (string) pods_var_raw( 'where', $fields['data'], '' ) ), |
| 224 | 'expires' => (int) trim( (string) pods_var_raw( 'expires', $fields['data'], ( 60 * 5 ) ) ), |
| 225 | 'cache_mode' => trim( (string) pods_var_raw( 'cache_mode', $fields['data'], 'transient', null, true ) ), |
| 226 | ); |
| 227 | |
| 228 | $content = trim( (string) pods_var_raw( 'template_custom', $fields['data'], '' ) ); |
| 229 | |
| 230 | if ( 0 < strlen( $args['name'] ) && ( 0 < strlen( $args['template'] ) || 0 < strlen( $content ) ) ) { |
| 231 | echo pods_shortcode( $args, ( isset( $content ) ? $content : null ) ); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | }//end if |
| 237 | |
| 238 | new PodsBuilderModuleList(); |
| 239 |