ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

MultiImageControl.php (3747B)


      1 <?php
      2 
      3 namespace Materialis\Customizer\Controls;
      4 
      5 class MultiImageControl extends \Materialis\Customizer\BaseControl
      6 {
      7     public function init()
      8     {
      9         $this->cpData['min'] = isset($this->cpData['min'])?$this->cpData['min']:2;
     10         $this->cpData['limit'] = isset($this->cpData['max'])?$this->cpData['max']:2;
     11     }
     12 
     13 
     14     public function enqueue()
     15     {
     16         $jsUrl = $this->companion()->assetsRootURL() . "/js/customizer";
     17         wp_enqueue_script('cp-multi-image-control', $jsUrl . "/multi-image-control.js", array('cp-customizer-base'));
     18         wp_localize_script('cp-multi-image-control', 'cpMultiImageTexts', array(
     19             'addImage'=> __('Add New Image', 'cloudpress-companion'),
     20             'deleteTitle'=>__('Delete image from slideshow', 'cloudpress-companion'),
     21             'changeTitle'=>__('changeImage', 'cloudpress-companion')
     22         ));
     23     }
     24 
     25 
     26     public function render()
     27     {
     28         $id    = 'customize-control-' . str_replace(array( '[', ']' ), array( '-', '' ), $this->id);
     29         $class = 'list ';
     30         $options = 'data-min="'.$this->cpData['min'].'" data-max="'.$this->cpData['limit'].'"'; ?>
     31 
     32         <li data-type="cp-multi-image-manager" id="<?php echo esc_attr($id); ?>" <?php $this->link(); ?> <?php  echo $options ?> class="<?php echo esc_attr($class); ?>">
     33            <?php if (! empty($this->label)) : ?>
     34                 <span class="customize-control-title"><?php echo esc_html($this->label); ?></span>
     35             <?php endif;
     36         if (! empty($this->description)) : ?>
     37                 <span class="description customize-control-description"><?php echo $this->description; ?></span>
     38             <?php endif; ?>
     39             <div>
     40 			    <?php $this->render_content(); ?>
     41             </div>
     42             <div class="add-new-container">
     43                  <button type="button" class="button upload-button control-focus" >Add New Image</button>
     44             </div>
     45 		</li>
     46         <?php
     47 
     48     }
     49 
     50     public function render_content()
     51     {
     52         $value = $this->value();
     53 
     54         if (count($value)) {
     55             foreach ($value as $item) {
     56                 $this->renderItem($item);
     57             }
     58         } else {
     59             for ($i=0; $i< $this->cpData['min']; $i++) {
     60                 $this->renderItem('');
     61             }
     62         }
     63     }
     64 
     65     private function renderItem($item)
     66     {
     67         $fieldID = uniqid($this->id);
     68         $value = $this->value(); ?>
     69                 <div class="cp-multi-image-item attachment-media-view attachment-media-view-image ">
     70                     <div class="thumbnail thumbnail-image">
     71                         <img id="<?php echo  $fieldID ?>-thumb" class="attachment-thumb" src="<?php echo $item ?>" draggable="false" alt="">
     72                     </div>
     73                     <div class="actions">
     74                         <input type="hidden" value="<?php echo $item ?>" id="<?php echo  $fieldID ?>" />
     75                         <div class="actions">
     76                         <span title="Change Image" onClick='CP_Customizer.openMediaBrowser("image", jQuery("#<?php echo  $fieldID ?>"))' class="open-right section-icon"></span>
     77                         <?php if (count($value)>$this->cpData['min']): ?>
     78                             <span title="Delete image from slideshow" class="item-remove"></span>
     79                         <?php endif; ?>
     80                     </div>
     81                     <script>
     82                         jQuery("#<?php echo  $fieldID ?>").change(function(){
     83                             jQuery('#<?php echo  $fieldID ?>-thumb').attr('src',this.value);
     84                         });
     85                     </script>
     86                 </div>
     87         <?php
     88 
     89     }
     90 }