Blame view

protected/modules/admin/components/widgets/GalleryInput.php 2.96 KB
a1684257   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  <?php
  /**
   * Created by JetBrains PhpStorm.
   * User: z_bodya
   * Date: 8/2/12
   * Time: 10:53 PM
   * To change this template use File | Settings | File Templates.
   */
  class GalleryInput extends CWidget
  {
      /**
       * @var CModel the data model associated with this widget.
       */
      public $model;
      /**
       * @var string the attribute associated with this widget.
       * The name can contain square brackets (e.g. 'name[1]') which is used to collect tabular data input.
       */
      public $attribute;
      /**
       * @var array additional HTML options to be rendered in the input tag
       */
      public $htmlOptions = array();
      /**
       * @var array Settings for image auto-generation
       * @example
       *  array(
       *       'small' => array(
       *              'resize' => array(200, null),
       *       ),
       *      'medium' => array(
       *              'resize' => array(800, null),
       *      )
       *  );
       */
      public $versions;
      /** @var boolean does images in gallery need names */
      public $name;
      /** @var boolean does images in gallery need descriptions */
      public $description;
      /** @var boolean does images in gallery need links */
      public $link=false;
  
      public function run()
      {
          $name = CHtml::resolveName($this->model, $this->attribute);
          $value = $this->model->{$this->attribute};
  
          if (empty($value)) {
              $gallery = new Gallery();
              $gallery->name = $this->name;
              $gallery->description = $this->description;
              $gallery->link = $this->link;
              $gallery->versions = $this->versions;
              $gallery->save();
  
              $value = $gallery->id;
          } else {
              $gallery = Gallery::model()->findByPk((int)$value);
              $changed = false;
              if ($gallery->name != (bool)$this->name) {
                  $changed = true;
              }
              if ($gallery->description != (bool)$this->description) {
                  $changed = true;
              }
              if ($gallery->link != (bool)$this->link) {
                  $changed = true;
              }
              if ($gallery->versions_data != serialize($this->versions)) {
                  $changed = true;
              }
              if ($changed) {
                  foreach ($gallery->galleryPhotos as $photo) {
                      $photo->removeImages();
                  }
  
                  $gallery->name = $this->name;
                  $gallery->description = $this->description;
                  $gallery->link = $this->link;
                  $gallery->versions = $this->versions;
  
                  $gallery->save();
  
                  foreach ($gallery->galleryPhotos as $photo) {
                      $photo->updateImages();
                  }
              }
          }
  //        CVarDumper::dump($gallery,10,true);exit();
  
          echo CHtml::hiddenField($name, $value);
          $this->widget('GalleryManager', array(
              'gallery' => $gallery,
              'htmlOptions' => $this->htmlOptions,
          ));
      }
  }