PodsBuilderModuleForm.php
159 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( 'PodsBuilderModuleForm' ) ) { |
| 11 | /** |
| 12 | * Class PodsBuilderModuleForm |
| 13 | */ |
| 14 | class PodsBuilderModuleForm extends LayoutModule { |
| 15 | |
| 16 | public $_name = ''; |
| 17 | public $_var = 'pods-builder-form'; |
| 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 - Form', 'pods' ); |
| 28 | $this->_description = __( 'Display a form for creating and editing 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 | 'slug' => '', |
| 46 | 'fields' => '', |
| 47 | 'label' => __( 'Submit', 'pods' ), |
| 48 | 'thank_you' => '', |
| 49 | 'sidebar' => 'none', |
| 50 | ); |
| 51 | |
| 52 | return ITUtility::merge_defaults( $new_defaults, $defaults ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Output something before the table form |
| 57 | * |
| 58 | * @param object $form Form class |
| 59 | * @param bool $results |
| 60 | */ |
| 61 | public function _before_table_edit( $form, $results = true ) { |
| 62 | |
| 63 | ?> |
| 64 | <p><?php echo $this->_description; ?></p> |
| 65 | <?php |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Output something at the start of the table form |
| 70 | * |
| 71 | * @param object $form Form class |
| 72 | * @param bool $results |
| 73 | */ |
| 74 | public function _start_table_edit( $form, $results = true ) { |
| 75 | |
| 76 | $api = pods_api(); |
| 77 | $all_pods = $api->load_pods( array( 'names' => true ) ); |
| 78 | |
| 79 | $pod_types = array(); |
| 80 | |
| 81 | foreach ( $all_pods as $pod_name => $pod_label ) { |
| 82 | $pod_types[ $pod_name ] = $pod_label . ' (' . $pod_name . ')'; |
| 83 | } |
| 84 | ?> |
| 85 | <tr> |
| 86 | <td valign="top"> |
| 87 | <label for="pod_type"><?php _e( 'Pod', 'pods' ); ?></label> |
| 88 | </td> |
| 89 | <td> |
| 90 | <?php |
| 91 | if ( 0 < count( $all_pods ) ) { |
| 92 | $form->add_drop_down( 'pod_type', $pod_types ); |
| 93 | } else { |
| 94 | echo '<strong class="red">' . __( 'None Found', 'pods' ) . '</strong>'; |
| 95 | } |
| 96 | ?> |
| 97 | </td> |
| 98 | </tr> |
| 99 | <tr> |
| 100 | <td valign="top"> |
| 101 | <label for="slug"><?php _e( 'Slug or ID', 'pods' ); ?></label> |
| 102 | </td> |
| 103 | <td> |
| 104 | <?php $form->add_text_box( 'slug' ); ?> |
| 105 | </td> |
| 106 | </tr> |
| 107 | <tr> |
| 108 | <td valign="top"> |
| 109 | <label for="fields"><?php _e( 'Fields (comma-separated)', 'pods' ); ?></label> |
| 110 | </td> |
| 111 | <td> |
| 112 | <?php $form->add_text_box( 'fields' ); ?> |
| 113 | </td> |
| 114 | </tr> |
| 115 | <tr> |
| 116 | <td valign="top"> |
| 117 | <label for="label"><?php _e( 'Submit Label', 'pods' ); ?></label> |
| 118 | </td> |
| 119 | <td> |
| 120 | <?php $form->add_text_box( 'label' ); ?> |
| 121 | </td> |
| 122 | </tr> |
| 123 | <tr> |
| 124 | <td valign="top"> |
| 125 | <label for="thank_you"><?php _e( 'Thank You URL upon submission', 'pods' ); ?></label> |
| 126 | </td> |
| 127 | <td> |
| 128 | <?php $form->add_text_box( 'thank_you' ); ?> |
| 129 | </td> |
| 130 | </tr> |
| 131 | <?php |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Module Output |
| 136 | * |
| 137 | * @param $fields |
| 138 | */ |
| 139 | public function _render( $fields ) { |
| 140 | |
| 141 | $args = array( |
| 142 | 'name' => trim( (string) pods_var_raw( 'pod_type', $fields['data'], '' ) ), |
| 143 | 'slug' => trim( (string) pods_var_raw( 'slug', $fields['data'], '' ) ), |
| 144 | 'fields' => trim( (string) pods_var_raw( 'fields', $fields['data'], '' ) ), |
| 145 | 'label' => trim( (string) pods_var_raw( 'label', $fields['data'], __( 'Submit', 'pods' ), null, true ) ), |
| 146 | 'thank_you' => trim( (string) pods_var_raw( 'thank_you', $fields['data'], '' ) ), |
| 147 | 'form' => 1, |
| 148 | ); |
| 149 | |
| 150 | if ( 0 < strlen( $args['name'] ) ) { |
| 151 | echo pods_shortcode( $args, ( isset( $content ) ? $content : null ) ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | }//end if |
| 157 | |
| 158 | new PodsBuilderModuleForm(); |
| 159 |