Projet

Général

Profil

Paste
Télécharger (32,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entity / modules / callbacks.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * Provides various callbacks for the whole core module integration.
6
 */
7

    
8
/**
9
 * Callback for getting properties of an entity.
10
 */
11
function entity_metadata_entity_get_properties($entity, array $options, $name, $entity_type) {
12
  if ($name == 'url') {
13
    $return = entity_uri($entity_type, $entity);
14
    return url($return['path'], $return['options'] + $options);
15
  }
16
}
17

    
18
/**
19
 * Callback for getting book node properties.
20
 * @see entity_metadata_book_entity_info_alter()
21
 */
22
function entity_metadata_book_get_properties($node, array $options, $name, $entity_type) {
23
  if (!isset($node->book['bid'])) {
24
    throw new EntityMetadataWrapperException('This node is no book page.');
25
  }
26
  switch ($name) {
27
    case 'book':
28
      return $node->book['bid'];
29

    
30
    case 'book_ancestors':
31
      $ancestors = array();
32
      while (!empty($node->book['plid'])) {
33
        $link = book_link_load($node->book['plid']);
34
        array_unshift($ancestors, $link['nid']);
35
        $node = node_load($link['nid']);
36
      }
37
      return $ancestors;
38
  }
39
}
40

    
41
/**
42
 * Callback for getting comment properties.
43
 * @see entity_metadata_comment_entity_info_alter()
44
 */
45
function entity_metadata_comment_get_properties($comment, array $options, $name) {
46
  switch ($name) {
47
    case 'name':
48
      return $comment->name;
49

    
50
    case 'mail':
51
      if ($comment->uid != 0) {
52
        $account = user_load($comment->uid);
53
        return $account->mail;
54
      }
55
      return $comment->mail;
56

    
57
    case 'edit_url':
58
      return url('comment/edit/' . $comment->cid, $options);
59

    
60
    case 'parent':
61
      if (!empty($comment->pid)) {
62
        return $comment->pid;
63
      }
64
      // There is no parent comment.
65
      return NULL;
66
  }
67
}
68

    
69
/**
70
 * Callback for setting comment properties.
71
 * @see entity_metadata_comment_entity_info_alter()
72
 */
73
function entity_metadata_comment_setter($comment, $name, $value) {
74
  switch ($name) {
75
    case 'node':
76
      $comment->nid = $value;
77
      // Also set the bundle name.
78
      $node = node_load($value);
79
      $comment->node_type = 'comment_node_' . $node->type;
80
      break;
81
  }
82
}
83

    
84
/**
85
 * Callback for getting comment related node properties.
86
 * @see entity_metadata_comment_entity_info_alter()
87
 */
88
function entity_metadata_comment_get_node_properties($node, array $options, $name, $entity_type) {
89
  switch ($name) {
90
    case 'comment_count':
91
      return isset($node->comment_count) ? $node->comment_count : 0;
92

    
93
    case 'comment_count_new':
94
      return comment_num_new($node->nid);
95
  }
96
}
97

    
98
/**
99
 * Getter callback for getting global languages.
100
 */
101
function entity_metadata_locale_get_languages($data, array $options, $name) {
102
  return isset($GLOBALS[$name]) ? $GLOBALS[$name]->language : NULL;
103
}
104

    
105
/**
106
 * Getter callback for getting the preferred user language.
107
 */
108
function entity_metadata_locale_get_user_language($account, array $options, $name) {
109
  return user_preferred_language($account)->language;
110
}
111

    
112
/**
113
 * Return the options lists for the node and comment status property.
114
 */
115
function entity_metadata_status_options_list() {
116
  return array(
117
    NODE_PUBLISHED => t('Published'),
118
    NODE_NOT_PUBLISHED => t('Unpublished'),
119
  );
120
}
121

    
122
/**
123
 * Callback for getting node properties.
124
 *
125
 * @see entity_metadata_node_entity_info_alter()
126
 */
127
function entity_metadata_node_get_properties($node, array $options, $name, $entity_type) {
128
  switch ($name) {
129
    case 'is_new':
130
      return empty($node->nid) || !empty($node->is_new);
131

    
132
    case 'source':
133
      if (!empty($node->tnid) && $source = node_load($node->tnid)) {
134
        return $source;
135
      }
136
      return NULL;
137

    
138
    case 'edit_url':
139
      return url('node/' . $node->nid . '/edit', $options);
140

    
141
    case 'author':
142
      return !empty($node->uid) ? $node->uid : drupal_anonymous_user();
143
  }
144
}
145

    
146
/**
147
 * Callback for determing access for node revision related properties.
148
 */
149
function entity_metadata_node_revision_access($op, $name, $entity = NULL, $account = NULL) {
150
  return $op == 'view' ? user_access('view revisions', $account) : user_access('administer nodes', $account);
151
}
152

    
153
/**
154
 * Callback for getting poll properties.
155
 * @see entity_metadata_poll_entity_info_alter()
156
 */
157
function entity_metadata_poll_node_get_properties($node, array $options, $name) {
158
  $total_votes = $highest_votes = 0;
159
  foreach ($node->choice as $choice) {
160
    if ($choice['chvotes'] > $highest_votes) {
161
      $winner = $choice;
162
      $highest_votes = $choice['chvotes'];
163
    }
164
    $total_votes = $total_votes + $choice['chvotes'];
165
  }
166

    
167
  if ($name == 'poll_duration') {
168
    return $node->runtime;
169
  }
170
  elseif ($name == 'poll_votes') {
171
    return $total_votes;
172
  }
173
  elseif (!isset($winner)) {
174
    // There is no poll winner yet.
175
    return NULL;
176
  }
177
  switch ($name) {
178
    case 'poll_winner_votes':
179
        return $winner['chvotes'];
180

    
181
    case 'poll_winner':
182
        return $winner['chtext'];
183

    
184
    case 'poll_winner_percent':
185
        return ($winner['chvotes'] / $total_votes) * 100;
186
  }
187
}
188

    
189
/**
190
 * Callback for getting statistics properties.
191
 * @see entity_metadata_statistics_entity_info_alter()
192
 */
193
function entity_metadata_statistics_node_get_properties($node, array $options, $name) {
194
  $statistics = (array) statistics_get($node->nid);
195
  $statistics += array('totalcount' => 0, 'daycount' => 0, 'timestamp' => NULL);
196

    
197
  switch ($name) {
198
    case 'views':
199
      return $statistics['totalcount'];
200

    
201
    case 'day_views':
202
      return $statistics['daycount'];
203

    
204
    case 'last_view':
205
      return $statistics['timestamp'];
206
  }
207
}
208

    
209
/**
210
 * Access callback for restricted node statistics properties.
211
 */
212
function entity_metadata_statistics_properties_access($op, $property, $entity = NULL, $account = NULL) {
213
  if ($property == 'views' && user_access('view post access counter', $account)) {
214
    return TRUE;
215
  }
216
  return user_access('access statistics', $account);
217
}
218

    
219
/**
220
 * Callback for getting site-wide properties.
221
 * @see entity_metadata_system_entity_info_alter()
222
 */
223
function entity_metadata_system_get_properties($data = FALSE, array $options, $name) {
224
  switch ($name) {
225
    case 'name':
226
      return variable_get('site_name', 'Drupal');
227

    
228
    case 'url':
229
      return url('<front>', $options);
230

    
231
    case 'login_url':
232
      return url('user', $options);
233

    
234
    case 'current_user':
235
      return $GLOBALS['user']->uid ? $GLOBALS['user']->uid : drupal_anonymous_user();
236

    
237
    case 'current_date':
238
      return REQUEST_TIME;
239

    
240
    case 'current_page':
241
      // Subsequent getters of the struct retrieve the actual values.
242
      return array();
243

    
244
    default:
245
      return variable_get('site_' . $name, '');
246
  }
247
}
248

    
249
/**
250
 * Callback for getting properties for the current page request.
251
 * @see entity_metadata_system_entity_info_alter()
252
 */
253
function entity_metadata_system_get_page_properties($data = array(), array $options, $name) {
254
  switch ($name) {
255
    case 'url':
256
      return $GLOBALS['base_root'] . request_uri();
257
  }
258
}
259

    
260
/**
261
 * Callback for getting file properties.
262
 * @see entity_metadata_system_entity_info_alter()
263
 */
264
function entity_metadata_system_get_file_properties($file, array $options, $name) {
265
  switch ($name) {
266
    case 'name':
267
      return $file->filename;
268

    
269
    case 'mime':
270
      return $file->filemime;
271

    
272
    case 'size':
273
      return $file->filesize;
274

    
275
    case 'url':
276
      return url(file_create_url($file->uri), $options);
277

    
278
    case 'owner':
279
      return $file->uid;
280
  }
281
}
282

    
283
/**
284
 * Callback for getting term properties.
285
 *
286
 * @see entity_metadata_taxonomy_entity_info_alter()
287
 */
288
function entity_metadata_taxonomy_term_get_properties($term, array $options, $name) {
289
  switch ($name) {
290
    case 'node_count':
291
      return count(taxonomy_select_nodes($term->tid));
292

    
293
    case 'description':
294
      return check_markup($term->description, isset($term->format) ? $term->format : NULL, '', TRUE);
295

    
296
    case 'parent':
297
      if (isset($term->parent[0]) && !is_array(isset($term->parent[0]))) {
298
        return $term->parent;
299
      }
300
      return array_keys(taxonomy_get_parents($term->tid));
301

    
302
    case 'parents_all':
303
      // We have to return an array of ids.
304
      $tids = array();
305
      foreach (taxonomy_get_parents_all($term->tid) as $parent) {
306
        $tids[] = $parent->tid;
307
      }
308
      return $tids;
309
  }
310
}
311

    
312
/**
313
 * Callback for setting term properties.
314
 *
315
 * @see entity_metadata_taxonomy_entity_info_alter()
316
 */
317
function entity_metadata_taxonomy_term_setter($term, $name, $value) {
318
  switch ($name) {
319
    case 'vocabulary':
320
      // Make sure to update the taxonomy bundle key, so load the vocabulary.
321
      // Support both, loading by name or ID.
322
      $vocabulary = is_numeric($value) ? taxonomy_vocabulary_load($value) : taxonomy_vocabulary_machine_name_load($value);
323
      $term->vocabulary_machine_name = $vocabulary->machine_name;
324
      return $term->vid = $vocabulary->vid;
325
    case 'parent':
326
      return $term->parent = $value;
327
  }
328
}
329

    
330
/**
331
 * Callback for getting vocabulary properties.
332
 * @see entity_metadata_taxonomy_entity_info_alter()
333
 */
334
function entity_metadata_taxonomy_vocabulary_get_properties($vocabulary, array $options, $name) {
335
  switch ($name) {
336
    case 'term_count':
337
      $sql = "SELECT COUNT (1) FROM {taxonomy_term_data} td WHERE td.vid = :vid";
338
      return db_query($sql, array(':vid' => $vocabulary->vid))->fetchField();
339
  }
340
}
341

    
342
/**
343
 * Callback for getting user properties.
344
 * @see entity_metadata_user_entity_info_alter()
345
 */
346
function entity_metadata_user_get_properties($account, array $options, $name, $entity_type) {
347
  switch ($name) {
348
    case 'last_access':
349
      // In case there was no access the value is 0, but we have to return NULL.
350
      return empty($account->access) ? NULL : $account->access;
351

    
352
    case 'last_login':
353
      return empty($account->login) ? NULL : $account->login;
354

    
355
    case 'name':
356
      return empty($account->uid) ? variable_get('anonymous', t('Anonymous')) : $account->name;
357

    
358
    case 'url':
359
      if (empty($account->uid)) {
360
        return NULL;
361
      }
362
      $return = entity_uri('user', $account);
363
      return $return ? url($return['path'], $return['options'] + $options) : '';
364

    
365
    case 'edit_url':
366
      return empty($account->uid) ? NULL : url("user/$account->uid/edit", $options);
367

    
368
    case 'roles':
369
      return isset($account->roles) ? array_keys($account->roles) : array();
370

    
371
    case 'theme':
372
      return empty($account->theme) ? variable_get('theme_default', 'bartik') : $account->theme;
373
  }
374
}
375

    
376
/**
377
 * Callback for setting user properties.
378
 * @see entity_metadata_user_entity_info_alter()
379
 */
380
function entity_metadata_user_set_properties($account, $name, $value) {
381
  switch ($name) {
382
    case 'roles':
383
      $account->roles = array_intersect_key(user_roles(), array_flip($value));
384
      break;
385
  }
386
}
387

    
388
/**
389
 * Options list callback returning all user roles.
390
 */
391
function entity_metadata_user_roles($property_name = 'roles', $info = array(), $op = 'edit') {
392
  $roles = user_roles();
393
  if ($op == 'edit') {
394
    unset($roles[DRUPAL_AUTHENTICATED_RID], $roles[DRUPAL_ANONYMOUS_RID]);
395
  }
396
  return $roles;
397
}
398

    
399
/**
400
 * Return the options lists for user status property.
401
 */
402
function entity_metadata_user_status_options_list() {
403
  return array(
404
    0 => t('Blocked'),
405
    1 => t('Active'),
406
  );
407
}
408

    
409
/**
410
 * Callback defining an options list for language properties.
411
 */
412
function entity_metadata_language_list() {
413
  $list = array();
414
  $list[LANGUAGE_NONE] = t('Language neutral');
415
  foreach (language_list() as $language) {
416
    $list[$language->language] = $language->name;
417
  }
418
  return $list;
419
}
420

    
421
/**
422
 * Callback for getting field property values.
423
 */
424
function entity_metadata_field_property_get($entity, array $options, $name, $entity_type, $info) {
425
  $field = field_info_field($name);
426
  $columns = array_keys($field['columns']);
427
  $langcode = isset($options['language']) ? $options['language']->language : LANGUAGE_NONE;
428
  $langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode, TRUE);
429
  $values = array();
430
  if (isset($entity->{$name}[$langcode])) {
431
    foreach ($entity->{$name}[$langcode] as $delta => $data) {
432
      $values[$delta] = $data[$columns[0]];
433
      if ($info['type'] == 'boolean' || $info['type'] == 'list<boolean>') {
434
        // Ensure that we have a clean boolean data type.
435
        $values[$delta] = (boolean) $values[$delta];
436
      }
437
    }
438
  }
439
  // For an empty single-valued field, we have to return NULL.
440
  return $field['cardinality'] == 1 ? ($values ? reset($values) : NULL) : $values;
441
}
442

    
443
/**
444
 * Callback for setting field property values.
445
 */
446
function entity_metadata_field_property_set($entity, $name, $value, $langcode, $entity_type, $info) {
447
  $field = field_info_field($name);
448
  $columns = array_keys($field['columns']);
449
  $langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode);
450
  $values = $field['cardinality'] == 1 ? array($value) : (array) $value;
451

    
452
  $items = array();
453
  foreach ($values as $delta => $value) {
454
    if (isset($value)) {
455
      $items[$delta][$columns[0]] = $value;
456
      if ($info['type'] == 'boolean' || $info['type'] == 'list<boolean>') {
457
        // Convert boolean values back to an integer for writing.
458
        $items[$delta][$columns[0]] = (integer) $items[$delta][$columns[0]] = $value;
459
      }
460
    }
461
  }
462
  $entity->{$name}[$langcode] = $items;
463
  // Empty the static field language cache, so the field system picks up any
464
  // possible new languages.
465
  drupal_static_reset('field_language');
466
}
467

    
468
/**
469
 * Callback returning the options list of a field.
470
 */
471
function entity_metadata_field_options_list($name, $info) {
472
  $field_property_info = $info;
473
  if (is_numeric($name) && isset($info['parent'])) {
474
    // The options list is to be returned for a single item of a multiple field.
475
    $field_property_info = $info['parent']->info();
476
    $name = $field_property_info['name'];
477
  }
478
  if (($field = field_info_field($name)) && isset($field_property_info['parent'])) {
479
    // Retrieve the wrapped entity holding the field.
480
    $wrapper = $field_property_info['parent'];
481
    try {
482
      $entity = $wrapper->value();
483
    }
484
    catch (EntityMetadataWrapperException $e) {
485
      // No data available.
486
      $entity = NULL;
487
    }
488

    
489
    // Support translating labels via i18n field.
490
    if (module_exists('i18n_field') && ($translate = i18n_field_type_info($field['type'], 'translate_options'))) {
491
      return $translate($field);
492
    }
493
    else {
494
      $instance = $wrapper->getBundle() ? field_info_instance($wrapper->type(), $name, $wrapper->getBundle()) : NULL;
495
      return (array) module_invoke($field['module'], 'options_list', $field, $instance, $wrapper->type(), $entity);
496
    }
497
  }
498
}
499

    
500
/**
501
 * Callback to verbatim get the data structure of a field. Useful for fields
502
 * that add metadata for their own data structure.
503
 */
504
function entity_metadata_field_verbatim_get($entity, array $options, $name, $entity_type, &$context) {
505
  // Set contextual info useful for getters of any child properties.
506
  $context['instance'] = field_info_instance($context['parent']->type(), $name, $context['parent']->getBundle());
507
  $context['field'] = field_info_field($name);
508
  $langcode = isset($options['language']) ? $options['language']->language : LANGUAGE_NONE;
509
  $langcode = entity_metadata_field_get_language($entity_type, $entity, $context['field'], $langcode, TRUE);
510

    
511
  if ($context['field']['cardinality'] == 1) {
512
    return isset($entity->{$name}[$langcode][0]) ? $entity->{$name}[$langcode][0] : NULL;
513
  }
514
  return isset($entity->{$name}[$langcode]) ? $entity->{$name}[$langcode] : array();
515
}
516

    
517
/**
518
 * Writes the passed field items in the object. Useful as field level setter
519
 * to set the whole data structure at once.
520
 */
521
function entity_metadata_field_verbatim_set($entity, $name, $items, $langcode, $entity_type) {
522
  $field = field_info_field($name);
523
  $langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode);
524
  $value = $field['cardinality'] == 1 ? array($items) : (array) $items;
525
  // Filter out any items set to NULL.
526
  $entity->{$name}[$langcode] = array_filter($value);
527

    
528
  // Empty the static field language cache, so the field system picks up any
529
  // possible new languages.
530
  drupal_static_reset('field_language');
531
}
532

    
533
/**
534
 * Helper for determining the field language to be used.
535
 *
536
 * Note that we cannot use field_language() as we are not about to display
537
 * values, but generally read/write values.
538
 *
539
 * @param $fallback
540
 *   (optional) Whether to fall back to the entity default language, if no
541
 *   value is available for the given language code yet.
542
 *
543
 * @return
544
 *   The language code to use.
545
 */
546
function entity_metadata_field_get_language($entity_type, $entity, $field, $langcode = LANGUAGE_NONE, $fallback = FALSE) {
547
  // Try to figure out the default language used by the entity.
548
  // With Drupal >= 7.15 we can use entity_language().
549
  if (function_exists('entity_language')) {
550
    $default_langcode = entity_language($entity_type, $entity);
551
  }
552
  else {
553
    $default_langcode = !empty($entity->language) ? $entity->language : LANGUAGE_NONE;
554
  }
555

    
556
  // Determine the right language to use.
557
  if ($default_langcode != LANGUAGE_NONE && field_is_translatable($entity_type, $field)) {
558
    $langcode = ($langcode != LANGUAGE_NONE) ? field_valid_language($langcode, $default_langcode) : $default_langcode;
559
    if (!isset($entity->{$field['field_name']}[$langcode]) && $fallback) {
560
      $langcode = $default_langcode;
561
    }
562
    return $langcode;
563
  }
564
  else {
565
    return LANGUAGE_NONE;
566
  }
567
}
568

    
569
/**
570
 * Callback for getting the sanitized text of 'text_formatted' properties.
571
 * This callback is used for both the 'value' and the 'summary'.
572
 */
573
function entity_metadata_field_text_get($item, array $options, $name, $type, $context) {
574
  // $name is either 'value' or 'summary'.
575
  if (!isset($item['safe_' . $name])) {
576
    // Apply input formats.
577
    $langcode = isset($options['language']) ? $options['language']->language : '';
578
    $format = isset($item['format']) ? $item['format'] : filter_default_format();
579
    $item['safe_' . $name] = check_markup($item[$name], $format, $langcode);
580
    // To speed up subsequent calls, update $item with the 'safe_value'.
581
    $context['parent']->set($item);
582
  }
583
  return $item['safe_' . $name];
584
}
585

    
586
/**
587
 * Defines the list of all available text formats.
588
 */
589
function entity_metadata_field_text_formats() {
590
  foreach (filter_formats() as $key => $format) {
591
    $formats[$key] = $format->name;
592
  }
593
  return $formats;
594
}
595

    
596
/**
597
 * Callback for getting the file entity of file fields.
598
 */
599
function entity_metadata_field_file_get($item) {
600
  return $item['fid'];
601
}
602

    
603
/**
604
 * Callback for setting the file entity of file fields.
605
 */
606
function entity_metadata_field_file_set(&$item, $property_name, $value) {
607
  $item['fid'] = $value;
608
}
609

    
610
/**
611
 * Callback for auto-creating file field $items.
612
 */
613
function entity_metadata_field_file_create_item($property_name, $context) {
614
  // 'fid' is required, so 'file' has to be set as initial property.
615
  return array('display' => isset($context['field']['settings']['display_default']) ? $context['field']['settings']['display_default'] : 0);
616
}
617

    
618
/**
619
 * Callback for validating file field $items.
620
 */
621
function entity_metadata_field_file_validate_item($items, $context) {
622
  // Allow NULL values.
623
  if (!isset($items)) {
624
    return TRUE;
625
  }
626

    
627
  // Stream-line $items for multiple vs non-multiple fields.
628
  $items = !entity_property_list_extract_type($context['type']) ? array($items) : (array) $items;
629

    
630
  foreach ($items as $item) {
631
    // File-field items require a valid file.
632
    if (!isset($item['fid']) || !file_load($item['fid'])) {
633
      return FALSE;
634
    }
635
    if (isset($context['property info']['display']) && !isset($item['display'])) {
636
      return FALSE;
637
    }
638
  }
639
  return TRUE;
640
}
641

    
642
/**
643
 * Access callback for the node entity.
644
 *
645
 * This function does not implement hook_node_access(), thus it may not be
646
 * called entity_metadata_node_access().
647
 *
648
 * @see entity_access()
649
 *
650
 * @param $op
651
 *   The operation being performed. One of 'view', 'update', 'create' or
652
 *   'delete'.
653
 * @param $node
654
 *   A node to check access for. Must be a node object. Must have nid,
655
 *   except in the case of 'create' operations.
656
 * @param $account
657
 *   The user to check for. Leave it to NULL to check for the global user.
658
 *
659
 * @throws EntityMalformedException
660
 *
661
 * @return boolean
662
 *   TRUE if access is allowed, FALSE otherwise.
663
 */
664
function entity_metadata_no_hook_node_access($op, $node = NULL, $account = NULL) {
665
  // First deal with the case where a $node is provided.
666
  if (isset($node)) {
667
    if ($op == 'create') {
668
      if (isset($node->type)) {
669
        return node_access($op, $node->type, $account);
670
      }
671
      else {
672
        throw new EntityMalformedException('Permission to create a node was requested but no node type was given.');
673
      }
674
    }
675
    // If a non-default revision is given, incorporate revision access.
676
    $default_revision = node_load($node->nid);
677
    if ($node->vid !== $default_revision->vid) {
678
      return _node_revision_access($node, $op, $account);
679
    }
680
    else {
681
      return node_access($op, $node, $account);
682
    }
683
  }
684
  // No node is provided. Check for access to all nodes.
685
  if (user_access('bypass node access', $account)) {
686
    return TRUE;
687
  }
688
  if (!user_access('access content', $account)) {
689
    return FALSE;
690
  }
691
  if ($op == 'view' && node_access_view_all_nodes($account)) {
692
    return TRUE;
693
  }
694
  return FALSE;
695
}
696

    
697
/**
698
 * Access callback for the user entity.
699
 */
700
function entity_metadata_user_access($op, $entity = NULL, $account = NULL, $entity_type) {
701
  $account = isset($account) ? $account : $GLOBALS['user'];
702
  // Grant access to the users own user account and to the anonymous one.
703
  if (isset($entity) && $op != 'delete' && (($entity->uid == $account->uid && $entity->uid) || (!$entity->uid && $op == 'view'))) {
704
    return TRUE;
705
  }
706
  if (user_access('administer users', $account) || user_access('access user profiles', $account) && $op == 'view' && $entity->status) {
707
    return TRUE;
708
  }
709
  return FALSE;
710
}
711

    
712
/**
713
 * Access callback for restricted user properties.
714
 */
715
function entity_metadata_user_properties_access($op, $property, $entity = NULL, $account = NULL) {
716
  if (user_access('administer users', $account)) {
717
    return TRUE;
718
  }
719
  $account = isset($account) ? $account : $GLOBALS['user'];
720
  // Flag to indicate if this user entity is the own user account.
721
  $is_own_account = isset($entity) && $account->uid == $entity->uid;
722
  switch ($property) {
723
    case 'name':
724
      // Allow view access to anyone with access to the entity.
725
      if ($op == 'view') {
726
        return TRUE;
727
      }
728
      // Allow edit access for own user name if the permission is satisfied.
729
      return $is_own_account && user_access('change own username', $account);
730
    case 'mail':
731
      // Allow access to own mail address.
732
      return $is_own_account;
733
    case 'roles':
734
      // Allow view access for own roles.
735
      return ($op == 'view' && $is_own_account);
736
  }
737
  return FALSE;
738
}
739

    
740
/**
741
 * Access callback for the comment entity.
742
 */
743
function entity_metadata_comment_access($op, $entity = NULL, $account = NULL) {
744
  // When determining access to a comment, 'comment_access' does not take any
745
  // access restrictions to the comment's associated node into account. If a
746
  // comment has an associated node, the user must be able to view it in order
747
  // to access the comment.
748
  if (isset($entity->nid)) {
749
    if (!entity_access('view', 'node', node_load($entity->nid), $account)) {
750
      return FALSE;
751
    }
752
  }
753

    
754
  // Comment administrators are allowed to perform all operations on all
755
  // comments.
756
  if (user_access('administer comments', $account)) {
757
    return TRUE;
758
  }
759

    
760
  // Unpublished comments can never be accessed by non-admins.
761
  if (isset($entity->status) && $entity->status == COMMENT_NOT_PUBLISHED) {
762
    return FALSE;
763
  }
764

    
765
  if (isset($entity) && $op == 'update') {
766
    // Because 'comment_access' only checks the current user, we need to do our
767
    // own access checking if an account was specified.
768
    if (!isset($account)) {
769
      return comment_access('edit', $entity);
770
    }
771
    else {
772
      return $account->uid && $account->uid == $entity->uid && user_access('edit own comments', $account);
773
    }
774
  }
775
  if (user_access('access comments', $account) && $op == 'view') {
776
    return TRUE;
777
  }
778
  return FALSE;
779
}
780

    
781
/**
782
 * Access callback for restricted comment properties.
783
 */
784
function entity_metadata_comment_properties_access($op, $property, $entity = NULL, $account = NULL) {
785
  return user_access('administer comments', $account);
786
}
787

    
788
/**
789
 * Access callback for the taxonomy entities.
790
 */
791
function entity_metadata_taxonomy_access($op, $entity = NULL, $account = NULL, $entity_type) {
792
  if ($entity_type == 'taxonomy_vocabulary') {
793
    return user_access('administer taxonomy', $account);
794
  }
795
  if (isset($entity) && $op == 'update' && !isset($account) && taxonomy_term_edit_access($entity)) {
796
    return TRUE;
797
  }
798
  if (user_access('administer taxonomy', $account) || user_access('access content', $account) && $op == 'view') {
799
    return TRUE;
800
  }
801
  return FALSE;
802
}
803

    
804
/**
805
 * Access callback for file entities.
806
 */
807
function entity_metadata_file_access($op, $file = NULL, $account = NULL, $entity_type) {
808
  // We can only check access for the current user, so return FALSE on other accounts.
809
  global $user;
810
  if ($op == 'view' && isset($file) && (!isset($account) || $user->uid == $account->uid)) {
811
    // Invoke hook_file_download() to obtain access information.
812
    foreach (module_implements('file_download') as $module) {
813
      $result = module_invoke($module, 'file_download', $file->uri);
814
      if ($result == -1) {
815
        return FALSE;
816
      }
817
    }
818
    return TRUE;
819
  }
820
  return FALSE;
821
}
822

    
823

    
824
/**
825
 * Callback to determine access for properties which are fields.
826
 */
827
function entity_metadata_field_access_callback($op, $name, $entity = NULL, $account = NULL, $entity_type) {
828
  $field = field_info_field($name);
829
  return field_access($op, $field, $entity_type, $entity, $account);
830
}
831

    
832
/**
833
 * Callback to create entity objects.
834
 */
835
function entity_metadata_create_object($values = array(), $entity_type) {
836
  $info = entity_get_info($entity_type);
837
  // Make sure at least the bundle and label properties are set.
838
  if (isset($info['entity keys']['bundle']) && $key = $info['entity keys']['bundle']) {
839
    $values += array($key => NULL);
840
  }
841
  if (isset($info['entity keys']['label']) && $key = $info['entity keys']['label']) {
842
    $values += array($key => NULL);
843
  }
844
  $entity = (object) $values;
845
  $entity->is_new = TRUE;
846
  return $entity;
847
}
848

    
849
/**
850
 * Callback to create a new comment.
851
 */
852
function entity_metadata_create_comment($values = array()) {
853
  $comment = (object) ($values + array(
854
    'status' => COMMENT_PUBLISHED,
855
    'pid' => 0,
856
    'subject' => '',
857
    'uid' => 0,
858
    'language' => LANGUAGE_NONE,
859
    'node_type' => NULL,
860
    'is_new' => TRUE,
861
  ));
862
  $comment->cid = FALSE;
863
  return $comment;
864
}
865

    
866
/**
867
 * Callback to create a new node.
868
 */
869
function entity_metadata_create_node($values = array()) {
870
  $node = (object) array(
871
    'type' => $values['type'],
872
    'language' => LANGUAGE_NONE,
873
    'is_new' => TRUE,
874
  );
875
  // Set some defaults.
876
  $node_options = variable_get('node_options_' . $node->type, array('status', 'promote'));
877
  foreach (array('status', 'promote', 'sticky') as $key) {
878
    $node->$key = (int) in_array($key, $node_options);
879
  }
880
  if (module_exists('comment') && !isset($node->comment)) {
881
    $node->comment = variable_get("comment_$node->type", COMMENT_NODE_OPEN);
882
  }
883
  // Apply the given values.
884
  foreach ($values as $key => $value) {
885
    $node->$key = $value;
886
  }
887
  return $node;
888
}
889

    
890
/**
891
 * Callback to save a user account.
892
 */
893
function entity_metadata_user_save($account) {
894
  $edit = (array) $account;
895
  // Don't save the hashed password as password.
896
  unset($edit['pass']);
897
  user_save($account, $edit);
898
}
899

    
900
/**
901
 * Callback to delete a file.
902
 * Watch out to not accidentilly implement hook_file_delete().
903
 */
904
function entity_metadata_delete_file($fid) {
905
  file_delete(file_load($fid), TRUE);
906
}
907

    
908
/**
909
 * Callback to view nodes.
910
 */
911
function entity_metadata_view_node($entities, $view_mode = 'full', $langcode = NULL) {
912
  $result = node_view_multiple($entities, $view_mode, 0, $langcode);
913
  // Make sure to key the result with 'node' instead of 'nodes'.
914
  return array('node' => reset($result));
915
}
916

    
917
/**
918
 * Callback to view comments.
919
 */
920
function entity_metadata_view_comment($entities, $view_mode = 'full', $langcode = NULL) {
921
  $build = array();
922
  $nodes = array();
923
  // The comments, indexed by nid and then by cid.
924
  $nid_comments = array();
925
  foreach ($entities as $cid => $comment) {
926
    $nid = $comment->nid;
927
    $nodes[$nid] = $nid;
928
    $nid_comments[$nid][$cid] = $comment;
929
  }
930
  $nodes = node_load_multiple(array_keys($nodes));
931
  foreach ($nid_comments as $nid => $comments) {
932
    $node = isset($nodes[$nid]) ? $nodes[$nid] : NULL;
933
    $build += comment_view_multiple($comments, $node, $view_mode, 0, $langcode);
934
  }
935
  return array('comment' => $build);
936
}
937

    
938
/**
939
 * Callback to view an entity, for which just ENTITYTYPE_view() is available.
940
 */
941
function entity_metadata_view_single($entities, $view_mode = 'full', $langcode = NULL, $entity_type) {
942
  $function = $entity_type . '_view';
943
  $build = array();
944
  foreach ($entities as $key => $entity) {
945
    $build[$entity_type][$key] = $function($entity, $view_mode, $langcode);
946
  }
947
  return $build;
948
}
949

    
950
/**
951
 * Callback to get the form of a node.
952
 */
953
function entity_metadata_form_node($node) {
954
  // Pre-populate the form-state with the right form include.
955
  $form_state['build_info']['args'] = array($node);
956
  form_load_include($form_state, 'inc', 'node', 'node.pages');
957
  return drupal_build_form($node->type . '_node_form', $form_state);
958
}
959

    
960
/**
961
 * Callback to get the form of a comment.
962
 */
963
function entity_metadata_form_comment($comment) {
964
  if (!isset($comment->node_type)) {
965
    $node = node_load($comment->nid);
966
    $comment->node_type = 'comment_node_' . $node->type;
967
  }
968
  return drupal_get_form($comment->node_type . '_form', $comment);
969
}
970

    
971
/**
972
 * Callback to get the form of a user account.
973
 */
974
function entity_metadata_form_user($account) {
975
  // Pre-populate the form-state with the right form include.
976
  $form_state['build_info']['args'] = array($account);
977
  form_load_include($form_state, 'inc', 'user', 'user.pages');
978
  return drupal_build_form('user_profile_form', $form_state);
979
}
980

    
981
/**
982
 * Callback to get the form of a term.
983
 */
984
function entity_metadata_form_taxonomy_term($term) {
985
  // Pre-populate the form-state with the right form include.
986
  $form_state['build_info']['args'] = array($term);
987
  form_load_include($form_state, 'inc', 'taxonomy', 'taxonomy.admin');
988
  return drupal_build_form('taxonomy_form_term', $form_state);
989
}
990

    
991
/**
992
 * Callback to get the form of a vocabulary.
993
 */
994
function entity_metadata_form_taxonomy_vocabulary($vocab) {
995
  // Pre-populate the form-state with the right form include.
996
  $form_state['build_info']['args'] = array($vocab);
997
  form_load_include($form_state, 'inc', 'taxonomy', 'taxonomy.admin');
998
  return drupal_build_form('taxonomy_form_vocabulary', $form_state);
999
}
1000

    
1001
/**
1002
 * Callback to get the form for entities using the entity API admin ui.
1003
 */
1004
function entity_metadata_form_entity_ui($entity, $entity_type) {
1005
  $info = entity_get_info($entity_type);
1006
  $form_state = form_state_defaults();
1007
  // Add in the include file as the form API does else with the include file
1008
  // specified for the active menu item.
1009
  if (!empty($info['admin ui']['file'])) {
1010
    $path = isset($info['admin ui']['file path']) ? $info['admin ui']['file path'] : drupal_get_path('module', $info['module']);
1011
    $form_state['build_info']['files']['entity_ui'] = $path . '/' . $info['admin ui']['file'];
1012
    // Also load the include file.
1013
    if (file_exists($form_state['build_info']['files']['entity_ui'])) {
1014
      require_once DRUPAL_ROOT . '/' . $form_state['build_info']['files']['entity_ui'];
1015
    }
1016
  }
1017
  return entity_ui_get_form($entity_type, $entity, $op = 'edit', $form_state);
1018
}
1019

    
1020
/**
1021
 * Callback for querying entity properties having their values stored in the
1022
 * entities main db table.
1023
 */
1024
function entity_metadata_table_query($entity_type, $property, $value, $limit) {
1025
  $properties = entity_get_all_property_info($entity_type);
1026
  $info = $properties[$property] + array('schema field' => $property);
1027

    
1028
  $query = new EntityFieldQuery();
1029
  $query->entityCondition('entity_type', $entity_type, '=')
1030
        ->propertyCondition($info['schema field'], $value, is_array($value) ? 'IN' : '=')
1031
        ->range(0, $limit);
1032

    
1033
  $result = $query->execute();
1034
  return !empty($result[$entity_type]) ? array_keys($result[$entity_type]) : array();
1035
}
1036

    
1037
/**
1038
 * Callback for querying entities by field values. This function just queries
1039
 * for the value of the first specified column. Also it is only suitable for
1040
 * fields that don't process the data, so it's stored the same way as returned.
1041
 */
1042
function entity_metadata_field_query($entity_type, $property, $value, $limit) {
1043
  $query = new EntityFieldQuery();
1044
  $field = field_info_field($property);
1045
  $columns = array_keys($field['columns']);
1046

    
1047
  $query->entityCondition('entity_type', $entity_type, '=')
1048
        ->fieldCondition($field, $columns[0], $value, is_array($value) ? 'IN' : '=')
1049
        ->range(0, $limit);
1050

    
1051
  $result = $query->execute();
1052
  return !empty($result[$entity_type]) ? array_keys($result[$entity_type]) : array();
1053
}
1054

    
1055
/**
1056
 * Implements entity_uri() callback for file entities.
1057
 */
1058
function entity_metadata_uri_file($file) {
1059
  return array(
1060
    'path' => file_create_url($file->uri),
1061
  );
1062
}