-
Notifications
You must be signed in to change notification settings - Fork 2
/
entityform.entity.inc
567 lines (512 loc) · 15.3 KB
/
entityform.entity.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<?php
/**
* @file
* Entityform editing UI.
*
* We make very little use of the EntityAPI interface for this - preferring
* instead to use views. That offers more flexibility to change a UI that will,
* more often than not, be end-user facing.
*/
/**
* The class used for entityform entities.
*/
class Entityform extends Entity {
/**
* The user id who submitted this form.
*
* @var int
*/
public $uid;
/**
* {@inheritdoc}
*/
public function __construct($values = array()) {
parent::__construct($values);
}
/**
* {@inheritdoc}
*/
protected function defaultLabel() {
$entityform_type = entityform_type_load($this->type);
$label = $entityform_type->label;
$submit_user = NULL;
if (!empty($this->uid)) {
$submit_user = user_load($this->uid);
}
$label .= ' - ' . user_format_name($submit_user);
$label .= ' - ' . format_date($this->created, 'short');
return $label;
}
/**
* {@inheritdoc}
*/
protected function defaultUri() {
return array('path' => 'entityform/' . $this->entityform_id);
}
/**
* Get human readable entityform_type label.
*
* @return string
*/
public function getTypeName() {
$entity_info = entity_get_info('entityform');
if (!empty($entity_info['bundles'][$this->type])) {
$entityform_type = entityform_type_load($this->type);
return $entityform_type->getTranslation('label');
}
else {
return t('Broken or missing form type');
}
}
/**
* Returns the user who submitted this form.
*/
public function user() {
return user_load($this->uid);
}
/**
* Sets a new user who submitted this form.
*
* @param User|AnonymousUser|int $account
* The user account object or the user account id (uid).
*/
public function setUser($account) {
$this->uid = is_object($account) ? $account->uid : $account;
}
/**
* {@inheritdoc}
*/
public function uri() {
return array(
'path' => 'entityform/' . $this->entityform_id,
'options' => array(),
);
}
/**
* Implements EntityInterface::id().
*/
public function id() {
return $this->entityform_id;
}
/**
* Implements EntityInterface::entityType().
*/
public function entityType() {
return 'entityform';
}
/**
* Implements EntityInterface::label().
*/
public function label() {
return t('Form Submission: @label', array(
'@label' => $this->getTypeName(),
));
}
/**
* Implements EntityInterface::bundle().
*/
public function bundle() {
return $this->type;
}
}
/**
* The class used for entityform type entities.
*/
class EntityformType extends Entity {
/**
*
*/
protected function defaultUri() {
return array(
'path' => 'eform/submit/' . str_replace('_', '-', $this->type),
);
}
public $type;
public $label;
public $data;
/**
*
*/
public function __construct($values = array()) {
parent::__construct($values);
}
/**
* Get A path property for this EntityformType with tokens replaced.
*/
public function get_path_property($property, $entityform = NULL) {
$path = $this->getTranslation($property);
if (empty($path)) {
return '';
}
if ($path == '<front>') {
return $path;
}
$path = _entityform_format_text($path, array(
'entityform_type' => $this,
'entityform' => $entityform,
));
$options = backdrop_parse_url(decode_entities($path));
return array($options['path'], array('query' => $options['query']));
}
/**
*
*/
public function get_prop($key, $entityform = NULL) {
if (isset($this->data[$key])) {
if (is_array($this->data[$key]) && !empty($this->data[$key]['value'])) {
$value = array(
'value' => $this->getTranslation($key),
);
if (!empty($this->data[$key]['format'])) {
$value['format'] = $this->data[$key]['format'];
}
}
else {
$value = $this->getTranslation($key);
}
}
else {
$value = '';
}
$text_value = $value;
if (is_array($text_value)) {
// We are dealing for a filtered text value.
$text_value = $text_value['value'];
}
if ($text_value == '<none>') {
return '';
}
$token_types['entityform_type'] = $this;
if (!empty($entityform)) {
$token_types['entityform'] = $entityform;
}
if (empty($text_value)) {
$default_entityform_type = entity_get_controller('entityform_type')->create(array());
if (isset($default_entityform_type->data[$key])) {
$value = $default_entityform_type->data[$key];
}
}
return _entityform_format_text($value, $token_types);
}
/**
* {@inheritdoc}
*/
public function getTranslation($property, $langcode = NULL) {
$all_info = entity_plus_get_all_property_info($this->entityType());
// Assign by reference to avoid triggering notices if metadata is missing.
$property_info = &$all_info[$property];
// @todo make this code readable:
$property_value = isset($this->$property) ? $this->$property : (isset($this->data[$property]) ? $this->data[$property] : NULL);
if (is_array($property_value) && isset($property_value['value'])) {
$property_value = $property_value['value'];
}
if (!empty($property_info['translatable'])) {
if (!empty($property_info['field'])) {
return field_get_items($this->entityType, $this, $property, $langcode);
}
elseif (module_exists('i18n_string') && !empty($property_info['i18n string'])) {
$name = 'entityform:entityform_type:' . $this->id() . ':' . $property;
$options = array(
'langcode' => $langcode,
'sanitize' => FALSE,
);
return html_entity_decode(i18n_string($name, $property_value, $options), ENT_QUOTES);
}
}
return $property_value;
}
/**
* Clears values default text values so global text values can be used.
*/
public function clear_text_props() {
$clear_keys = array_keys(entity_get_controller('entityform_type')->get_default_text_values());
foreach ($clear_keys as $key) {
$this->data[$key] = '';
}
}
/**
* Return a list of property labels used for translation forms.
*/
public function getLabelsForTranslation() {
return entity_get_controller('entityform_type')->get_text_labels();
}
/**
*
*/
public function uri() {
return array(
'path' => 'eform/submit/' . str_replace('_', '-', $this->type),
'options' => array(),
);
}
/**
* Implements EntityInterface::id().
*/
public function id() {
return $this->type;
}
/**
* Implements EntityInterface::entityType().
*/
public function entityType() {
return 'entityform_type';
}
/**
* Implements EntityInterface::label().
*/
public function label() {
return $this->getTranslation('label');
}
}
/**
* The Controller for Entityform entities.
*/
class EntityformController extends EntityPlusController {
/**
*
*/
public function __construct($entityType) {
parent::__construct($entityType);
}
/**
* Create a entityform - we first set up the values that are specific
* to our entityform schema but then also go through the EntityAPIController
* function.
*
* @param array $values
*
* @return Entityform|null
* A entityform object with all default fields initialized.
*/
public function create(array $values = array()) {
// We need a type for the bundle but it may not be valid.
if (isset($values['type'])) {
// Rules supplies a EntityformType object instead of string.
if (is_object($values['type'])) {
if (get_class($values['type']) == 'EntityformType') {
$values['type'] = $values['type']->type;
}
else {
// This should never happen.
throw new EntityMalformedException("Object sent for EntityformType not of class EntityformType.");
}
}
$type = entityform_get_types($values['type']);
if (empty($type)) {
$values['type'] = '';
}
}
// Add values that are specific to our Entityform.
$values += array(
'is_new' => TRUE,
'title' => '',
'created' => '',
'changed' => '',
'data' => '',
);
$entityform = parent::create($values);
return $entityform;
}
/**
* Overriding the buildContent function to add entity specific fields.
*/
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$content = parent::buildContent($entity, $view_mode, $langcode, $content);
$content['info']['user'] = array(
'#markup' => _entityform_get_submit_info($entity),
'#weight' => -100,
'#prefix' => '<div class="submitted">',
'#suffix' => '</div>',
);
$content['info']['#weight'] = -99;
return $content;
}
}
/**
* The Controller for Entityform entities.
*/
class EntityformTypeController extends EntityPlusController {
/**
*
*/
public function __construct($entityType) {
parent::__construct($entityType);
}
/**
* Create a entityform type.
*
* We first set up the values that are specific to our entityform type schema
* but then also go through the EntityAPIController function.
*
* @param array $values
* @param bool $load_defaults
*
* @return EntityformType
* A entityform type object with all default fields initialized.
*/
public function create(array $values = array(), $load_defaults = FALSE) {
// Add values that are specific to our Entityform.
$default_values = config_get('entityform.settings', 'entityform_type_defaults');
if (empty($default_values)) {
$default_values = array();
}
$values += $default_values;
$values += array(
'id' => '',
'is_new' => TRUE,
);
if (!isset($values['data'])) {
$values['data'] = array();
}
if ($load_defaults) {
$values['data'] += array(
'submissions_view' => 'entityforms',
'user_submissions_view' => 'user_entityforms',
'preview_page' => 0,
);
$values['data'] += $this->get_default_text_values();
}
else {
if ($values['is_new']) {
// Don't override values even if is_new. Features will send values in
// for checking status.
$values['data'] += array(
'submissions_view' => 'default',
'user_submissions_view' => 'default',
);
}
}
$entityform_type = parent::create($values);
return $entityform_type;
}
/**
* Returns default text values.
*/
public function get_default_text_values() {
return array(
'label' => '',
'instruction_pre' => '',
'submit_confirm_msg' => t('Your submission has been saved.'),
'submission_page_title' => t('Thank You.'),
'draft_button_text' => t('Save Draft'),
'submit_button_text' => t('Submit'),
'your_submissions' => t('Your Submissions: @label', array('@label' => '[entityform_type:label]')),
'disallow_resubmit_msg' => t('You already submitted this form'),
'delete_confirm_msg' => t('Are you sure you want to delete this Submission for @label', array('@label' => '[entityform_type:label]?')),
'draft_save_text' => '',
'page_title_view' => t('Form Submission: @label', array('@label' => '[entityform_type:label]')),
);
}
/**
* Returns text labels.
*/
public function get_text_labels() {
return array(
'label' => t('Label'),
'draft_redirect_path' => t('Draft Redirect path'),
'draft_button_text' => t('Override draft button text'),
'draft_save_text' => t('Draft save text'),
'submit_button_text' => t('Submit Button Text'),
'submit_confirm_msg' => t('Submission Confirmation Text'),
'your_submissions' => t('Your Submissions Text'),
'disallow_resubmit_msg' => t('Form Disallow Resubmit Text'),
'delete_confirm_msg' => t('Submission Delete Text'),
'submission_page_title' => t('Thank You.'),
'page_title_view' => t('Submission View Title'),
'submission_text' => t('Submission reply'),
'redirect_path' => t('Redirect path'),
'instruction_pre' => t('Intro form instructions'),
);
}
/**
* Overridden to Load file.
*/
public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
$view = parent::view($entities, $view_mode, $langcode, $page);
foreach ($entities as $entity_id => $entity) {
module_load_include('inc', 'entityform', 'entityform.admin');
$view[$this->entityType][$entity->type]['form'] = entityform_form_wrapper(entityform_empty_load($entity->type), 'submit', ($page ? 'page' : 'embedded'));
}
return $view;
}
/**
* Overridden to load paths also.
*/
public function load($ids = array(), $conditions = array()) {
$entityform_types = parent::load($ids, $conditions);
foreach ($entityform_types as $entityform_type) {
if (module_exists('path')) {
$entityform_type->paths = array();
$path_types = _entityform_type_get_path_types($entityform_type->type);
foreach ($path_types as $key => $path_type) {
// Check for existing alias.
$conditions = array('source' => $path_type['default_path']);
$path = path_load($conditions);
if ($path !== FALSE) {
$entityform_type->paths[$key] = $path;
}
}
}
}
return $entityform_types;
}
/**
* Overridden to clear cache.
*/
public function save($entity, DatabaseTransaction $transaction = NULL) {
$return = parent::save($entity, $transaction);
// Reset the entityform type cache. We need to do this first so
// menu changes pick up our new type.
entityform_type_cache_reset();
// Clear field info caches such that any changes to extra fields get
// reflected.
field_info_cache_clear();
return $return;
}
/**
* Overridden to delete aliases and clear cache.
*/
public function delete($ids, DatabaseTransaction $transaction = NULL) {
$entities = $ids ? $this->load($ids) : FALSE;
if ($entities) {
if (module_exists('path')) {
foreach ($entities as $id => $entity) {
try {
$path_types = _entityform_type_get_path_types($entity->type);
foreach ($path_types as $path_type) {
path_delete(array('source' => $path_type['default_path']));
}
}
catch (Exception $e) {
}
}
}
// Delete all menu module links that point to this entityform type.
$submit_paths = array();
foreach ($entities as $id => $entity) {
$submit_paths[] = _entityform_type_get_submit_url($entity->type);
}
$result = db_query("SELECT mlid FROM {menu_links} WHERE link_path IN (:path) AND module = 'entityform'", array(':path' => $submit_paths), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $m) {
menu_link_delete($m['mlid']);
}
parent::delete($ids, $transaction);
// Clear field info caches such that any changes to extra fields get
// reflected.
field_info_cache_clear();
// Reset the entityform type cache.
entityform_type_cache_reset();
}
}
/**
* Overridden to exclude pid in alias Export.
*/
public function export($entity, $prefix = '') {
if (module_exists('path')) {
foreach ($entity->paths as &$path) {
unset($path['pid']);
}
}
return parent::export($entity, $prefix);
}
}