-
Notifications
You must be signed in to change notification settings - Fork 37
/
publish.class.php
155 lines (136 loc) · 4.91 KB
/
publish.class.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
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Publishes a resource.
*
* @param integer $id The ID of the resource
*
* @package modx
* @subpackage processors.resource
*/
class CollectionsResourcePublishProcessor extends modProcessor
{
/** @var modResource $resource */
public $resource;
/** @var modUser $lockedUser */
public $lockedUser;
public function checkPermissions()
{
return $this->modx->hasPermission('publish_document');
}
public function getLanguageTopics()
{
return array('resource');
}
public function initialize()
{
$id = $this->getProperty('id', false);
if (empty($id)) return $this->modx->lexicon('resource_err_ns');
$this->resource = $this->modx->getObject('modResource', $id);
if (empty($this->resource)) return $this->modx->lexicon('resource_err_nfs', array('id' => $id));
/* validate resource can be deleted */
if (!$this->resource->checkPolicy(array('save' => true, 'publish' => true))) {
return $this->modx->lexicon('permission_denied');
}
return true;
}
public function process()
{
if (!$this->addLock()) {
return $this->failure($this->modx->lexicon('resource_locked_by', array(
'id' => $this->resource->get('id'),
'user' => $this->lockedUser->get('username'),
)));
}
$duplicateAlias = $this->checkForDuplicateAlias();
if ($duplicateAlias !== false) {
return $this->failure($duplicateAlias);
}
/* publish resource */
$this->resource->set('published', true);
$this->resource->set('pub_date', false);
$this->resource->set('unpub_date', false);
$this->resource->set('editedby', $this->modx->user->get('id'));
$this->resource->set('editedon', time(), 'integer');
$this->resource->set('publishedby', $this->modx->user->get('id'));
$this->resource->set('publishedon', time());
$saved = $this->resource->save();
$this->resource->removeLock();
if (!$saved) return $this->failure($this->modx->lexicon('resource_err_publish'));
$this->fireAfterPublish();
$this->logManagerAction();
$skipClearCache = $this->getProperty('skipClearCache', false);
if ($skipClearCache == false) {
$this->clearCache();
}
return $this->success('', $this->resource->get(array('id', 'pub_date', 'unpub_date', 'editedby', 'editedon', 'publishedby', 'publishedon')));
}
/**
* Attempt to lock the Resource
* @return boolean
*/
public function addLock()
{
$locked = $this->resource->addLock();
if ($locked !== true) {
$this->lockedUser = $this->modx->getObject('modUser', $locked);
if ($this->lockedUser) {
$locked = false;
}
}
return $locked;
}
/**
* Check for a duplicate alias before publishing
* @return boolean|string
*/
public function checkForDuplicateAlias()
{
$duplicateAlias = false;
/* get the targeted working context */
$workingContext = $this->modx->getContext($this->resource->get('context_key'));
/* friendly url duplicate alias checks */
if ($workingContext->getOption('friendly_urls', false)) {
$duplicateContext = $workingContext->getOption('global_duplicate_uri_check', false) ? '' : $this->resource->get('context_key');
$aliasPath = $this->resource->getAliasPath($this->resource->get('alias'));
$duplicateId = $this->resource->isDuplicateAlias($aliasPath, $duplicateContext);
if (!empty($duplicateId)) {
return $this->modx->lexicon('duplicate_uri_found', array('id' => $duplicateId, 'uri' => $aliasPath));
}
}
return $duplicateAlias;
}
/**
* Fire after-publish events
* @return void
*/
public function fireAfterPublish()
{
$this->modx->invokeEvent('OnDocPublished', array(
'docid' => $this->resource->get('id'),
'id' => $this->resource->get('id'),
'resource' => &$this->resource,
));
}
/**
* Log a manager action
* @return void
*/
public function logManagerAction()
{
$this->modx->logManagerAction('publish_resource', $this->resource->get('class_key'), $this->resource->get('id'));
}
/**
* Clear the site cache
* @return void
*/
public function clearCache()
{
$this->modx->cacheManager->refresh(array(
'db' => array(),
'auto_publish' => array('contexts' => array($this->resource->get('context_key'))),
'context_settings' => array('contexts' => array($this->resource->get('context_key'))),
'resource' => array('contexts' => array($this->resource->get('context_key'))),
));
}
}
return 'CollectionsResourcePublishProcessor';