-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_legal.entity_controller.inc
221 lines (191 loc) · 6.36 KB
/
entity_legal.entity_controller.inc
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* @file
* Entity API controller classes for entity_legal module.
*/
/**
* Entity Legal Document controller class.
*/
class EntityLegalDocumentController extends EntityAPIControllerExportable {
/**
* {@inheritdoc}
*/
public function save($entity) {
// When creating a new legal document, add the document text to the bundle.
if (!empty($entity->is_new)) {
$instance = array(
'field_name' => 'entity_legal_document_text',
'entity_type' => ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME,
'bundle' => $entity->identifier(),
'label' => 'Document text',
'widget' => array(
'type' => 'text_textarea_with_summary',
'weight' => 1,
),
'settings' => array('display_summary' => TRUE),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
),
),
);
field_create_instance($instance);
}
$success = parent::save($entity);
// Flush the entity info cache to allow the new bundle to be registered.
entity_info_cache_clear();
return $success;
}
/**
* {@inheritdoc}
*/
public function delete($ids, DatabaseTransaction $transaction = NULL) {
// Delete all associated versions.
foreach ($ids as $document_name) {
$version_query = new EntityFieldQuery();
$version_query->entityCondition('entity_type', ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME)
->propertyCondition('document_name', $document_name);
$version_result = $version_query->execute();
if (!empty($version_result) && !empty($version_result[ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME])) {
foreach (array_keys($version_result[ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME]) as $version_name) {
entity_delete(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME, $version_name);
}
}
// Delete field instance.
$instances = field_read_instances(array(
'entity_type' => 'entity_legal_document_version',
'bundle' => $document_name,
), array(
'include_inactive' => FALSE,
'include_deleted' => FALSE,
));
foreach ($instances as $instance) {
field_delete_instance($instance, FALSE);
}
}
parent::delete($ids, $transaction);
}
/**
* Get all versions of a legal document entity.
*
* @param EntityLegalDocument $entity
* The legal document entity to get versions of.
*
* @return array
* All versions of this legal document entity.
*/
public function getAllVersions(EntityLegalDocument $entity) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME)
->propertyCondition('document_name', $entity->identifier());
$results = $query->execute();
if (!empty($results)) {
return entity_load(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME, array_keys($results[ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME]));
}
else {
return array();
}
}
/**
* Override the document view to instead output the version view.
*/
public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
$entities = entity_key_array_by_property($entities, $this->idKey);
$view = array();
foreach ($entities as $entity) {
$published_version = $entity->getPublishedVersion();
if ($published_version) {
$key = isset($entity->{$this->idKey}) ? $entity->{$this->idKey} : NULL;
$view[$this->entityType][$key] = $published_version->view('full', NULL, TRUE);
}
}
return $view;
}
}
/**
* Legal document version exportable controller.
*/
class EntityLegalDocumentVersionController extends EntityAPIControllerExportable {
/**
* {@inheritdoc}
*/
public function create(array $values = array()) {
global $user;
if (empty($values['uid'])) {
$values['uid'] = $user->uid;
}
if (empty($values['created'])) {
$values['created'] = time();
}
return parent::create($values);
}
/**
* {@inheritdoc}
*/
public function save($entity, DatabaseTransaction $transaction = NULL) {
if (!empty($entity->is_new)) {
$entity->created = time();
$entity->updated = time();
}
return parent::save($entity, $transaction);
}
/**
* Get acceptances for the given version of this document.
*
* @param EntityLegalDocumentVersion $legal_document_entity_version
* The legal document entity to get the acceptances of.
* @param bool|object $account
* The account the acceptance belongs to, or all accounts if not set.
*
* @return array
* The acceptances associated with this legal document entity and account.
*/
public function getAcceptances(EntityLegalDocumentVersion $legal_document_entity_version, $account = FALSE) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME)
->propertyCondition('document_version_name', $legal_document_entity_version->name);
if ($account) {
$query->propertyCondition('uid', $account->uid);
}
$results = $query->execute();
if (!empty($results)) {
$results = entity_load(ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME, array_keys($results[ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME]));
}
return $results;
}
}
/**
* Entity controller for legal document acceptance entity.
*/
class EntityLegalDocumentAcceptanceController extends EntityAPIController {
/**
* {@inheritdoc}
*
* Adds legal document identifier and revision values to the acceptance
* entity.
*/
public function save($entity, DatabaseTransaction $transaction = NULL) {
// Dump all available data from the current users browsing session.
$entity->data = $_SERVER;
return parent::save($entity, $transaction);
}
/**
* {@inheritdoc}
*/
public function create(array $values = array()) {
if (empty($values['document_version_name'])) {
throw new EntityFieldQueryException(t('Acceptance does not have a valid associated document version name'));
}
if (!isset($values['uid'])) {
global $user;
$values['uid'] = $user->uid;
}
$values['acceptance_date'] = time();
return parent::create($values);
}
}