1 <?php
  2 
  3   4   5   6   7   8   9  10 
 11 
 12 namespace Icybee\Modules\Images;
 13 
 14 use ICanBoogie\Operation;
 15 
 16 use Brickrouge\Element;
 17 
 18 class PopOrUploadImage extends Element
 19 {
 20     const POP_OPTIONS = '#poporuploadimage-pop-options';
 21     const UPLOAD_OPTIONS = '#poporuploadimage-upload-options';
 22 
 23     private $pop_image;
 24 
 25     static protected function add_assets(\Brickrouge\Document $document)
 26     {
 27         parent::add_assets($document);
 28 
 29         $document->js->add(DIR . 'public/module.js');
 30         $document->css->add(DIR . 'public/module.css');
 31     }
 32 
 33     public function __construct(array $attributes=array())
 34     {
 35         $attributes += [
 36 
 37             self::POP_OPTIONS => [],
 38             self::UPLOAD_OPTIONS => []
 39 
 40         ];
 41 
 42         parent::__construct
 43         (
 44             'div', $attributes + array
 45             (
 46                 Element::CHILDREN => array
 47                 (
 48                     $this->pop_image = new PopImage($attributes[self::POP_OPTIONS]),
 49                     $this->upload_image = new UploadImage($attributes[self::UPLOAD_OPTIONS] + [
 50 
 51                         UploadImage::FILE_WITH_LIMIT => true,
 52 
 53                         'data-name' => Image::PATH,
 54                         'data-upload-url' => Operation::encode('images/save')
 55                     ])
 56                 ),
 57 
 58                 Element::WIDGET_CONSTRUCTOR => 'PopOrUploadImage'
 59             )
 60         );
 61     }
 62 
 63     protected function alter_class_names(array $class_names)
 64     {
 65         return parent::alter_class_names($class_names) + array
 66         (
 67             'widget-class' => 'widget-pop-or-upload-image'
 68         );
 69     }
 70 
 71     public function offsetExists($attribute)
 72     {
 73         if ($attribute == 'name' || $attribute == 'value')
 74         {
 75             return isset($this->pop_image[$attribute]);
 76         }
 77 
 78         return parent::offsetExists($attribute);
 79     }
 80 
 81     public function offsetSet($attribute, $value)
 82     {
 83         if ($attribute == 'name' || $attribute == 'value')
 84         {
 85             $this->pop_image[$attribute] = $value;
 86 
 87             return;
 88         }
 89 
 90         parent::offsetSet($attribute, $value);
 91     }
 92 
 93     public function offsetGet($attribute)
 94     {
 95         if ($attribute == 'name' || $attribute == 'value')
 96         {
 97             return $this->pop_image[$attribute];
 98         }
 99 
100         return parent::offsetGet($attribute);
101     }
102 
103     public function offsetUnset($attribute)
104     {
105         if ($attribute == 'name' || $attribute == 'value')
106         {
107             unset($this->pop_image[$attribute]);
108 
109             return;
110         }
111 
112         parent::offsetUnset($attribute);
113     }
114 }
115 
116 class UploadImage extends \Brickrouge\File
117 {
118     const UPLOAD_MODE = '#uploadimage-upload-mode';
119     const UPLOAD_MODE_CREATE = 'create';
120     const UPLOAD_MODE_UPDATE = 'update';
121 
122     protected function alter_dataset(array $dataset)
123     {
124         return parent::alter_dataset($dataset) + [
125 
126             'upload-mode' => $this[self::UPLOAD_MODE] ?: self::UPLOAD_MODE_CREATE
127 
128         ];
129     }
130 }