GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FBasket::beginTemplate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 2
1
<?php
2
/**
3
 * @author Alexey Tatarinov <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2014 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 * @package frontend.components.collection
8
 */
9
class FBasket extends FCollectionUI
10
{
11
  static $templateCounter = 0;
12
13
  /**
14
   * Пример:
15
   * <pre>
16
   *  $this->basket = new FBasket('basket');
17
   *  $this->basket->collectionItemsForSum = array('options', 'parameters');
18
   *  или
19
   *  $this->basket->collectionItemsForSum = FBasket::COLLECTION_ITEMS_ROOT;
20
   * </pre>
21
   *
22
   * Ключи collectionItems стоимость которых нужно считать, если елементы не сгруппированы по индексу, то нужно указать ключ FBasket::COLLECTION_ITEMS_ROOT
23
   * @var mixed
24
   */
25
  public $collectionItemsForSum;
26
27
  public $classFastOrderShowButton = 'fast-order-show-{keyCollection}';
28
29
  public $classFastOrderCloseButton = 'fast-order-close-{keyCollection}';
30
31
  public $classSubmitFastOrderButton = 'fast-order-submit-{keyCollection}';
32
33
  public $classRepeatOrderButton = 'repeat-order-{keyCollection}';
34
35
  public $classTooltip = 'tooltip-{keyCollection}';
36
37
  public $fastOrderFormId = 'fast-order-form-{keyCollection}';
38
39
  public $fastOrderFormSuccessId = 'fast-order-form-success-{keyCollection}';
40
41
  protected $fastOrderFormPopupId = 'fast-order-popup-{keyCollection}';
42
43
  protected $fastOrderPopupContainer = 'div';
44
45
  protected $templates;
46
47
  protected $templateId = 'template-{keyCollection}-';
48
49
  public function collectionItemSum($index = null)
50
  {
51
    $sum = 0;
52
53
    /**
54
     * @var FCollectionElementBehavior $element
55
     */
56
    foreach($this as $element)
57
    {
58
      $sum += $element->getCollectionItemSum($index);
59
    }
60
61
    return $sum;
62
  }
63
64
  public function getSumTotal()
65
  {
66
    $sum = 0;
67
68
    /**
69
     * @var FCollectionElementBehavior $element
70
     */
71
    foreach($this as $element)
72
    {
73
      $sum += $element->getSumTotal();
74
    }
75
76
    return $sum;
77
  }
78
79
  /**
80
   * Строит кнопку для быстрого заказа
81
   * Пример:
82
   * <pre>
83
   *  $this->basket->buttonFastOrder(
84
   *    $model,
85
   *   'Купить в один клик',
86
   *   array('class' => 'red'),
87
   *   array(
88
   *     'name' => $data->name,
89
   *     'url' => $data->url,
90
   *     'img' => $image ? $image->pre : '',
91
   *     'description:selector' => '.parent-block .description'
92
   *  ));
93
   * </pre>
94
   * @param array|FCollectionElementBehavior|CActiveRecord $model
95
   * @param string $text текст кнопки
96
   * @param array $htmlOptions
97
   * @param array $formData массив данных которые будут подставлятся в попап быстрого заказа. Вместо данных можно использовать селектор для копирования содержимого блока. Формат задания селектора array('description:selector' => '.parent-block .description')
98
   *
99
   * @return string
100
   */
101
  public function buttonFastOrder($model, $text = '', $htmlOptions = array(), $formData = array())
102
  {
103
    $this->appendHtmlOption($htmlOptions, $this->classFastOrderShowButton);
104
105
    if( !empty($formData) )
106
      $this->appendHtmlOption($htmlOptions, CJSON::encode($formData), 'data-form-data');
107
108
    return CHtml::link($text, '#', CMap::mergeArray($this->prepareInputData($model), $htmlOptions));
109
  }
110
111
  /**
112
   * Кнопка закрытия попапа быстрого заказа
113
   * @param array $htmlOptions
114
   * @param string $text
115
   *
116
   * @return string
117
   */
118
  public function buttonFastOrderClose($htmlOptions = array(), $text = '')
119
  {
120
    $this->appendHtmlOption($htmlOptions, $this->classFastOrderCloseButton);
121
122
    return CHtml::link($text, '#', $htmlOptions);
123
  }
124
125 7
  public function buttonSubmitFastOrder($text = '', $htmlOptions = array())
126
  {
127 7
    $this->appendHtmlOption($htmlOptions, $this->classSubmitFastOrderButton);
128
129 7
    return CHtml::button($text, $htmlOptions);
130
  }
131
132
  public function buttonRepeatOrder($orderId, $text = '', $htmlOptions = array())
133
  {
134
    $this->appendHtmlOption($htmlOptions, $this->classRepeatOrderButton);
135
    $this->appendHtmlOption($htmlOptions, $orderId, 'data-order-id');
136
137
    return CHtml::link($text, '#', $htmlOptions);
138
  }
139
140 7
  public function beginFastOrderPopup($htmlOptions = array())
141
  {
142 7
    $this->appendHtmlOption($htmlOptions, $this->fastOrderFormPopupId, 'id');
143 7
    echo CHtml::tag($this->fastOrderPopupContainer, $htmlOptions, false, false);
144 7
  }
145
146 7
  public function endFastOrderPopup()
147
  {
148 7
    echo CHtml::closeTag($this->fastOrderPopupContainer);
149 7
  }
150
151 7
  public function beginTemplate($htmlOptions = array(), $tag = 'div')
152
  {
153 7
    $htmlOptions['id'] = empty($htmlOptions['id']) ? $this->templateId.self::$templateCounter++ : $htmlOptions['id'];
154 7
    $this->templates[$htmlOptions['id']] = '';
155
156 7
    echo CHtml::tag($tag, $htmlOptions, false, false);
157 7
    ob_start();
158 7
  }
159
160 7
  public function endTemplate($tag = 'div')
161
  {
162 7
    end($this->templates);
163 7
    $id = key($this->templates);
164 7
    $this->templates[$id] = trim(strtr(ob_get_contents(), array("\n" => '', "\r" => '')));
165 7
    ob_end_clean();
166
167 7
    echo CHtml::closeTag($tag);
168 7
  }
169
170 7
  protected function registerScripts()
171
  {
172 7
    parent::registerScripts();
173
174 7
    $this->registerScriptButtonFastOrder();
175 7
    $this->registerScriptButtonFastOrderClose();
176 7
    $this->registerScriptButtonSubmitFastOrder();
177 7
    $this->registerScriptButtonRepeatOrder();
178 7
  }
179
180 7
  protected function registerScriptButtonFastOrder()
181
  {
182 7
    $this->registerScript("$('body').on('click', '.{$this->classFastOrderShowButton}', function(e){
183
      e.preventDefault();
184 7
      var templates = ".CJSON::encode($this->templates).";
185
      var element = $(this).clone().data($(this).data());
186
      var data = element.data();
187
188
      if( data != undefined && data['formData'] != undefined )
189
      {
190
        for(templateId in templates)
191
        {
192
          if( templates.hasOwnProperty(templateId) )
193
          {
194
            var template = templates[templateId];
195
196
            for(key in data['formData'])
197
            {
198
              if( data['formData'].hasOwnProperty(key) )
199
              {
200
                var selectorKey = key.match(/(\w+):selector/)
201
202
                if( selectorKey )
203
                {
204
                  var replacedElement = $(data['formData'][key]);
205
                  if( replacedElement.length > 0 )
206
                    template = template.replace(new RegExp('{' + selectorKey[1] + '}', 'gi'), replacedElement.html());
207
                }
208
                else
209
                {
210
                  template = template.replace(new RegExp('{' + key + '}', 'gi'), data['formData'][key]);
211
                }
212
              }
213
            }
214
215
            $('#' + templateId).html(template);
216
          }
217
        }
218
219
        delete data['formData'];
220
      }
221
222
      var showFastOrderPopup = function()
223
      {
224 7
        var target = $('#{$this->fastOrderFormPopupId}');
225
        $.overlayLoader(true, {
226
          node: target,
227
          onShow: function()
228
          {
229
            setTimeout(function() {
230
              target.find('.autofocus-inp').focus();
231
            }, 300);
232
          }
233
         });
234
       }
235
236
      if( parentPopup = $(this).closest('.popup') ) {
237
        $.overlayLoader(false, {node : parentPopup});
238
        setTimeout(function(){
239
          showFastOrderPopup();
240
        }, 300);
241
      }
242
      else {
243
        showFastOrderPopup();
244
      }
245
246 7
      $('#{$this->fastOrderFormId}').show();
247 7
      $('#{$this->fastOrderFormSuccessId}').hide();
248
249 7
      var classSubmitButton = '{$this->classSubmitFastOrderButton}';
250
      $('.' + classSubmitButton).data(data);
251 7
    });");
252 7
  }
253
254 7
  protected function registerScriptButtonFastOrderClose()
255
  {
256 7
    $this->registerScript("$('body').on('click', '.{$this->classFastOrderCloseButton}', function(e){
257
      e.preventDefault();
258 7
      var target = $('#{$this->fastOrderFormPopupId}');
259
      $.overlayLoader(false, target);
260 7
    });");
261 7
  }
262
263 7
  protected function registerScriptButtonSubmitFastOrder()
264
  {
265 7
    $this->registerScript("$('body').on('click', '.{$this->classSubmitFastOrderButton}', function(e){
266
      e.preventDefault();
267
268 7
      var form = $('#{$this->fastOrderFormId}');
269
      var url = form.attr('action');
270 7
      var data = {'{$this->keyCollection}' : $(this).data(), 'action' : 'fastOrder'};
271
272
      $.post(url, $.param(data) + '&' + form.serialize(), function(resp) {
273
        checkResponse(resp, form);
274
      }, 'json');
275 7
    });");
276 7
  }
277
278 7 View Code Duplication
  protected function registerScriptButtonRepeatOrder()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
279
  {
280 7
    $url = Yii::app()->controller->createUrl('basket/repeatOrder');
281
282 7
    $this->registerScript("$('body, .{$this->classRepeatOrderButton}').on('click', '.{$this->classRepeatOrderButton}', function(e){
283
      e.preventDefault();
284
285 7
      var collection = $.fn.collection('{$this->keyCollection}');
286
287
      collection.send({
288 7
        'url' : '{$url}',
289
        'data' : {'order-id' : $(this).data('order-id')}
290
      });
291 7
    });");
292
  }
293
}