-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzariz.module
1716 lines (1477 loc) · 49 KB
/
zariz.module
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Content Revision Control.
*/
/**
* Indicate a branch can be merged.
*/
define('ZARIZ_BRANCH_IS_MERGABLE', 0);
/**
* Indicate branch has no content in it.
*/
define('ZARIZ_BRANCH_HAS_NO_CONTENT', 1);
/**
* Indicate a branch can not be merged due to conflicts with newer content.
*/
define('ZARIZ_BRANCH_HAS_CONFLICTS', 2);
/**
* Indicate a branch was already merged.
*/
define('ZARIZ_BRANCH_IS_MERGED', 3);
/**
* Indicate a branch can not be merged as it is the "master" branch.
*/
define('ZARIZ_BRANCH_IS_MASTER', 4);
/**
* Implements hook_entity_info().
*/
function zariz_entity_info() {
$items['snapshot'] = array(
'label' => t('Snapshot'),
'controller class' => 'EntityAPIController',
'entity class' => 'Snapshot',
'base table' => 'zariz_snapshot',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'sid',
),
'bundles' => array(
'snapshot' => array(
'label' => t('Snapshot'),
),
),
'view modes' => array(
'full' => array(
'label' => t('Full'),
'custom settings' => FALSE,
),
),
'module' => 'zariz',
'metadata controller class' => 'EntityDefaultMetadataController',
);
return $items;
}
/**
* Implements hook_entity_info_alter().
*
* Add "last id field name" property to determine which entities should be
* tracked by the Snapshot entity.
*/
function zariz_entity_info_alter(&$entity_info) {
$entity_info['node']['zariz'] = array(
'last id field name' => 'field_last_node',
);
}
/**
* Merge the content of a snapshot into another branch.
*
* We iterate over the content in the "merge from" branch, and clone it into the
* the "merge to" branch. Like this we confirm that the new content has higher
* entity ID.
*
* @param $from_branch_id
* The branch node ID.
* @param $to_branch_id
* Optional; The other branch node ID to merge to. If empty, the parent
* branch would be used. Defaults to NULL.
*
* @todo: What to do if there is no content to merge?
*
* @return Snapshot
* The snapshot entity created in the other branch, or FALSE if there was no
* content to merge.
*
* @throw Exception
* Throws exception if entity did not replicate.
*
*/
function zariz_merge_branch($from_branch_id, $to_branch_id = NULL) {
$master_nid = variable_get('zariz_master_branch');
if (!$to_branch_id && $from_branch_id == $master_nid) {
throw new Exception('The master branch has no parent branch to merge into.');
}
$params = array(
'@from-id' => $from_branch_id,
'@to-id' => $to_branch_id,
);
if (zariz_get_merge_conflicts($from_branch_id, $to_branch_id)) {
throw new Exception(format_string('Cannot merge branch @from-id to @to-id as they are conflicting branches.', $params));
}
$wrapper = entity_metadata_wrapper('node', $from_branch_id);
if ($wrapper->field_branch_merged->value()) {
throw new Exception(format_string('Branch @from-id is already marked as merged.', $params));
}
$to_branch_id = $to_branch_id ? $to_branch_id : $wrapper->field_parent_branch->getIdentifier();
// Lock the existing snapshots in both branches.
$from_snapshot = zariz_get_snapshot_from_branch($from_branch_id);
$from_snapshot->lock();
$to_snapshot = zariz_get_snapshot_from_branch($to_branch_id);
$to_snapshot->lock();
foreach (array_keys(zariz_get_tracked_entity_types()) as $entity_type) {
$entity_info = entity_get_info($entity_type);
$id_key = $entity_info['entity keys']['id'];
// Clone content from "merge from" to "merge to" branch.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', array_keys(zariz_get_group_content_bundles($entity_type)))
->fieldCondition('field_branch', 'target_id', $from_branch_id)
->propertyOrderBy($id_key)
->execute();
if (empty($result[$entity_type])) {
// No content to merge.
continue;
}
$ids = array_keys($result[$entity_type]);
$entity_items = entity_load($entity_type, $ids);
foreach ($entity_items as $entity_item) {
// Replicate the entity, only after we changed the branch it belongs to.
$wrapper_item = entity_metadata_wrapper($entity_type, $entity_item);
$wrapper_item->field_branch->set($to_branch_id);
if (!$new_id = replicate_entity($entity_type, $entity_item)) {
$params = array(
'@type' => $entity_type,
'@id' => $wrapper_item->getIdentifier(),
'@branch_id' => $to_branch_id,
);
throw new Exception(format_string('Could not merge @type with ID @id into branch ID @branch_id', $params));
}
}
}
// Mark "from branch" as merged.
$wrapper->field_branch_merged->set(TRUE);
$wrapper->save();
// Lock and return the newely created snapshot.
$snapshot = zariz_get_snapshot_from_branch($to_branch_id);
$snapshot->lock();
return $snapshot;
}
/**
* Get entities that have a merge conflict.
*
* A merge conflict can happen when content with same UUID on the "to branch"
* was already updated, and the entity ID is higher than the one in the "from
* branch".
*
* @param $from_branch_id
* The branch node ID.
* @param $to_branch_id
* Optional; The other branch node ID to merge to. If empty, the parent
* branch would be used. Defaults to NULL.
* @param $entity_types
* (optional) Array of entity types to check for merge conflicts. If empty,
* all entities that are registered to be tracked by Zariz will be checked.
*
* @return
* Array keyed by the enity type and UUID, ans as values the entity ID from
* the "from branch" and the entity ID from the "to branch" as the value.
*/
function zariz_get_merge_conflicts($from_branch_id, $to_branch_id = NULL, $entity_types = array()) {
$from_branch_wrapper = entity_metadata_wrapper('node', $from_branch_id);
$to_branch_id = $to_branch_id ? $to_branch_id : $from_branch_wrapper->field_parent_branch->getIdentifier();
$entity_types = $entity_types ? $entity_types : array_keys(zariz_get_tracked_entity_types());
$found_conflicts = FALSE;
$values = array();
foreach ($entity_types as $entity_type) {
$entity_info = entity_get_info($entity_type);
$id_key = $entity_info['entity keys']['id'];
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', array_keys(zariz_get_group_content_bundles($entity_type)))
->fieldCondition('field_branch', 'target_id', $from_branch_id)
->range(0, 1)
->propertyOrderBy($id_key)
->execute();
if (empty($result[$entity_type])) {
// Nothing to merge.
continue;
}
$first_from_id = key($result[$entity_type]);
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', array_keys(zariz_get_group_content_bundles($entity_type)))
->fieldCondition('field_branch', 'target_id', $to_branch_id)
->range(0, 1)
->propertyOrderBy($id_key, 'DESC')
->execute();
if (empty($result[$entity_type])) {
// No content in the "to branch".
continue;
}
$last_to_id = key($result[$entity_type]);
if ($last_to_id < $first_from_id) {
// The last entity ID of the "to branch" is lower than the first one in
// the "from branch", so no conflict.
continue;
}
// Get all the UUID from the "from branch".
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', array_keys(zariz_get_group_content_bundles($entity_type)))
->fieldCondition('field_branch', 'target_id', $from_branch_id)
->execute();
$ids = array_keys($result[$entity_type]);
foreach (entity_load($entity_type, $ids) as $entity) {
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$uuid = $wrapper->field_uuid->value();
$values[$entity_type][$uuid]['from'] = $wrapper->getIdentifier();
}
// Get the UUID values of the content from the "to branch" with higher entity
// ID then the first content from the "from branch."
$all_uuids = array_keys($values[$entity_type]);
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', array_keys(zariz_get_group_content_bundles($entity_type)))
->fieldCondition('field_branch', 'target_id', $to_branch_id)
->fieldCondition('field_uuid', 'value', $all_uuids, 'IN')
->propertyCondition($id_key, $first_from_id, '>')
->propertyOrderBy($id_key)
->execute();
if (empty($result[$entity_type])) {
// No matching UUIDs found.
continue;
}
$ids = array_keys($result[$entity_type]);
foreach (entity_load($entity_type, $ids) as $entity) {
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$uuid = $wrapper->field_uuid->value();
$id = $wrapper->getIdentifier();
if ($id > $values[$entity_type][$uuid]['from']) {
$values[$entity_type][$uuid]['to'] = $id;
$found_conflicts = TRUE;
}
}
}
return $found_conflicts ? $values : NULL;
}
/**
* Determines if a branch can be merged.
*
* @param $branch_id
* The node ID or object of the branch.
*
* @return
* TRUE if branch can be merged.
*/
function zariz_is_mergable($branch_id) {
return zariz_get_merge_status($branch_id) == ZARIZ_BRANCH_IS_MERGABLE;
}
/**
* Determines if a branch is the "master" branch.
*
* @param $branch_id
* The node ID of the branch.
*
* @return
* TRUE if a branch is the "master" branch.
*/
function zariz_is_master_branch($branch_id) {
$master_nid = variable_get('zariz_master_branch');
return $branch_id == $master_nid;
}
/**
* Return the merge status of a branch.
*
* @param $branch_id
* The node ID or object of the branch.
*
*/
function zariz_get_merge_status($branch_id) {
if (zariz_is_master_branch($branch_id)) {
return ZARIZ_BRANCH_IS_MASTER;
}
$wrapper = entity_metadata_wrapper('node', $branch_id);
if ($wrapper->field_branch_merged->value()) {
return ZARIZ_BRANCH_IS_MERGED;
}
if (zariz_get_merge_conflicts($branch_id)) {
return ZARIZ_BRANCH_HAS_CONFLICTS;
}
// Check there's content in the branch.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'og_membership')
->propertyCondition('gid', $branch_id)
->propertyCondition('entity_type', 'user', '<>')
->count()
->execute();
if (!$result) {
return ZARIZ_BRANCH_HAS_NO_CONTENT;
}
return ZARIZ_BRANCH_IS_MERGABLE;
}
/**
* Get the node IDs of the parents of the branch, including the node itself.
*
* @param $branch_nid
* The branch ID or node.
* @param $include
* Determines if to include the given branch ID. Defaults to TRUE.
* @param $ids
* Internal use; Array of the parent IDs, used in this recursive function.
*
* @return
* Array with the branch node ID, and it's parents. The top most parent is the
* last element in the array.
*/
function zariz_get_branch_parents_ids($branch_nid, $include = TRUE, $ids = array()) {
$wrapper = entity_metadata_wrapper('node', $branch_nid);
$branch_node = $wrapper->value();
if ($include) {
$ids[] = $branch_node->nid;
}
if (!$parent_node = $wrapper->field_parent_branch->value()) {
// We reached the top most parent.
return $ids;
}
// Call function, with the parent node.
$ids = zariz_get_branch_parents_ids($parent_node, TRUE, $ids);
return $ids;
}
/**
* Get the snapshot IDs of the parents of the snapshot, including the snapshot itself.
*
* @param $snapshot_id
* The branch ID or node.
* @param $strict_snapshot
* Determines if the parent snapshot should be the real parent snapshot, or
* the last snapshot the exists in the Branch. If using the last snapshot then
* the returned values will include content that was inserted or deleted after
* the branch was created. If TRUE, then only the content that was created
* exactly when the branch was created will be show. Defaults to FALSE.
*
* @return
* array with the snapshot ID, and it's parents. The top most parent is the
* last element in the array.
*/
function zariz_get_snapshot_parents_ids($snapshot_id, $strict_snapshot = FALSE) {
if ($strict_snapshot) {
return zariz_get_snapshot_parents_ids_strict($snapshot_id);
}
return zariz_get_snapshot_parents_ids_not_strict($snapshot_id);
}
/**
* Helper function; Return the snapshot parents in strict mode.
*
* @param $snapshot_id
* The branch ID or node.
* @param $ids
* Internal use. Array of the parent IDs, used in this recursive function.
*
* @return
* array with the snapshot ID, and it's parents. The top most parent is the
* last element in the array.
*
* @see zariz_get_snapshot_parents_ids().
*/
function zariz_get_snapshot_parents_ids_strict($snapshot_id, $ids = array()) {
$wrapper = entity_metadata_wrapper('snapshot', $snapshot_id);
$ids[] = $snapshot_id;
if (!$parent_snapshot = $wrapper->field_snapshot->value()) {
// We reached the top most parent.
return $ids;
}
// Call function, with the parent snapshot.
$ids = zariz_get_snapshot_parents_ids($parent_snapshot, $ids);
return $ids;
}
/**
* Helper function; Return the snapshot parents in a non-strict mode.
*
* @param $snapshot_id
* The branch ID or node.
* @param $ids
* Internal use. Array of the parent IDs, used in this recursive function.
*
* @return
* array with the snapshot ID, and it's parents. The top most parent is the
* last element in the array.
*
* @see zariz_get_snapshot_parents_ids().
*/
function zariz_get_snapshot_parents_ids_not_strict($snapshot_id, $ids = array()) {
// Get all the parent branches.
$ids = array($snapshot_id);
$snapshot = entity_load_single('snapshot', $snapshot_id);
$branch_ids = zariz_get_branch_parents_ids($snapshot->nid);
foreach ($branch_ids as $branch_id) {
$snapshot = zariz_get_snapshot_from_branch($branch_id);
$ids[] = $snapshot->sid;
}
return $ids;
}
/**
* Return array with the last entity IDs per branch, and the deleted entity IDs.
*
* @param $snapshot_id
* The snapshot ID.
* @param $entity_types
* Array of the entity type of the last ID that should be returned. If empty
* array, all entity types tracked by Zariz will be used.
* @param $deleted_ids_field_name
* The name of the field that holds the the "deleted" entity ID. Defaults to
* "field_deleted_nodes".
* @param $strict_snapshot
* Determines if the parent snapshot should be the real parent snapshot, or
* the last snapshot the exists in the Branch. If using the last snapshot then
* the returned values will include content that was inserted or deleted after
* the branch was created. If TRUE, then only the content that was created
* exactly when the branch was created will be show. Defaults to FALSE.
*
* @return
* Array keyed with:
* - last_ids: array keyed by the branch node ID and the entity type. The
* last entity ID as value.
* - @todo: deleted_ids: array of entity IDs.
*/
function zariz_get_last_entity_ids_by_snapshot($snapshot_id, $entity_types = array(), $strict_snapshot = FALSE) {
$sids = zariz_get_snapshot_parents_ids($snapshot_id, $strict_snapshot);
$snapshots = entity_load('snapshot', $sids);
$entity_types = $entity_types ? $entity_types : array_keys(zariz_get_tracked_entity_types());
$return = array('last_ids' => array(), 'deleted_ids' => array());
foreach ($snapshots as $snapshot) {
$wrapper = entity_metadata_wrapper('snapshot', $snapshot);
$branch_id = $snapshot->nid;
foreach ($entity_types as $entity_type) {
$field_name = zariz_get_last_id_field_name($entity_type);
$return['last_ids'][$branch_id][$entity_type] = $wrapper->{$field_name}->getIdentifier();
}
// @todo: Return deleted items.
}
return $return;
}
/**
* Return array of all group content that has "field_branch" field.
*
* @param $entity_type
* The entity type. Defaults to "node".
*
* @return
* Array keyed by the bundle name, and the bundle label as value. Empty array
* if no bundles exist for the entity type.
*/
function zariz_get_group_content_bundles($entity_type = 'node') {
$field = field_info_field('field_branch');
if (empty($field['bundles'][$entity_type])) {
return array();
}
$entity_info = entity_get_info($entity_type);
if (empty($entity_info['bundles'])) {
return array();
}
$return = array();
foreach ($field['bundles'][$entity_type] as $bundle) {
$return[$bundle] = $entity_info['bundles'][$bundle]['label'];
}
return $return;
}
/**
* Return an array of entity types that are registered to tracked by Zariz.
*
* @return
* Array keyed by the entity type name, and the Zariz properties array as
* value.
*/
function zariz_get_tracked_entity_types() {
$return = array();
foreach (entity_get_info() as $entity_type => $info) {
if (empty($info['zariz'])) {
continue;
}
$return[$entity_type] = $info['zariz'];
}
return $return;
}
/**
* Determines if a bundle is tracked by Zariz.
*
* @param $entity_type
* The entity type name.
* @param $bundle
* The bundle na,e.
*
* @return bool
* TRUE if the bundle is tracked by Zariz.
*/
function zariz_is_bundle_tracked($entity_type, $bundle) {
if (!in_array($entity_type, array_keys(zariz_get_tracked_entity_types()))) {
return;
}
return og_is_group_content_type($entity_type, $bundle);
}
/**
* Get the name of the "last id field name" by entity type.
*
* @param $entity_type
* The entity type.
*
* @return string
* The field name if exists, or NULL.
*/
function zariz_get_last_id_field_name($entity_type) {
$entity_info = entity_get_info($entity_type);
if (empty($entity_info['zariz'])) {
return;
}
return $entity_info['zariz']['last id field name'];
}
/**
* Get the node IDs of "deleted" nodes, that should be excluded.
*
* @param $branch_nid
* The branch node.
* @return
* array with the node IDs.
*/
function zariz_get_deleted_node_ids($branch_nid) {
$branch_ids = zariz_get_branch_parents_ids($branch_nid);
// Get all the deleted node IDs in the branches.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', array_keys(zariz_get_group_content_bundles()))
->fieldCondition('field_deleted', 'value', TRUE)
->fieldCondition(field_branch, 'target_id', $branch_ids, 'IN')
->execute();
if (empty($result['node'])) {
return array();
}
// Load all the deleted nodes, to get their UUID, so we can exclude also their
// non-deleted version.
$uuids = array();
foreach (node_load_multiple(array_keys($result['node'])) as $node) {
$wrapper = entity_metadata_wrapper('node', $node);
$uuid = $wrapper->field_uuid->value();
$uuids[$uuid] = TRUE;
}
// Query for the nodes, based on the UUID in the parent branches.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', array_keys(zariz_get_group_content_bundles()))
->fieldCondition('field_branch', 'target_id', $branch_ids, 'IN')
->fieldCondition('field_uuid', 'value', array_keys($uuids), 'IN')
->execute();
return array_keys($result['node']);
}
/**
* Implements hook_field_access().
*/
function zariz_field_access($op, $field, $entity_type, $entity, $account) {
$field_names = array(
'field_branch_merged',
'field_deleted_snapshots',
'field_is_updated',
'field_uuid',
);
if ($op == 'edit' && in_array($field['field_name'], $field_names)) {
return FALSE;
}
}
/**
* Utility function; Create a new branch form an existing one.
*
* @param $branch_name
* The branch name.
* @param $parent_branch
* Optional; The node object of the parent branch.
* @param $account
* Optional; The user object to use as the node author.
*
* @return
* The branch node object.
*/
function zariz_create_branch($branch_name, $parent_branch = NULL, $account = NULL) {
global $user;
$values = array(
'type' => 'branch',
'title' => $branch_name,
'uid' => !empty($account) ? $account->uid : $user->uid,
);
$node = entity_create('node', $values);
$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->{OG_GROUP_FIELD}->set(TRUE);
if ($parent_branch) {
$wrapper->field_parent_branch->set($parent_branch);
}
$wrapper->save();
return $node;
}
/**
* Implements hook_entity_presave().
*
* Populate a unique ID value.
*/
function zariz_entity_presave($entity, $entity_type) {
if (!empty($entity->nid)) {
return;
}
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$bundle = $wrapper->getBundle();
if ($entity_type == 'node' && $bundle == 'branch') {
_zariz_validate_branch_presave($entity);
// Explicitly set the "field_branch_merged" to FALSE is it is NULL, to allow
// easier use of EntityFieldQuery that doesn't support querying over NULL
// values.
if (!$wrapper->field_branch_merged->value()) {
$wrapper->field_branch_merged->set(FALSE);
}
}
if (!zariz_is_bundle_tracked($entity_type, $bundle)) {
return;
}
_zariz_validate_group_content_presave($entity_type, $entity);
if (!$uuid = $wrapper->field_uuid->value()) {
// UUID doesn't exist.
$uuid = md5(rand());
$wrapper->field_uuid->set($uuid);
}
// Check if the UUID already exists.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', array_keys(zariz_get_group_content_bundles($entity_type)))
->fieldCondition('field_uuid', 'value', $uuid)
->range(0, 1)
->execute();
if (empty($result[$entity_type])) {
return;
}
// Set the "Is updated" field to TRUE, and keep the same "created" date of the
// content. Like this, even if the entity IDs are changing, we can still query
// by the entity create date.
$wrapper->field_is_updated->set(TRUE);
$id = key($result[$entity_type]);
$previous_entity = entity_load_single($entity_type, $id);
$entity->created = $previous_entity->created;
$entity->changed = $previous_entity->changed;
}
/**
* Helper function; Validate a branch can be created.
*
* @param $node
* The node object.
*
* @throws Exception
*
* @see zariz_node_presave()
*/
function _zariz_validate_branch_presave($node) {
if ($master_nid = variable_get('zariz_master_branch')) {
// There can be only a single "master" branch.
$wrapper = entity_metadata_wrapper('node', $node);
if (!$wrapper->field_parent_branch->value()) {
$master_node = node_load($master_nid);
$params = array(
'@title' => $node->title,
'@master' => $master_node->title,
);
throw new Exception(format_string('Branch @title cannot be a "master" branch, as @master already exists.', $params));
}
}
// Check branch doesn't exist already.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'branch')
->propertyCondition('title', $node->title)
->count()
->execute();
if ($result) {
throw new Exception(format_string('Branch @name already exists.', array('@name' => $node->title)));
}
}
/**
* Helper function; Validate a group content can be created or updated.
*
* Group content cannot be changed if it belongs to a locked snapshot.
*
* @param $entity_type
* The entity type.
* @param $entity
* The entity object.
*
* @throws Exception
*
* @see zariz_node_presave()
*/
function _zariz_validate_group_content_presave($entity_type, $entity) {
list($id) = entity_extract_ids($entity_type, $entity);
if (empty($id)) {
return;
}
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$branch_id = $wrapper->field_branch->getIdentifier();
$snapshot = zariz_get_snapshot_from_branch($branch_id);
if ($snapshot->isLocked()) {
$params = array(
'@type' => $entity_type,
'@id' => $id,
);
throw new Exception(format_string('Entity @type with ID @id cannot be saved, as it belongs to a locked snapshot.', $params));
}
}
/**
* Implements hook_node_insert().
*/
function zariz_node_insert($node) {
if ($node->type != 'branch') {
return;
}
if (!variable_get('zariz_master_branch')) {
// Set the branch as the "master" branch.
variable_set('zariz_master_branch', $node->nid);
}
$wrapper = entity_metadata_wrapper('node', $node);
$parent_branch = $wrapper->field_parent_branch->value();
$parent_snapshot = $parent_branch ? zariz_get_snapshot_from_branch($parent_branch->nid) : NULL;
// Create a new Snapshot.
$snapshot = zariz_get_snapshot_from_branch($node->nid, TRUE, 0, $parent_snapshot);
$snapshot->save();
}
/**
* Implements hook_og_membership_insert().
*/
function zariz_og_membership_insert(OgMembership $og_membership) {
if (!empty($og_membership->_skip_zariz_snapshot)) {
// Entity explicitly set to skip snapshot creation. This can be used by
// implementing modules that want for example to create several entities and
// only then create a snapshot for them.
return;
}
$entity_type = $og_membership->entity_type;
$id = $og_membership->etid;
$wrapper = entity_metadata_wrapper($entity_type, $id);
$bundle = $wrapper->getBundle();
if (!zariz_is_bundle_tracked($entity_type, $bundle)) {
return;
}
$branch_id = $og_membership->gid;
// Add entity to snapshot.
zariz_snapshot_set_last_entity_id($branch_id, $entity_type, $id);
}
/**
* Set the last entity ID of a snapshot.
*
* @param $branch_id
* The branch ID.
* @parm $id
* The entity ID to set as last.
* @param $entity_type
* The entity type of the last ID that should be registered.
* @param Snapshot $snapshot
* Optional; The snapshot to set. If NULL, the latest snapshot or a new one
* if none found, will be used.
*
* @return
* The saved Snapshot entity.
*/
function zariz_snapshot_set_last_entity_id($branch_id, $entity_type, $id, Snapshot $snapshot = NULL) {
$field_name = zariz_get_last_id_field_name($entity_type);
$snapshot = $snapshot ? $snapshot : zariz_get_snapshot_from_branch($branch_id, TRUE);
$snapshot_wrapper = entity_metadata_wrapper('snapshot', $snapshot);
$snapshot_wrapper->{$field_name}->set($id);
$snapshot_wrapper->save();
return $snapshot;
}
/**
* Returns the latest snapshot object of a branch.
*
* @param $branch_id
* The branch ID.
* @param $create
* Determine if a new Snapshot should be created if none exists. $offset
* should be set to 0. Defaults to FALSE.
* @param $offset
* The offest from the latest snapshot. Defaults to 0, which is the latest
* snapshot created.
* * @param $parent_snapshot
* Optioanl; The parent snapshot object. If not set, function will try to get
* a previous parent from same branch.
*
* @return Snapshot
* The Snapshot entity object if found, or un-unsaved Snapshot entity if non
* found and $create set to TRUE, or NULL.
*
*/
function zariz_get_snapshot_from_branch($branch_id, $create = FALSE, $offset = 0, $parent_snapshot = NULL) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'snapshot')
->propertyCondition('nid', $branch_id)
->range($offset, $offset + 1)
->propertyOrderBy('sid', 'DESC');
if ($create) {
// If we need to create a snapshot, make sure we are not returning an
// existing yet locked one.
$query->propertyCondition('locked', TRUE, '<>');
}
$result = $query->execute();
if (empty($result['snapshot'])) {
if (!$create) {
return;
}
if ($offset) {
throw new Exception('Offset value should be 0, when calling zariz_get_snapshot_from_branch() with $create set to TRUE.');
}
$parent_snapshot = $parent_snapshot ? $parent_snapshot : zariz_get_snapshot_from_branch($branch_id, FALSE);
$values = array(
'nid' => $branch_id,
'parent_sid' => !empty($parent_snapshot) ? $parent_snapshot->sid : NULL,
'created' => time(),
);
$snapshot = entity_create('snapshot', $values);
if ($parent_snapshot) {
// Copy the last entity ID fields from the parent.
foreach (zariz_get_tracked_entity_types() as $info) {
$field_name = $info['last id field name'];
$snapshot->{$field_name} = $parent_snapshot->{$field_name};
}
}
return $snapshot;
}
$sid = key($result['snapshot']);
return entity_load_single('snapshot', $sid);
}
/**
* Get the Snapshot an entity belongs to.
*
* @param $entity_type
* The entity type.
* @param $entity
* The entity object.
*
* @return Snapshot
* The Snapshot object, or NULL if not found.
*/
function zariz_get_snapshot_from_content($entity_type, $entity) {
list($id,, $bundle) = entity_extract_ids($entity_type, $entity);
if (!zariz_is_bundle_tracked($entity_type, $bundle)) {
// Bundle isn't tracked by Zariz.
return;
}
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$branch_id = $wrapper->field_branch->getIdentifier();
$field_name = zariz_get_last_id_field_name($entity_type);
$query = new EntityFieldQuery();
$result = $query