Language::getSortorder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2011 - 2014 Oleksandr Torosh (http://wezoom.net)
4
 * @author Oleksandr Torosh <[email protected]>
5
 */
6
7
namespace Cms\Model;
8
9
use Application\Mvc\Helper\CmsCache;
10
use Phalcon\DI;
11
use Phalcon\Mvc\Model;
12
use Phalcon\Validation;
13
use Phalcon\Validation\Validator\Uniqueness;
14
use Phalcon\Validation\Validator\PresenceOf;
15
16
class Language extends Model
17
{
18
19
    public function getSource()
20
    {
21
        return "language";
22
    }
23
24
    public $id;
25
    public $iso;
26
    public $locale;
27
    public $name;
28
    public $short_name;
29
    public $url;
30
    public $sortorder;
31
    public $primary;
32
33
    public function validation()
34
    {
35
        $validator = new Validation();
36
37
        /**
38
        * ISO
39
        */
40
        $validator->add('iso', new Uniqueness([
41
            'model' => $this,
42
            "message" => "The inputted ISO language is existing"
43
        ]));
44
        $validator->add('iso', new PresenceOf([
45
            'model' => $this,
46
            'message' => 'ISO is required'
47
        ]));
48
49
        /**
50
        * Name
51
        */
52
        $validator->add('name', new Uniqueness([
53
            'model' => $this,
54
            "message" => "The inputted name is existing"
55
        ]));
56
        $validator->add('name', new PresenceOf([
57
            'model' => $this,
58
            'message' => 'Name is required'
59
        ]));
60
61
        /**
62
        * URL
63
        */
64
        $validator->add('url', new Uniqueness([
65
            'model' => $this,
66
            "message" => "The inputted URL is existing"
67
        ]
68
    ));
69
70
    if ($this->primary == 0) {
71
        $validator->add('url', new PresenceOf([
72
            'model' => $this,
73
            'message' => 'URL is required'
74
        ]));
75
    }
76
77
    return $this->validationHasFailed() != true;
78
}
79
80
    public function afterCreate()
81
    {
82
        $this->sortorder = $this->getUpperSortorder() + 1;
83
84
    }
85
86
    public function afterUpdate()
87
    {
88
        $cache = $this->getDI()->get('cache');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDI() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
89
        $cache->delete(self::cacheKey());
90
    }
91
92
    public function afterSave()
93
    {
94
        CmsCache::getInstance()->save('languages', $this->buildCmsLanguagesCache());
95
        CmsCache::getInstance()->save('translates', Translate::buildCmsTranslatesCache());
96
    }
97
98
    public function afterDelete()
99
    {
100
        CmsCache::getInstance()->save('languages', $this->buildCmsLanguagesCache());
101
        CmsCache::getInstance()->save('translates', Translate::buildCmsTranslatesCache());
102
    }
103
104
    private function buildCmsLanguagesCache()
105
    {
106
        $modelsManager = DI::getDefault()->get('modelsManager');
0 ignored issues
show
Bug introduced by
The method get cannot be called on \Phalcon\DI::getDefault() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
107
        $qb = $modelsManager->createBuilder();
108
        $qb->from('Cms\Model\Language');
109
        $qb->orderBy('primary DESC, sortorder ASC');
110
111
        $entries = $qb->getQuery()->execute();
112
        $save = [];
113
        if ($entries->count()) {
114
            foreach ($entries as $el) {
115
                $save[$el->getIso()] = [
116
                    'id'         => $el->getId(),
117
                    'iso'        => $el->getIso(),
118
                    'locale'     => $el->getLocale(),
119
                    'name'       => $el->getName(),
120
                    'short_name' => $el->getShort_name(),
121
                    'url'        => $el->getUrl(),
122
                    'primary'    => $el->getPrimary(),
123
                ];
124
            }
125
        }
126
        return $save;
127
    }
128
129
    public function afterValidation()
130
    {
131
        if (!$this->sortorder) {
132
            $this->sortorder = $this->getUpperSortorder();
133
        }
134
135
    }
136
137
    public function afterValidationOnCreate()
138
    {
139
        $this->sortorder = $this->getUpperSortorder() + 1;
140
141
    }
142
143
    public static function findCachedLanguages()
144
    {
145
        return CmsCache::getInstance()->get('languages');
146
    }
147
148
    public static function findCachedLanguagesIso()
149
    {
150
        $languages = self::findCachedLanguages();
151
        $iso_array = [];
152
        if (!empty($languages)) {
153
            foreach ($languages as $lang) {
154
                $iso_array[] = $lang['iso'];
155
            }
156
        }
157
        return $iso_array;
158
    }
159
160
    public static function findCachedByIso($iso)
161
    {
162
        $languages = self::findCachedLanguages();
163
        foreach ($languages as $lang) {
164
            if ($iso == $lang['iso']) {
165
                return $lang;
166
            }
167
        }
168
    }
169
170
    public static function cacheKey()
171
    {
172
        return HOST_HASH . md5('Language::findCachedLanguages');
173
    }
174
175
    public function getUpperSortorder()
176
    {
177
        $count = self::count();
178
        return $count;
179
180
    }
181
182
    public function setOnlyOnePrimary()
183
    {
184
        if ($this->getPrimary() == 1) {
185
            $languages = $this->find();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $languages is correct as $this->find() (which targets Phalcon\Mvc\Model::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
186
            foreach ($languages as $lang) {
0 ignored issues
show
Bug introduced by
The expression $languages of type null is not traversable.
Loading history...
187
                if ($lang->getId() != $this->getId()) {
188
                    $lang->setPrimary(0);
189
                    $lang->save();
190
                }
191
            }
192
        } else {
193
            $primary = $this->findFirst("primary = '1'");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $primary is correct as $this->findFirst('primary = \'1\'') (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
194
            if (!$primary) {
195
                $this->setPrimary(1);
196
                $this->save();
197
                $this->getDI()->get('flash')->notice('There should always be a primary language');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDI() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
198
            }
199
        }
200
    }
201
202
    /**
203
     * @param mixed $id
204
     */
205
    public function setId($id)
206
    {
207
        $this->id = $id;
208
    }
209
210
    /**
211
     * @return mixed
212
     */
213
    public function getId()
214
    {
215
        return $this->id;
216
    }
217
218
    /**
219
     * @param mixed $iso
220
     */
221
    public function setIso($iso)
222
    {
223
        $this->iso = $iso;
224
    }
225
226
    /**
227
     * @return mixed
228
     */
229
    public function getIso()
230
    {
231
        return $this->iso;
232
    }
233
234
    /**
235
     * @param mixed $name
236
     */
237
    public function setName($name)
238
    {
239
        $this->name = $name;
240
    }
241
242
    /**
243
     * @return mixed
244
     */
245
    public function getName()
246
    {
247
        return $this->name;
248
    }
249
250
    /**
251
     * @param mixed $sortorder
252
     */
253
    public function setSortorder($sortorder)
254
    {
255
        $this->sortorder = $sortorder;
256
    }
257
258
    /**
259
     * @return mixed
260
     */
261
    public function getSortorder()
262
    {
263
        return $this->sortorder;
264
    }
265
266
    /**
267
     * @param mixed $url
268
     */
269
    public function setUrl($url)
270
    {
271
        $this->url = $url;
272
    }
273
274
    /**
275
     * @return mixed
276
     */
277
    public function getUrl()
278
    {
279
        return $this->url;
280
    }
281
282
    /**
283
     * @param mixed $primary
284
     */
285
    public function setPrimary($primary)
286
    {
287
        $this->primary = $primary;
288
    }
289
290
    /**
291
     * @return mixed
292
     */
293
    public function getPrimary()
294
    {
295
        return $this->primary;
296
    }
297
298
    /**
299
     * @param mixed $short_name
300
     */
301
    public function setShort_name($short_name)
302
    {
303
        $this->short_name = $short_name;
304
    }
305
306
    /**
307
     * @return mixed
308
     */
309
    public function getShort_name()
310
    {
311
        return $this->short_name;
312
    }
313
314
    /**
315
     * @param mixed $locale
316
     */
317
    public function setLocale($locale)
318
    {
319
        $this->locale = $locale;
320
    }
321
322
    /**
323
     * @return mixed
324
     */
325
    public function getLocale()
326
    {
327
        return $this->locale;
328
    }
329
330
}
331