TaskManagerElementType::getStatuses()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Craft;
4
5
/**
6
 * Task Manager.
7
 *
8
 * @author    Bob Olde Hampsink <[email protected]>
9
 * @copyright Copyright (c) 2015, Bob Olde Hampsink
10
 * @license   MIT
11
 *
12
 * @link      http://github.com/boboldehampsink
13
 */
14
class TaskManagerElementType extends BaseElementType
15
{
16
    /**
17
     * Returns the element type name.
18
     *
19
     * @return string
20
     */
21
    public function getName()
22
    {
23
        return Craft::t('Task Manager');
24
    }
25
26
    /**
27
     * Returns whether this element type has content.
28
     *
29
     * @return bool
30
     */
31
    public function hasContent()
32
    {
33
        return false;
34
    }
35
36
    /**
37
     * Returns whether this element type has titles.
38
     *
39
     * @return bool
40
     */
41
    public function hasTitles()
42
    {
43
        return false;
44
    }
45
46
    /**
47
     * Returns whether this element type has statuses.
48
     *
49
     * @return bool
50
     */
51
    public function hasStatuses()
52
    {
53
        return true;
54
    }
55
56
    /**
57
     * Returns this element's statuses.
58
     *
59
     * @return array
60
     */
61
    public function getStatuses()
62
    {
63
        return array(
64
            EntryModel::PENDING => Craft::t('Pending'),
65
            EntryModel::EXPIRED => Craft::t('Error'),
66
            EntryModel::LIVE    => Craft::t('Running'),
67
        );
68
    }
69
70
    /**
71
     * Return sources.
72
     *
73
     * @param string $context
74
     *
75
     * @return array
76
     */
77
    public function getSources($context = null)
78
    {
79
        $sources = array(
80
            '*' => array(
81
                'label'    => Craft::t('All tasks'),
82
                'criteria' => array('editable' => true),
83
            ),
84
        );
85
86
        // Get sources by type
87
        $results = craft()->db->createCommand()
88
            ->select('id, type')
89
            ->from('tasks')
90
            ->order('root asc, lft asc')
91
            ->group('type')
92
            ->queryAll();
93
        foreach ($results as $result) {
94
            $sources['type:'.$result['type']] = array(
95
                'label' => $result['type'],
96
                'data' => array('id' => $result['id']),
97
                'criteria' => array('type' => $result['type'], 'editable' => true),
98
            );
99
        }
100
101
        // Allow plugins to modify the sources
102
        craft()->plugins->call('modifyTaskManagerSources', array(&$sources, $context));
103
104
        return $sources;
105
    }
106
107
    /**
108
     * @inheritDoc IElementType::getAvailableActions()
109
     *
110
     * @param string|null $source
111
     *
112
     * @return array|null
113
     */
114
    public function getAvailableActions($source = null)
115
    {
116
        $actions = array();
117
118
        $restartAction = craft()->elements->getAction('TaskManager_Restart');
119
        $restartAction->setParams(array(
120
            'confirmationMessage' => Craft::t('Are you sure you want to restart the selected task(s)?'),
121
            'successMessage' => Craft::t('Task(s) restarted'),
122
        ));
123
        $actions[] = $restartAction;
124
125
        $deleteAction = craft()->elements->getAction('TaskManager_Delete');
126
        $deleteAction->setParams(array(
127
            'confirmationMessage' => Craft::t('Are you sure you want to delete the selected task(s)?'),
128
            'successMessage' => Craft::t('Task(s) deleted'),
129
        ));
130
        $actions[] = $deleteAction;
131
132
        // Allow plugins to add additional actions
133
        $allPluginActions = craft()->plugins->call('addTaskManagerActions', array($source), true);
134
135
        foreach ($allPluginActions as $pluginActions) {
136
            $actions = array_merge($actions, $pluginActions);
137
        }
138
139
        return $actions;
140
    }
141
142
    /**
143
     * @inheritDoc IElementType::defineAvailableTableAttributes()
144
     *
145
     * @return array
146
     */
147
    public function defineAvailableTableAttributes()
148
    {
149
        $attributes = array(
150
            'description' => Craft::t('Description'),
151
            'type'        => Craft::t('Type'),
152
            'dateCreated' => Craft::t('Created'),
153
            'currentStep' => Craft::t('Current step'),
154
            'totalSteps'  => Craft::t('Total steps'),
155
        );
156
157
        // Allow plugins to modify the attributes
158
        $pluginAttributes = craft()->plugins->call('defineAdditionalTaskManagerTableAttributes', array(), true);
159
        foreach ($pluginAttributes as $thisPluginAttributes) {
160
            $attributes = array_merge($attributes, $thisPluginAttributes);
161
        }
162
163
        return $attributes;
164
    }
165
166
    /**
167
     * @inheritDoc IElementType::getDefaultTableAttributes()
168
     *
169
     * @param string|null $source
170
     *
171
     * @return array
172
     */
173
    public function getDefaultTableAttributes($source = null)
174
    {
175
        return array(
176
            'description',
177
            'type',
178
            'dateCreated',
179
            'currentStep',
180
            'totalSteps',
181
        );
182
    }
183
184
    /**
185
     * Get table attribute html.
186
     *
187
     * @param BaseElementModel $element
188
     * @param string           $attribute
189
     *
190
     * @return string
191
     */
192
    public function getTableAttributeHtml(BaseElementModel $element, $attribute)
193
    {
194
        // First give plugins a chance to set this
195
        $pluginAttributeHtml = craft()->plugins->callFirst('getTaskManagerTableAttributeHtml', array($element, $attribute), true);
196
197
        if ($pluginAttributeHtml !== null) {
198
            return $pluginAttributeHtml;
199
        }
200
201
        // Or format default
202
        return parent::getTableAttributeHtml($element, $attribute);
203
    }
204
205
    /**
206
     * Define sortable attributes.
207
     *
208
     * @param string $source
209
     *
210
     * @return array
211
     */
212
    public function defineSortableAttributes($source = null)
213
    {
214
        $attributes = array(
215
            'type'        => Craft::t('Type'),
216
            'dateCreated' => Craft::t('Created'),
217
        );
218
219
        // Allow plugins to modify the attributes
220
        craft()->plugins->call('modifyTaskManagerSortableAttributes', array(&$attributes));
221
222
        return $attributes;
223
    }
224
225
    /**
226
     * Defines any custom element criteria attributes for this element type.
227
     *
228
     * @return array
229
     */
230
    public function defineCriteriaAttributes()
231
    {
232
        return array(
233
            'type'   => AttributeType::String,
234
            'dateCreated' => AttributeType::DateTime,
235
        );
236
    }
237
238
    /**
239
     * Modifies an element query targeting elements of this type.
240
     *
241
     * @param DbCommand            $query
242
     * @param ElementCriteriaModel $criteria
243
     *
244
     * @return mixed
245
     */
246
    public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria)
247
    {
248
        // Default query
249
        $query
250
            ->select('id, currentStep, totalSteps, status, type, description, settings, dateCreated')
251
            ->from('tasks elements');
252
253
        // Reset default element type query parts
254
        $query->setJoin('');
255
        $query->setWhere('1=1');
256
        $query->setGroup('');
257
        unset($query->params[':locale']);
258
        unset($query->params[':elementsid1']);
259
260
        if ($criteria->type) {
261
            $query->andWhere(DbHelper::parseParam('type', $criteria->type, $query->params));
262
        }
263
264
        // Add search capabilities
265
        if ($criteria->search) {
266
            $query->andWhere(DbHelper::parseParam('description', '*'.$criteria->search.'*', $query->params));
267
            $criteria->search = null;
268
        }
269
    }
270
271
    /**
272
     * Populates an element model based on a query result.
273
     *
274
     * @param array $row
275
     *
276
     * @return array
277
     */
278
    public function populateElementModel($row)
279
    {
280
        return TaskManagerModel::populateModel($row);
281
    }
282
}
283