UIX Documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • None
  • uix
    • data
    • ui
      • control

Classes

  • uix\data\data
  • uix\ui
  • uix\ui\box
  • uix\ui\control
  • uix\ui\control\autocomplete
  • uix\ui\control\button
  • uix\ui\control\checkbox
  • uix\ui\control\color
  • uix\ui\control\editor
  • uix\ui\control\email
  • uix\ui\control\file
  • uix\ui\control\hidden
  • uix\ui\control\number
  • uix\ui\control\post_relation
  • uix\ui\control\radio
  • uix\ui\control\select
  • uix\ui\control\separator
  • uix\ui\control\slider
  • uix\ui\control\template
  • uix\ui\control\text
  • uix\ui\control\textarea
  • uix\ui\control\toggle
  • uix\ui\footer
  • uix\ui\grid
  • uix\ui\header
  • uix\ui\help
  • uix\ui\metabox
  • uix\ui\modal
  • uix\ui\notice
  • uix\ui\page
  • uix\ui\panel
  • uix\ui\post_type
  • uix\ui\repeat
  • uix\ui\section
  • uix\ui\uix

Interfaces

  • uix\data\load
  • uix\data\save

Functions

  • uix
  • uix_autoload_class
  1 <?php
  2 /**
  3  * UIX repeat
  4  *
  5  * @package   ui
  6  * @author    David Cramer
  7  * @license   GPL-2.0+
  8  * @link
  9  * @copyright 2016 David Cramer
 10  */
 11 namespace uix\ui;
 12 
 13 /**
 14  * A repetable container for repeatable areas.
 15  *
 16  * @since 1.0.0
 17  * @see \uix\uix
 18  */
 19 class repeat extends panel {
 20 
 21     /**
 22      * The type of object
 23      *
 24      * @since 1.0.0
 25      * @access public
 26      * @var      string
 27      */
 28     public $type = 'repeat';
 29 
 30     /**
 31      * The instance of this object
 32      *
 33      * @since 1.0.0
 34      * @access public
 35      * @var      int|string
 36      */
 37     public $instance = 0;
 38 
 39     /**
 40      * total instances of this object
 41      *
 42      * @since 1.0.0
 43      * @access public
 44      * @var      int|string
 45      */
 46     public $instances = 0;
 47 
 48     /**
 49      * The templates to render in the footer
 50      *
 51      * @since 1.0.0
 52      * @access public
 53      * @var      string
 54      */
 55     public $templates = null;
 56 
 57     /**
 58      * Button Label
 59      *
 60      * @since 1.0.0
 61      * @access public
 62      * @var      string
 63      */
 64     public $button_label;
 65 
 66 
 67     /**
 68      * Define core page style
 69      *
 70      * @since 1.0.0
 71      * @access public
 72      */
 73     public function set_assets() {
 74 
 75         $this->assets['script']['repeat']   =  $this->url . 'assets/js/repeat' . UIX_ASSET_DEBUG . '.js';
 76         $this->assets['style']['repeat']   =  $this->url . 'assets/css/repeat' . UIX_ASSET_DEBUG . '.css';
 77 
 78         parent::set_assets();
 79     }
 80 
 81 
 82     /**
 83      * Compares the key of submitted fields to match instances
 84      *
 85      * @since 1.0.0
 86      * @see \uix\uix
 87      * @access private
 88      * @param string $key Key to compare
 89      * @return bool
 90      */
 91     private function compare_var_key( $key ){
 92         $id_parts = $this->id_base_parts();
 93         $compare = implode('-', $id_parts ) . '-';
 94         return substr( $key, 0, strlen( $compare ) ) == $compare;
 95     }
 96 
 97     /**
 98      * Pushes the children to initilize setup in order to capture the instance data
 99      * @access private
100      * @param $index
101      */
102     private function push_instance_setup( $index ){
103         $this->instances = $this->instance = $index;
104         if( !isset( $this->data[ $this->instance ] ) )
105             $this->data[ $this->instance ] = array();
106 
107         foreach( $this->child as $child ){
108             $child->setup();
109             $this->data[ $this->instance ] += $child->get_data();
110         }
111     }
112 
113     /**
114      * Removes the instance number from the submission key
115      * @access private
116      * @param $key
117      * @return int
118      */
119     private function build_instance_count( $key ){
120         $key_parts = explode( '-', $key );
121         $id_parts = $this->id_base_parts();
122         return (int) $key_parts[ count( $id_parts ) ];
123     }
124 
125     /**
126      * Breaks apart the ID to get the base parts without the instance number
127      * @access private
128      * @return array
129      */
130     private function id_base_parts(){
131         $id_parts = explode( '-', $this->id() );
132         array_pop( $id_parts );
133         return $id_parts;
134     }
135 
136     /**
137      * prepares Data for extraction and saving
138      *
139      * @since 1.0.0
140      * @see \uix\uix
141      * @access public
142      */
143     public function prepare_data() {
144         $submit_data = uix()->request_vars( 'post' );
145         if( !empty( $submit_data ) ){
146             $instances = array_filter( array_keys( $submit_data ), array( $this, 'compare_var_key' ) );
147             $instances = array_map( array( $this, 'build_instance_count' ), $instances );
148             array_map( array( $this, 'push_instance_setup' ), array_unique( $instances ) );
149         }
150         $this->instance = 0; // reset instance;
151     }
152     public function setup(){
153         parent::setup();
154         $this->prepare_data();
155     }
156     /**
157      * Sets the data for all children
158      *
159      * @since 1.0.0
160      * @access public
161      */
162     public function get_data(){
163 
164         if( empty( $this->data ) )
165             $this->data = $this->set_instance_data();
166 
167         return $this->data;
168 
169     }
170 
171 
172     /**
173      * @return array
174      */
175     public function set_instance_data(){
176         $data = array();
177         $this->instance = 0;
178         while( $this->instance < $this->instances ){
179 
180             if( !isset( $data[ $this->instance ] ) )
181                 $data[ $this->instance ] = array();
182 
183             if( null !== $this->get_instance_data() )
184                 $data[ $this->instance ] += $this->get_instance_data();
185 
186             $this->instance++;
187         }
188         $this->instance = 0;
189 
190         return $data;
191     }
192     /**
193      * @return array
194      */
195     public function get_instance_data(){
196         $data = array();
197         foreach ( $this->child as $child ){
198             if( method_exists( $child, 'get_data' ) ){
199                 if( null !== $child->get_data() )
200                     $data += $child->get_data();
201             }
202         }
203 
204         return $data;
205     }
206 
207 
208     /**
209      * Sets the data for all children
210      *
211      * @since 1.0.0
212      * @access public
213      */
214     public function set_data( $data ){
215 
216         $this->instance = 0;
217 
218         foreach ( $data as $instance => $instance_data){
219             foreach ( $this->child as $child ){
220                 $child->set_data($instance_data);
221             }
222             $this->instance++;
223         }
224         $this->instances = $this->instance;
225         $this->instance = 0;
226 
227     }
228 
229     /**
230      * uix object id
231      *
232      * @since 1.0.0
233      * @access public
234      * @return string The object ID
235      */
236     public function id(){
237 
238         return parent::id() . '-' . $this->instance;
239     }
240 
241     /**
242      * Enqueues specific tabs assets for the active pages
243      *
244      * @since 1.0.0
245      * @access protected
246      */
247     protected function enqueue_active_assets(){
248 
249         parent::enqueue_active_assets();
250 
251         echo '<style type="text/css">';
252             echo '#' . $this->id() .' .uix-repeat{ box-shadow: 1px 0 0 ' . $this->base_color() . ' inset, -37px 0 0 #f5f5f5 inset, -38px 0 0 #ddd inset, 0 2px 3px rgba(0, 0, 0, 0.05); };';
253         echo '</stype>';
254     }
255 
256     /**
257      * Render the complete section
258      *
259      * @since 1.0.0
260      * @access public
261      * @return string|null HTML of rendered notice
262      */
263     public function render(){
264 
265         add_action( 'admin_footer', array( $this, 'render_repeatable_script' ) );
266         add_action( 'wp_footer', array( $this, 'render_repeatable_script' ) );
267 
268         $data = $this->get_data();
269 
270         $output = '<div data-uix-template="' . esc_attr( $this->id() ) . '" ' . $this->build_attributes() . '>';
271         foreach( (array) $data as $instance_id ){
272             if (!isset($this->struct['active']))
273                 $this->struct['active'] = 'true';
274 
275             $output .= $this->render_repeatable();
276 
277             $this->instance++;
278 
279         }
280         $output .= '</div>';
281 
282 
283         $output .= $this->render_repeatable_more();
284 
285         return $output;
286     }
287 
288 
289     /**
290      * Render the add more button and template
291      *
292      * @since 1.0.0
293      * @access public
294      * @return string|null HTML of rendered object
295      */
296     public function render_repeatable_more(){
297 
298         $label = __( 'Add Another', 'uix' );
299 
300         if( !empty( $this->struct['label'] ) )
301             $label = $this->struct['label'];
302 
303         $this->instance = '{{_inst_}}';
304         $this->templates = $this->render_repeatable();
305         $this->instance = 0;
306         $output = '<div class="repeatable-footer"><button type="button" class="button" data-uix-repeat="' . esc_attr( $this->id() ) . '">' . esc_html( $label ) . '</button></div>';
307 
308 
309         return $output;
310 
311     }
312 
313     /**
314      * Render the internal section
315      *
316      * @since 1.0.0
317      * @access public
318      * @return string|null HTML of rendered object
319      */
320     public function render_repeatable(){
321         $output = '<div class="uix-repeat">';
322         $output .= $this->render_template();
323         if (!empty($this->child))
324             $output .= $this->render_children();
325 
326         $output .= '<button type="button" class="button button-small uix-remover"><span class="dashicons dashicons-no"></span></button> </div>';
327         return $output;
328 
329     }
330 
331     /**
332      * Render the script footer template
333      *
334      * @since 1.0.0
335      * @see \uix\ui\uix
336      * @access public
337      * @return string HTML of rendered box
338      */
339     public function render_repeatable_script(){
340         $output = null;
341         if( !empty( $this->templates ) ){
342             $output .= '<script type="text/html" id="' . esc_attr($this->id()) . '-tmpl">';
343             $output .= $this->templates;
344             $output .= '</script>';
345         }
346 
347 
348         echo $output;
349     }
350 
351 
352 }
UIX Documentation API documentation generated by ApiGen