aboutsummaryrefslogtreecommitdiffhomepage
path: root/main/app/sprinkles/admin/src/Sprunje/UserSprunje.php
blob: 12378f9ac15c9ee7142b1c0b5b2cf819f3fcee05 (plain) (blame)
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
<?php
/**
 * UserFrosting (http://www.userfrosting.com)
 *
 * @link      https://github.com/userfrosting/UserFrosting
 * @license   https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License)
 */
namespace UserFrosting\Sprinkle\Admin\Sprunje;

use Illuminate\Database\Capsule\Manager as Capsule;
use UserFrosting\Sprinkle\Core\Facades\Debug;
use UserFrosting\Sprinkle\Core\Facades\Translator;
use UserFrosting\Sprinkle\Core\Sprunje\Sprunje;

/**
 * UserSprunje
 *
 * Implements Sprunje for the users API.
 *
 * @author Alex Weissman (https://alexanderweissman.com)
 */
class UserSprunje extends Sprunje
{
    protected $name = 'users';

    protected $listable = [
        'status'
    ];

    protected $sortable = [
        'name',
        'last_activity',
        'status'
    ];

    protected $filterable = [
        'name',
        'last_activity',
        'status'
    ];

    protected $excludeForAll = [
        'last_activity'
    ];

    /**
     * {@inheritDoc}
     */
    protected function baseQuery()
    {
        $query = $this->classMapper->createInstance('user');

        // Join user's most recent activity
        return $query->joinLastActivity()->with('lastActivity');
    }

    /**
     * Filter LIKE the last activity description.
     *
     * @param Builder $query
     * @param mixed $value
     * @return $this
     */
    protected function filterLastActivity($query, $value)
    {
        // Split value on separator for OR queries
        $values = explode($this->orSeparator, $value);
        $query->where(function ($query) use ($values) {
            foreach ($values as $value) {
                $query->orLike('activities.description', $value);
            }
        });
        return $this;
    }

    /**
     * Filter LIKE the first name, last name, or email.
     *
     * @param Builder $query
     * @param mixed $value
     * @return $this
     */
    protected function filterName($query, $value)
    {
        // Split value on separator for OR queries
        $values = explode($this->orSeparator, $value);
        $query->where(function ($query) use ($values) {
            foreach ($values as $value) {
                $query->orLike('first_name', $value)
                        ->orLike('last_name', $value)
                        ->orLike('email', $value);
            }
        });
        return $this;
    }

    /**
     * Filter by status (active, disabled, unactivated)
     *
     * @param Builder $query
     * @param mixed $value
     * @return $this
     */
    protected function filterStatus($query, $value)
    {
        // Split value on separator for OR queries
        $values = explode($this->orSeparator, $value);
        $query->where(function ($query) use ($values) {
            foreach ($values as $value) {
                if ($value == 'disabled') {
                    $query->orWhere('flag_enabled', 0);
                } elseif ($value == 'unactivated') {
                    $query->orWhere('flag_verified', 0);
                } elseif ($value == 'active') {
                    $query->orWhere(function ($query) {
                        $query->where('flag_enabled', 1)->where('flag_verified', 1);
                    });
                }
            }
        });
        return $this;
    }

    /**
     * Return a list of possible user statuses.
     *
     * @return array
     */
    protected function listStatus()
    {
        return [
            [
                'value' => 'active',
                'text' => Translator::translate('ACTIVE')
            ],
            [
                'value' => 'unactivated',
                'text' => Translator::translate('UNACTIVATED')
            ],
            [
                'value' => 'disabled',
                'text' => Translator::translate('DISABLED')
            ]
        ];
    }

    /**
     * Sort based on last activity time.
     *
     * @param Builder $query
     * @param string $direction
     * @return $this
     */
    protected function sortLastActivity($query, $direction)
    {
        $query->orderBy('activities.occurred_at', $direction);
        return $this;
    }

    /**
     * Sort based on last name.
     *
     * @param Builder $query
     * @param string $direction
     * @return $this
     */
    protected function sortName($query, $direction)
    {
        $query->orderBy('last_name', $direction);
        return $this;
    }

    /**
     * Sort active, unactivated, disabled
     *
     * @param Builder $query
     * @param string $direction
     * @return $this
     */
    protected function sortStatus($query, $direction)
    {
        $query->orderBy('flag_enabled', $direction)->orderBy('flag_verified', $direction);
        return $this;
    }
}