|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Routes. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Zikula contributors (Zikula) |
|
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
|
7
|
|
|
* @author Zikula contributors <[email protected]>. |
|
8
|
|
|
* @link http://www.zikula.org |
|
9
|
|
|
* @link http://zikula.org |
|
10
|
|
|
* @version Generated by ModuleStudio 0.7.1 (http://modulestudio.de). |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Zikula\RoutesModule\Traits; |
|
14
|
|
|
|
|
15
|
|
|
use ServiceUtil; |
|
16
|
|
|
use Zikula_Workflow_Util; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Workflow trait implementation class. |
|
20
|
|
|
*/ |
|
21
|
|
|
trait EntityWorkflowTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array The current workflow data of this object |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $__WORKFLOW__ = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns the __ w o r k f l o w__. |
|
30
|
|
|
* |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
public function get__WORKFLOW__() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->__WORKFLOW__; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Sets the __ w o r k f l o w__. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $__WORKFLOW__ |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function set__WORKFLOW__($__WORKFLOW__ = []) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->__WORKFLOW__ = $__WORKFLOW__; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns the name of the primary identifier field. |
|
52
|
|
|
* For entities with composite keys the first identifier field is used. |
|
53
|
|
|
* |
|
54
|
|
|
* @return string Identifier field name |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getWorkflowIdColumn() |
|
57
|
|
|
{ |
|
58
|
|
|
$entityClass = 'ZikulaRoutesModule:' . ucfirst($this->get_objectType()) . 'Entity'; |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$entityManager = ServiceUtil::get('doctrine.orm.default_entity_manager'); |
|
61
|
|
|
$meta = $entityManager->getClassMetadata($entityClass); |
|
62
|
|
|
|
|
63
|
|
|
return $meta->getSingleIdentifierFieldName(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Sets/retrieves the workflow details. |
|
68
|
|
|
* |
|
69
|
|
|
* @param boolean $forceLoading load the workflow record |
|
70
|
|
|
* |
|
71
|
|
|
* @throws RuntimeException Thrown if retrieving the workflow object fails |
|
72
|
|
|
*/ |
|
73
|
|
|
public function initWorkflow($forceLoading = false) |
|
74
|
|
|
{ |
|
75
|
|
|
$request = ServiceUtil::get('request_stack')->getCurrentRequest(); |
|
76
|
|
|
$routeName = $request->get('_route'); |
|
77
|
|
|
|
|
78
|
|
|
$loadingRequired = false !== strpos($routeName, 'edit') || false !== strpos($routeName, 'delete'); |
|
79
|
|
|
$isReuse = $request->query->getBoolean('astemplate', false); |
|
80
|
|
|
|
|
81
|
|
|
$serviceManager = ServiceUtil::getManager(); |
|
82
|
|
|
$translator = $serviceManager->get('translator.default'); |
|
83
|
|
|
$workflowHelper = $serviceManager->get('zikula_routes_module.workflow_helper'); |
|
84
|
|
|
|
|
85
|
|
|
$objectType = $this->get_objectType(); |
|
|
|
|
|
|
86
|
|
|
$idColumn = $this->getWorkflowIdColumn(); |
|
87
|
|
|
|
|
88
|
|
|
// apply workflow with most important information |
|
89
|
|
|
$schemaName = $workflowHelper->getWorkflowName($objectType); |
|
90
|
|
|
$this['__WORKFLOW__'] = [ |
|
91
|
|
|
'module' => 'ZikulaRoutesModule', |
|
92
|
|
|
'state' => $this->getWorkflowState(), |
|
|
|
|
|
|
93
|
|
|
'obj_table' => $objectType, |
|
94
|
|
|
'obj_idcolumn' => $idColumn, |
|
95
|
|
|
'obj_id' => $this[$idColumn], |
|
96
|
|
|
'schemaname' => $schemaName |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
// load the real workflow only when required (e. g. when func is edit or delete) |
|
100
|
|
|
if (($loadingRequired && !$isReuse) || $forceLoading) { |
|
101
|
|
|
$result = Zikula_Workflow_Util::getWorkflowForObject($this, $objectType, $idColumn, 'ZikulaRoutesModule'); |
|
102
|
|
|
if (!$result) { |
|
103
|
|
|
$flashBag = $serviceManager->get('session')->getFlashBag(); |
|
104
|
|
|
$flashBag->add('error', $translator->__('Error! Could not load the associated workflow.')); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (!is_object($this['__WORKFLOW__']) && !isset($this['__WORKFLOW__']['schemaname'])) { |
|
109
|
|
|
$workflow = $this['__WORKFLOW__']; |
|
110
|
|
|
$workflow['schemaname'] = $schemaName; |
|
111
|
|
|
$this['__WORKFLOW__'] = $workflow; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Resets workflow data back to initial state. |
|
117
|
|
|
* This is for example used during cloning an entity object. |
|
118
|
|
|
*/ |
|
119
|
|
|
public function resetWorkflow() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->setWorkflowState('initial'); |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
$workflowHelper = ServiceUtil::get('zikula_routes_module.workflow_helper'); |
|
124
|
|
|
|
|
125
|
|
|
$schemaName = $workflowHelper->getWorkflowName($this->get_objectType()); |
|
|
|
|
|
|
126
|
|
|
$this['__WORKFLOW__'] = [ |
|
127
|
|
|
'module' => 'ZikulaRoutesModule', |
|
128
|
|
|
'state' => $this->getWorkflowState(), |
|
|
|
|
|
|
129
|
|
|
'obj_table' => $this->get_objectType(), |
|
|
|
|
|
|
130
|
|
|
'obj_idcolumn' => $this->getWorkflowIdColumn(), |
|
131
|
|
|
'obj_id' => 0, |
|
132
|
|
|
'schemaname' => $schemaName |
|
133
|
|
|
]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.