/
ResaveElementsTask.php
142 lines (121 loc) · 3.17 KB
/
ResaveElementsTask.php
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
<?php
namespace Craft;
/**
* Resave Elements Task
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @copyright Copyright (c) 2014, Pixel & Tonic, Inc.
* @license http://craftcms.com/license Craft License Agreement
* @see http://craftcms.com
* @package craft.app.tasks
* @since 2.0
*/
class ResaveElementsTask extends BaseTask
{
// Properties
// =========================================================================
/**
* @var
*/
private $_elementType;
/**
* @var
*/
private $_localeId;
/**
* @var
*/
private $_elementIds;
// Public Methods
// =========================================================================
/**
* @inheritDoc ITask::getDescription()
*
* @return string
*/
public function getDescription()
{
$elementType = craft()->elements->getElementType($this->getSettings()->elementType);
return Craft::t('Resaving {type}', array(
'type' => StringHelper::toLowerCase($elementType->getName())
));
}
/**
* @inheritDoc ITask::getTotalSteps()
*
* @return int
*/
public function getTotalSteps()
{
$settings = $this->getSettings();
// Let's save ourselves some trouble and just clear all the caches for this element type
craft()->templateCache->deleteCachesByElementType($settings->elementType);
// Now find the affected element IDs
$criteria = craft()->elements->getCriteria($settings->elementType, $settings->criteria);
$criteria->offset = null;
$criteria->limit = null;
$criteria->order = null;
$this->_elementType = $criteria->getElementType()->getClassHandle();
$this->_localeId = $criteria->locale;
$this->_elementIds = $criteria->ids();
return count($this->_elementIds);
}
/**
* @inheritDoc ITask::runStep()
*
* @param int $step
*
* @return bool
*/
public function runStep($step)
{
try
{
$element = craft()->elements->getElementById($this->_elementIds[$step], $this->_elementType, $this->_localeId);
if (!$element)
{
return true;
}
// (╯°□°)╯︵ ┻━┻
if ($element instanceof EntryModel)
{
$entryType = $element->getType();
if (!$entryType->hasTitleField)
{
$element->getContent()->title = craft()->templates->renderObjectTemplate($entryType->titleFormat, $element);
}
}
if (craft()->elements->saveElement($element, false))
{
return true;
}
else
{
$error = 'Encountered the following validation errors when trying to save '.strtolower($element->getElementType()).' element "'.$element.'" with the ID "'.$element->id.'":';
foreach ($element->getAllErrors() as $attributeError)
{
$error .= "\n - {$attributeError}";
}
return $error;
}
}
catch (\Exception $e)
{
return 'An exception was thrown while trying to save the '.$this->_elementType.' with the ID “'.$this->_elementIds[$step].'”: '.$e->getMessage();
}
}
// Protected Methods
// =========================================================================
/**
* @inheritDoc BaseSavableComponentType::defineSettings()
*
* @return array
*/
protected function defineSettings()
{
return array(
'elementType' => AttributeType::String,
'criteria' => AttributeType::Mixed,
);
}
}