pyrocms /system/cms/modules/navigation/src/Pyro/Module/Navigation/Model/Link.php

Language PHP Lines 380
MD5 Hash 869b33f32d1cb715cef25e02a0786bec
Repository https://github.com/asalem/pyrocms.git View Raw File View Project SPDX
  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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php namespace Pyro\Module\Navigation\Model;

use Pyro\Model\Eloquent;
use Pyro\Module\Navigation\Model\Group;
use Pyro\Module\Pages\Model\Page;
use Pyro\Module\Users;

/**
 * Navigation model for the navigation module.
 *
 * @author      PyroCMS Dev Team
 * @package     PyroCMS\Core\Modules\Navigation\Models
 */
class Link extends Eloquent
{
    /**
     * Define the table name
     *
     * @var string
     */
    protected $table = 'navigation_links';

    /**
     * Cache minutes
     * @var int
     */
    public $cacheMinutes = 30;

    /**
     * The attributes that aren't mass assignable
     *
     * @var array
     */
    protected $guarded = array();

    /**
     * Disable updated_at and created_at on table
     *
     * @var boolean
     */
    public $timestamps = false;

    /**
     * Relationship: Children
     *
     * @return Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany('Pyro\Module\Navigation\Model\Link', 'parent')->orderBy('position', 'ASC');
    }

    /**
     * Relationship: Page
     *
     * @return Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function page()
    {
        return $this->belongsTo('Pyro\Module\Pages\Model\Page');
    }

    /**
     * Get a navigation link with all the trimmings
     *
     *
     * @param  int   $id The ID of the item
     * @return mixed
     */
    public static function getUrl($id = 0)
    {
        $link = parent::find($id);

        if (! $link) {
            return;
        }

        return self::makeUrl($link);
    }

    /**
     * Find link by Group ID and order by position
     *
     * @param int    $group_id  The group_id of the link
     * @param string $direction The direction of the sort
     *
     * @return void
     */
    public static function findByGroupIdAndOrderByPosition($group_id, $direction = 'asc')
    {
        return static::where('navigation_group_id', '=', $group_id)->orderBy('position',$direction)->first();
    }

    /**
     * Get links by Group ID and order by position
     *
     * @param int    $group_id  The group_id of the links
     * @param string $direction The direction of the sort
     *
     * @return void
     */
    public static function findManyByGroupIdAndOrderByPosition($group_id, $direction = 'asc')
    {
        return static::where('navigation_group_id', '=', $group_id)->orderBy('position',$direction)->get();
    }

    /**
     * Reset the parent > child
     *
     * @param int $parent_id The parent_id of the link
     *
     * @return void
     */
    public static function resetChildByParentId($parent_id)
    {
        return static::where('parent', '=', $parent_id)->update(array('parent'=> 0));
    }

    /**
     * Build a multi-array of parent > children.
     *
     * @param  string $group Either the group abbrev or the group id
     * @return array  An array representing the link tree
     */
    public static function getTreeByGroup($group, $params = array())
    {
        $front_end = (isset($params['front_end']) and $params['front_end']);

        $user_groups = (isset($params['user_groups'])) ? $params['user_groups'] : false;

        // By wuuut?
        if (is_numeric($group)) {
            $group = Group::with('links.children', 'links.page')->where('id', '=', $group)->first();
        } else {
            $group = Group::with('links.children', 'links.page')->where('abbrev', '=', $group)->first();
        }

        $all_links = $group->links;

        $all_links = self::makeUrlArray($all_links, $user_groups, $front_end);

        return $all_links;
    }

    /**
     * Set order of links
     *
     * @param  array $link
     * @return void
     */
    public static function setOrder($order = array(), $group = null)
    {
        //reset all parent > child relations
        $group = Group::find($group);
        foreach ($group->links as $link) {
            $link->parent = 0;
            $link->save();
        }

        foreach ($order as $i => $link) {
            //set the order of the root links
            ci()->pdb
                ->table('navigation_links')
                ->where('id', str_replace('link_', '', $link['id']))
                ->update(array('position' => $i));

            //iterate through children and set their order and parent
            self::setChildren($link);
        }
    }

    /**
     * Set the parent > child relations and child order
     *
     * @param  array $link
     * @return void
     */
    public static function setChildren($link)
    {
        if (isset($link['children'])) {
            foreach ($link['children'] as $i => $child) {

                ci()->pdb
                    ->table('navigation_links')
                    ->where('id', str_replace('link_', '', $child['id']))
                    ->update(
                        array(
                            'parent' => str_replace('link_', '', $link['id']),
                            'position' => $i
                        )
                    );

                //repeat as long as there are children
                if (isset($child['children'])) {
                    self::setChildren($child);
                }
            }
        }
    }

    /**
     * Get a link's children IDs
     *
     * @param  array $link
     * @return void
     */
    public static function getChildrenIds($parent_id = null)
    {
        $children = ci()->pdb->table('navigation_links')
            ->where('parent', '=', $parent_id)->get();

        $ids = array();

        foreach ($children as $child) {
            $ids[] = $child->id;
            $ids = array_merge($ids, self::getChildrenIds($child->id));
        }

        return $ids;
    }

    /**
     * Delete a link and its children
     *
     * @param  int   $id The ID of the link to delete
     * @return array
     */
    public static function deleteLinkChildren($id = 0)
    {
        $ids = array($id);
        $ids = array_merge($ids, self::getChildrenIds($id));

        return self::whereIn('id', $ids)->delete();
    }

    /**
     * Format an array
     *
     * @param  array $input The data to format
     * @return array
     */
    public static function formatArray($input)
    {
        // If the url is not empty and not just the default http://
        if ( ! empty($input['url']) && $input['url'] != 'http://') {
            $input['uri'] = '';
            $input['module_name'] = '';
            $input['page_id'] = 0;
        }

        // If the uri is empty reset the others
        if ( ! empty($input['uri'])) {
            $input['url'] = '';
            $input['module_name'] = '';
            $input['page_id'] = 0;
        }

        // You get the idea...
        if ( ! empty($input['module_name'])) {
            $input['url'] = '';
            $input['uri'] = '';
            $input['page_id'] = 0;
        }

        if ( ! empty($input['page_id'])) {
            $input['url'] = '';
            $input['uri'] = '';
            $input['module_name'] = '';
        }

        return $input;
    }

    /**
     * Make URL
     *
     * @param  array $row Navigation record
     * @return mixed Valid url
     */
    public static function makeUrl($row)
    {
        // If its any other type than a URL, it needs some help becoming one
        switch ($row->link_type) {
            case 'uri':
                $row->url = site_url($row->uri);
                break;
            case 'module':
                $row->url = site_url($row->module_name);
                break;
            case 'page':

                if ($status = (is_subclass_of(ci(), 'Public_Controller') ? 'live' : null)) {
                    $page = Page::findByIdAndStatus($row->page_id, $status);
                } else {
                    $page = Page::find($row->page_id);
                }

                $row->url = site_url($page->uri);
                $row->is_home = $page->is_home;
                break;
        }

        return $row;
    }

    /**
     * Make a URL array
     *
     * @param  array $row Array of links
     * @return mixed Array of links with valid urls
     */
    public static function makeUrlArray($links, $user_groups = false, $front_end = false)
    {
        foreach ($links as $key => &$row) {

            // Looks like it's restricted. Let's find out who
            if ($row->restricted_to and $front_end) {

                // Explode!
                $row->restricted_to = (array) explode(',', $row->restricted_to);

                // Get the similarities
                $matches = $user_groups != false ? array_intersect($row->restricted_to, $user_groups) : $row->restricted_to;

                // Test it..
                if (empty($user_groups) or (in_array(1, $user_groups) == false and empty($matches))) {
                    unset($links[$key]);
                }
            }

            // If its any other type than a URL, it needs some help becoming one
            switch ($row->link_type) {
                case 'uri':
                    $row->url = site_url($row->uri);
                    break;
                case 'module':
                    $row->url = site_url($row->module_name);
                    break;
                case 'page':

                    if ($row->page and $row->page->status == 'live') {
                        $page = $row->page;
                    } else {
                        $page = false;
                    }

                    // Fuck this then
                    if (! $page) {
                        unset($links[$key]);
                        break;
                    }

                    $row->url = site_url($page->uri);
                    $row->is_home = $page->is_home;

                    // But wait. If we're on the front-end and they don't have access to the page then we'll remove it anyway.
                    if ($front_end and $page->restricted_to) {

                        // EXPLODE again!
                        $page->restricted_to = (array) explode(',', $page->restricted_to);

                        // Get the similarities
                        $matches = array_intersect($row->restricted_to, $user_groups);

                        if ( (empty($user_groups) and $page->restricted_to) or (in_array(1, $user_groups) == false and empty($matches))) {
                            unset($links[$key]);
                        }
                    }
                    break;
            }

            // Process all the little children
            $row->children = self::makeUrlArray($row->children, $user_groups, $front_end);
        }

        return $links;
    }

}
Back to Top