-
Notifications
You must be signed in to change notification settings - Fork 37
/
link.class.php
136 lines (122 loc) · 4.62 KB
/
link.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
<?php
/**
* Link resource
*
* @package babel
* @subpackage processors
*/
use mikrobi\Babel\Processors\ObjectUpdateProcessor;
class BabelLinkResourceProcessor extends ObjectUpdateProcessor
{
public $classKey = 'modResource';
public $objectType = 'resource';
public $languageTopics = ['resource', 'babel:default'];
/** @var modResource $targetResource The link target */
protected $targetResource;
/**
* {@inheritDoc}
* @return boolean
*/
public function initialize()
{
$success = parent::initialize();
$target = $this->getProperty('target', false);
if (empty($target)) {
return $this->modx->lexicon($this->objectType . '_err_ns');
}
$primaryKey = $this->getProperty($this->primaryKeyField, false);
if ($target === $primaryKey) {
return $this->modx->lexicon('babel.resource_err_link_of_selflink_not_possible');
}
$this->targetResource = $this->modx->getObject('modResource', $target);
if (!$this->targetResource) {
return $this->modx->lexicon('babel.resource_err_invalid_id', [
'resource' => $target
]);
}
$contextKey = $this->getProperty('context', false);
if (empty($contextKey)) {
return $this->modx->lexicon('babel.context_err_ns');
}
$context = $this->modx->getObject('modContext', ['key' => $contextKey]);
if (!$context) {
return $this->modx->lexicon('babel.context_err_invalid_key', [
'context' => $contextKey
]);
}
if ($this->targetResource->get('context_key') !== $contextKey) {
return $this->modx->lexicon('babel.resource_err_from_other_context', [
'resource' => $this->targetResource->get('id'),
'context' => $contextKey
]);
}
return $success;
}
/**
* {@inheritDoc}
* @return mixed
*/
public function process()
{
$targetResources = $this->babel->getLinkedResources($this->getProperty('target'));
$linkedResources = $this->babel->getLinkedResources($this->object->get('id'));
if (empty($linkedResources)) {
/* always be sure that the Babel TV is set */
$this->babel->initBabelTv($this->object);
}
$context = $this->getProperty('context');
/* add or change a translation link */
if (isset($linkedResources[$context])) {
/* If the existing link has been changed, reset the Babel TV of the old resource */
$this->babel->initBabelTvById($linkedResources[$context]);
}
$linkedResources[$context] = $this->targetResource->get('id');
$syncLinkedTranslations = $this->getProperty('sync-linked-tranlations');
if ($syncLinkedTranslations == 1) {
/* Join all existing linked resources from both resources */
$mergedResources = array_merge($targetResources, $linkedResources);
$this->babel->updateBabelTv($mergedResources, $mergedResources);
} else {
/* Only join between 2 resources */
$mergeLinked = array_merge($linkedResources, [
$this->getProperty('context') => $this->targetResource->get('id')
]);
$this->babel->updateBabelTv($this->object->get('id'), $mergeLinked);
$mergeTarget = array_merge($targetResources, [
$this->object->get('context_key') => $this->object->get('id')
]);
$this->babel->updateBabelTv($this->targetResource->get('id'), $mergeTarget);
}
$copyTvValues = $this->getProperty('copy-tv-values');
if ($copyTvValues == 1) {
/* copy values of synchronized TVs to target resource */
$this->babel->synchronizeTvs($this->object->get('id'));
}
$this->fireLinkEvent();
return $this->cleanup();
}
/**
* Fire the OnBabelLink event
*/
public function fireLinkEvent()
{
$this->modx->invokeEvent('OnBabelLink', [
'context_key' => $this->getProperty('context'),
'original_id' => $this->object->get('id'),
'original_resource' => &$this->object,
'target_id' => $this->targetResource->get('id'),
'target_resource' => &$this->targetResource
]);
}
/**
* {@inheritDoc}
* @return array
*/
public function cleanup()
{
$output = $this->object->toArray();
$output['menu'] = $this->babel->getMenu($this->object);
return $this->success('', $output);
}
}
return 'BabelLinkResourceProcessor';