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.
Completed
Pull Request — master (#215)
by
unknown
33:11
created

BContactController::sortFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Nikita Melnikov <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2014 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 * @package backend.modules.contact.controllers
8
 */
9
class BContactController extends BController
10
{
11
  public $name = 'Контакты';
12
13
  public $modelClass = 'BContact';
14
15
  public $position = 1;
16
17
  public function actionUpdate($id)
18
  {
19
    $scriptUrl = Yii::app()->assetManager->publish(GlobalConfig::instance()->backendPath . '/../js/src/for_modules');
20
    Yii::app()->clientScript->registerScriptFile($scriptUrl . '/contact/form.js', CClientScript::POS_END);
21
22
    parent::actionUpdate($id);
23
  }
24
25
  /**
26
   * Сохранение сортировки
27
   */
28
  public function actionSort()
29
  {
30
    switch( Yii::app()->request->getPost('type') )
31
    {
32
      case 'field':
33
        $this->sortFields();
34
        break;
35
      case 'textblock':
36
        $this->sortTextBlocks();
37
        break;
38
    }
39
  }
40
41
  /**
42
   * Удаление поля из группы полей
43
   */
44
  public function actionDelete()
45
  {
46
    $data = Yii::app()->request->getPost('delete');
47
48
    if( !empty($data['id']) )
49
    {
50
      /**
51
       * Может приходить всего 3 типа данных на удаление
52
       * field - поле в группе
53
       * group - группа полей
54
       * textbloc - текстовой блок
55
       */
56
      switch( $data['type'] )
57
      {
58
        case 'field':
59
          $field = BContactField::model()->findByPk($data['id']);
60
          $field->delete();
61
          break;
62
63
        // Для группы полей так же удаляем все дочерние элементы
64
        case 'group':
65
          $group = BContactGroup::model()->findByPk($data['id']);
66
          $group->delete();
67
          break;
68
69
        case 'textblock':
70
          $textblock = BContactTextBlock::model()->findByPk($data['id'])->delete();
0 ignored issues
show
Unused Code introduced by
$textblock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
          break;
72
      }
73
    }
74
  }
75
76
  /**
77
   * Сортировка полей группы
78
   */
79 View Code Duplication
  protected function sortFields()
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...
80
  {
81
    foreach( Yii::app()->request->getPost('sort') as $sortItem )
82
    {
83
      $field           = BContactField::model()->findByPk($sortItem['id']);
84
      $field->position = $sortItem['position'];
85
      $field->save();
86
    }
87
  }
88
89
  /**
90
   * Сортировка текстовых блоков
91
   */
92 View Code Duplication
  protected function sortTextBlocks()
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...
93
  {
94
    foreach( Yii::app()->request->getPost('sort') as $sortItem )
95
    {
96
      $textblock = BContactTextBlock::model()->findByPk($sortItem['id']);
97
      $textblock->position = $sortItem['position'];
98
      $textblock->save();
99
    }
100
  }
101
102
  /**
103
   * @inheritdoc
104
   */
105
  protected function saveModels($models, $extendedSave = true, $redirectToUpdate = true)
106
  {
107
    $model = Arr::reset($models);
108
109
    Yii::app()->getClientScript()->registerCoreScript( 'jquery.ui' );
110
111
    $this->performAjaxValidation($model);
112
    $attributes = Yii::app()->request->getPost(get_class($model));
113
114
    if( isset($attributes) )
115
    {
116
      $model->setAttributes($attributes);
117
118
      if( $model->save() )
119
      {
120
        $this->saveGroup($model);
121
        $this->saveField();
122
123
        $this->saveTextblock($model);
124
125
        if( $redirectToUpdate )
126
        {
127
          Yii::app()->user->setFlash('success', 'Запись успешно '.($model->isNewRecord ? 'создана' : 'сохранена').'.');
128
129
          if( Yii::app()->request->getParam('action') )
130
            $this->redirect($this->getBackUrl());
131
          else
132
            $this->redirect(array('update', 'id' => $model->id));
133
        }
134
      }
135
    }
136
  }
137
138
  /**
139
   * Сохранение текстовых блоков для записи контактов
140
   *
141
   * @param BContact $model
142
   */
143
  protected function saveTextblock(BContact $model)
144
  {
145
    $textblocks = Yii::app()->request->getPost('ContactTextBlock');
146
147
    if( !empty($textblocks['new']) )
148
    {
149
      $this->createTextBlock($model, $textblocks['new']);
150
      unset($textblocks['new']);
151
    }
152
153
    if( !empty($textblocks) )
154
    {
155
      foreach( $textblocks as $id => $values )
156
      {
157
        $textblock = BContactTextBlock::model()->findByPk($id);
158
159
        if( !empty($values['delete']) )
160
          $textblock->delete();
161
        else
162
        {
163
          if( empty($values['visible']) )
164
          $values['visible'] = 0;
165
166
          $textblock->setAttributes($values);
167
          $textblock->save();
168
        }
169
      }
170
    }
171
  }
172
173
  /**
174
   * Создание новых текстовых блоков
175
   *
176
   * @param BContact $model
177
   * @param array $data
178
   */
179
  public function createTextBlock(BContact $model, array $data)
180
  {
181
    foreach( $data as $item )
182
    {
183
      $textblock = new BContactTextBlock();
184
      $textblock->setAttributes($item);
185
      $textblock->contact_id = $model->id;
186
      $textblock->save();
187
    }
188
  }
189
190
  /**
191
   * Сохранение групп для текущей записи контактов
192
   *
193
   * @param array $model
194
   */
195
  protected function saveGroup($model)
196
  {
197
    $groups = Yii::app()->request->getPost('BContactGroup');
198
199
    if( empty($groups) ) return;
200
201
    foreach( $groups as $group )
202
    {
203
      $contactGroup             = new BContactGroup();
204
      $contactGroup->name       = $group['name'];
205
      $contactGroup->sysname    = $group['sysname'];
206
      $contactGroup->contact_id = $model->id;
207
208
      $contactGroup->save();
209
    }
210
  }
211
212
  /**
213
   * Сохранение полей для групп полей
214
   * Так же осуществляется создание новых полей по ключу 'new'
215
   */
216
  protected function saveField()
217
  {
218
    $fields = Yii::app()->request->getPost('BContactField');
219
220
    if( empty($fields) ) return;
221
222
    foreach( $fields as $id => $field )
223
    {
224
      // Если в качестве ключа используется PK, то обновляем запись
225
      if( is_numeric($id) )
226
      {
227
        $contactField              = BContactField::model()->findByPk($id);
228
        $contactField->value       = $field['value'];
229
        $contactField->description = $field['description'];
230
        $contactField->save();
231
      }
232
      // Если используется ключ new, создаем навые записи
233
      elseif( $id == 'new' )
234
      {
235
        foreach( $field as $newFields )
236
        {
237
          foreach( $newFields as $groupId => $params )
238
          {
239
            $contactField              = new BContactField();
240
            $contactField->value       = $params['value'];
241
            $contactField->description = $params['description'];
242
            $contactField->group_id    = $groupId;
243
            $contactField->save();
244
          }
245
        }
246
      }
247
    }
248
  }
249
}