/
ezcontentoperationcollection.php
1593 lines (1374 loc) · 60.6 KB
/
ezcontentoperationcollection.php
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 containing the eZContentOperationCollection class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
* @package kernel
*/
/*!
\class eZContentOperationCollection ezcontentoperationcollection.php
\brief The class eZContentOperationCollection does
*/
class eZContentOperationCollection
{
/**
* Use by {@see beginTransaction()} and {@see commitTransaction()} to handle nested publish operations
*/
private static $operationsStack = 0;
/*!
Constructor
*/
function eZContentOperationCollection()
{
}
static public function readNode( $nodeID )
{
}
static public function readObject( $nodeID, $userID, $languageCode )
{
if ( $languageCode != '' )
{
$node = eZContentObjectTreeNode::fetch( $nodeID, $languageCode );
}
else
{
$node = eZContentObjectTreeNode::fetch( $nodeID );
}
if ( $node === null )
// return $Module->handleError( eZError::KERNEL_NOT_AVAILABLE, 'kernel' );
return false;
$object = $node->attribute( 'object' );
if ( $object === null )
// return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
{
return false;
}
/*
if ( !$object->attribute( 'can_read' ) )
{
// return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
return false;
}
*/
return array( 'status' => true, 'object' => $object, 'node' => $node );
}
static public function loopNodes( $nodeID )
{
return array( 'parameters' => array( array( 'parent_node_id' => 3 ),
array( 'parent_node_id' => 5 ),
array( 'parent_node_id' => 12 ) ) );
}
static public function loopNodeAssignment( $objectID, $versionNum )
{
$object = eZContentObject::fetch( $objectID );
$version = $object->version( $versionNum );
$nodeAssignmentList = $version->attribute( 'node_assignments' );
$parameters = array();
foreach ( $nodeAssignmentList as $nodeAssignment )
{
if ( $nodeAssignment->attribute( 'parent_node' ) > 0 )
{
if ( $nodeAssignment->attribute( 'is_main' ) == 1 )
{
$mainNodeID = self::publishNode( $nodeAssignment->attribute( 'parent_node' ), $objectID, $versionNum, false );
}
else
{
$parameters[] = array( 'parent_node_id' => $nodeAssignment->attribute( 'parent_node' ) );
}
}
}
for ( $i = 0; $i < count( $parameters ); $i++ )
{
$parameters[$i]['main_node_id'] = $mainNodeID;
}
return array( 'parameters' => $parameters );
}
function publishObjectExtensionHandler( $contentObjectID, $contentObjectVersion )
{
eZContentObjectEditHandler::executePublish( $contentObjectID, $contentObjectVersion );
}
/**
* Starts a database transaction.
*/
static public function beginTransaction()
{
// We only start a transaction if another content publish operation hasn't been started
if ( ++self::$operationsStack === 1 )
{
eZDB::instance()->begin();
}
}
/**
* Commit a previously started database transaction.
*/
static public function commitTransaction()
{
if ( --self::$operationsStack === 0 )
{
eZDB::instance()->commit();
}
}
static public function setVersionStatus( $objectID, $versionNum, $status )
{
$object = eZContentObject::fetch( $objectID );
if ( !$versionNum )
{
$versionNum = $object->attribute( 'current_version' );
}
$version = $object->version( $versionNum );
if ( !$version )
return;
$version->setAttribute( 'status', $status );
$version->store();
}
static public function setObjectStatusPublished( $objectID, $versionNum )
{
$object = eZContentObject::fetch( $objectID );
$version = $object->version( $versionNum );
$db = eZDB::instance();
$db->begin();
$object->setAttribute( 'status', eZContentObject::STATUS_PUBLISHED );
$version->setAttribute( 'status', eZContentObjectVersion::STATUS_PUBLISHED );
$object->setAttribute( 'current_version', $versionNum );
$objectIsAlwaysAvailable = $object->isAlwaysAvailable();
$object->setAttribute( 'language_mask', eZContentLanguage::maskByLocale( $version->translationList( false, false ), $objectIsAlwaysAvailable ) );
if ( $object->attribute( 'published' ) == 0 )
{
$object->setAttribute( 'published', time() );
}
$object->setAttribute( 'modified', time() );
$classID = $object->attribute( 'contentclass_id' );
$class = eZContentClass::fetch( $classID );
$objectName = $class->contentObjectName( $object );
$object->setName( $objectName, $versionNum );
$existingTranslations = $version->translations( false );
foreach( $existingTranslations as $translation )
{
$translatedName = $class->contentObjectName( $object, $versionNum, $translation );
$object->setName( $translatedName, $versionNum, $translation );
}
if ( $objectIsAlwaysAvailable )
{
$initialLanguageID = $object->attribute( 'initial_language_id' );
$object->setAlwaysAvailableLanguageID( $initialLanguageID );
}
$version->store();
$object->store();
eZContentObjectTreeNode::setVersionByObjectID( $objectID, $versionNum );
$nodes = $object->assignedNodes();
foreach ( $nodes as $node )
{
$node->setName( $object->attribute( 'name' ) );
$node->updateSubTreePath();
}
$db->commit();
/* Check if current class is the user class, and if so, clean up the user-policy cache */
if ( in_array( $classID, eZUser::contentClassIDs() ) )
{
eZUser::purgeUserCacheByUserId( $object->attribute( 'id' ) );
}
}
static public function attributePublishAction( $objectID, $versionNum )
{
$object = eZContentObject::fetch( $objectID );
$nodes = $object->assignedNodes();
$version = $object->version( $versionNum );
$contentObjectAttributes = $object->contentObjectAttributes( true, $versionNum, $version->initialLanguageCode(), false );
foreach ( $contentObjectAttributes as $contentObjectAttribute )
{
$contentObjectAttribute->onPublish( $object, $nodes );
}
}
/*!
\static
Generates the related viewcaches (PreGeneration) for the content object.
It will only do this if [ContentSettings]/PreViewCache in site.ini is enabled.
\param $objectID The ID of the content object to generate caches for.
*/
static public function generateObjectViewCache( $objectID )
{
eZContentCacheManager::generateObjectViewCache( $objectID );
}
/*!
\static
Clears the related viewcaches for the content object using the smart viewcache system.
\param $objectID The ID of the content object to clear caches for
\param $versionNum The version of the object to use or \c true for current version
\param $additionalNodeList An array with node IDs to add to clear list,
or \c false for no additional nodes.
*/
static public function clearObjectViewCache( $objectID, $versionNum = true, $additionalNodeList = false )
{
eZContentCacheManager::clearContentCacheIfNeeded( $objectID, $versionNum, $additionalNodeList );
}
static public function publishNode( $parentNodeID, $objectID, $versionNum, $mainNodeID )
{
$object = eZContentObject::fetch( $objectID );
$nodeAssignment = eZNodeAssignment::fetch( $objectID, $versionNum, $parentNodeID );
$version = $object->version( $versionNum );
$fromNodeID = $nodeAssignment->attribute( 'from_node_id' );
$originalObjectID = $nodeAssignment->attribute( 'contentobject_id' );
$nodeID = $nodeAssignment->attribute( 'parent_node' );
$opCode = $nodeAssignment->attribute( 'op_code' );
$parentNode = eZContentObjectTreeNode::fetch( $nodeID );
// if parent doesn't exist, return. See issue #18320
if ( !$parentNode instanceof eZContentObjectTreeNode )
{
eZDebug::writeError( "Parent node doesn't exist. object id: $objectID, node_assignment id: " . $nodeAssignment->attribute( 'id' ), __METHOD__ );
return;
}
$parentNodeID = $parentNode->attribute( 'node_id' );
$existingNode = null;
$db = eZDB::instance();
$db->begin();
if ( strlen( $nodeAssignment->attribute( 'parent_remote_id' ) ) > 0 )
{
$existingNode = eZContentObjectTreeNode::fetchByRemoteID( $nodeAssignment->attribute( 'parent_remote_id' ) );
}
if ( !$existingNode );
{
$existingNode = eZContentObjectTreeNode::findNode( $nodeID , $object->attribute( 'id' ), true );
}
$updateSectionID = false;
// now we check the op_code to see what to do
if ( ( $opCode & 1 ) == eZNodeAssignment::OP_CODE_NOP )
{
// There is nothing to do so just return
$db->commit();
if ( $mainNodeID == false )
{
return $object->attribute( 'main_node_id' );
}
return;
}
$updateFields = false;
if ( $opCode == eZNodeAssignment::OP_CODE_MOVE ||
$opCode == eZNodeAssignment::OP_CODE_CREATE )
{
// if ( $fromNodeID == 0 || $fromNodeID == -1)
if ( $opCode == eZNodeAssignment::OP_CODE_CREATE ||
$opCode == eZNodeAssignment::OP_CODE_SET )
{
// If the node already exists it means we have a conflict (for 'CREATE').
// We resolve this by leaving node-assignment data be.
if ( $existingNode == null )
{
$parentNode = eZContentObjectTreeNode::fetch( $nodeID );
$user = eZUser::currentUser();
if ( !eZSys::isShellExecution() and !$user->isAnonymous() )
{
eZContentBrowseRecent::createNew( $user->id(), $parentNode->attribute( 'node_id' ), $parentNode->attribute( 'name' ) );
}
$updateFields = true;
$existingNode = $parentNode->addChild( $object->attribute( 'id' ), true );
if ( $fromNodeID == -1 )
{
$updateSectionID = true;
}
}
elseif ( $opCode == eZNodeAssignment::OP_CODE_SET )
{
$updateFields = true;
}
}
elseif ( $opCode == eZNodeAssignment::OP_CODE_MOVE )
{
if ( $fromNodeID == 0 || $fromNodeID == -1 )
{
eZDebug::writeError( "NodeAssignment '" . $nodeAssignment->attribute( 'id' ) . "' is marked with op_code='$opCode' but has no data in from_node_id. Cannot use it for moving node.", __METHOD__ );
}
else
{
// clear cache for old placement.
$additionalNodeIDList = array( $fromNodeID );
eZContentCacheManager::clearContentCacheIfNeeded( $objectID, $versionNum, $additionalNodeIDList );
$originalNode = eZContentObjectTreeNode::fetchNode( $originalObjectID, $fromNodeID );
if ( $originalNode->attribute( 'main_node_id' ) == $originalNode->attribute( 'node_id' ) )
{
$updateSectionID = true;
}
$originalNode->move( $parentNodeID );
$existingNode = eZContentObjectTreeNode::fetchNode( $originalObjectID, $parentNodeID );
$updateFields = true;
}
}
}
elseif ( $opCode == eZNodeAssignment::OP_CODE_REMOVE )
{
$db->commit();
return;
}
if ( $updateFields )
{
if ( strlen( $nodeAssignment->attribute( 'parent_remote_id' ) ) > 0 )
{
$existingNode->setAttribute( 'remote_id', $nodeAssignment->attribute( 'parent_remote_id' ) );
}
if ( $nodeAssignment->attribute( 'is_hidden' ) )
{
$existingNode->setAttribute( 'is_hidden', 1 );
$existingNode->setAttribute( 'is_invisible', 1 );
}
$existingNode->setAttribute( 'priority', $nodeAssignment->attribute( 'priority' ) );
$existingNode->setAttribute( 'sort_field', $nodeAssignment->attribute( 'sort_field' ) );
$existingNode->setAttribute( 'sort_order', $nodeAssignment->attribute( 'sort_order' ) );
}
$existingNode->setAttribute( 'contentobject_is_published', 1 );
eZDebug::createAccumulatorGroup( 'nice_urls_total', 'Nice urls' );
if ( $mainNodeID > 0 )
{
$existingNodeID = $existingNode->attribute( 'node_id' );
if ( $existingNodeID != $mainNodeID )
{
eZContentBrowseRecent::updateNodeID( $existingNodeID, $mainNodeID );
}
$existingNode->setAttribute( 'main_node_id', $mainNodeID );
}
else
{
$existingNode->setAttribute( 'main_node_id', $existingNode->attribute( 'node_id' ) );
}
$existingNode->store();
if ( $updateSectionID )
{
eZContentOperationCollection::updateSectionID( $objectID, $versionNum );
}
$db->commit();
if ( $mainNodeID == false )
{
return $existingNode->attribute( 'node_id' );
}
}
static public function updateSectionID( $objectID, $versionNum )
{
$object = eZContentObject::fetch( $objectID );
$version = $object->version( $versionNum );
if ( $versionNum == 1 or
$object->attribute( 'current_version' ) == $versionNum )
{
$newMainAssignment = null;
$newMainAssignments = eZNodeAssignment::fetchForObject( $objectID, $versionNum, 1 );
if ( isset( $newMainAssignments[0] ) )
{
$newMainAssignment = $newMainAssignments[0];
}
// we should not update section id for toplevel nodes
if ( $newMainAssignment && $newMainAssignment->attribute( 'parent_node' ) != 1 )
{
// We should check if current object already has been updated for section_id
// If yes we should not update object section_id by $parentNodeSectionID
$sectionID = $object->attribute( 'section_id' );
if ( $sectionID > 0 )
return;
$newParentObject = $newMainAssignment->getParentObject();
if ( !$newParentObject )
{
return array( 'status' => eZModuleOperationInfo::STATUS_CANCELLED );
}
$parentNodeSectionID = $newParentObject->attribute( 'section_id' );
$object->setAttribute( 'section_id', $parentNodeSectionID );
$object->store();
}
return;
}
$newMainAssignmentList = eZNodeAssignment::fetchForObject( $objectID, $versionNum, 1 );
$newMainAssignment = ( count( $newMainAssignmentList ) ) ? array_pop( $newMainAssignmentList ) : null;
$currentVersion = $object->attribute( 'current' );
// Here we need to fetch published nodes and not old node assignments.
$oldMainNode = $object->mainNode();
if ( $newMainAssignment && $oldMainNode
&& $newMainAssignment->attribute( 'parent_node' ) != $oldMainNode->attribute( 'parent_node_id' ) )
{
$oldMainParentNode = $oldMainNode->attribute( 'parent' );
if ( $oldMainParentNode )
{
$oldParentObject = $oldMainParentNode->attribute( 'object' );
$oldParentObjectSectionID = $oldParentObject->attribute( 'section_id' );
if ( $oldParentObjectSectionID == $object->attribute( 'section_id' ) )
{
$newParentNode = $newMainAssignment->attribute( 'parent_node_obj' );
if ( !$newParentNode )
return;
$newParentObject = $newParentNode->attribute( 'object' );
if ( !$newParentObject )
return;
$newSectionID = $newParentObject->attribute( 'section_id' );
if ( $newSectionID != $object->attribute( 'section_id' ) )
{
$oldSectionID = $object->attribute( 'section_id' );
$object->setAttribute( 'section_id', $newSectionID );
$db = eZDB::instance();
$db->begin();
$object->store();
$mainNodeID = $object->attribute( 'main_node_id' );
if ( $mainNodeID > 0 )
{
eZContentObjectTreeNode::assignSectionToSubTree( $mainNodeID,
$newSectionID,
$oldSectionID );
}
$db->commit();
}
}
}
}
}
static public function removeOldNodes( $objectID, $versionNum )
{
$object = eZContentObject::fetch( $objectID );
if ( !$object instanceof eZContentObject )
{
eZDebug::writeError( 'Unable to find object #' . $objectID, __METHOD__ );
return;
}
$version = $object->version( $versionNum );
if ( !$version instanceof eZContentObjectVersion )
{
eZDebug::writeError( 'Unable to find version #' . $versionNum . ' for object #' . $objectID, __METHOD__ );
return;
}
$moveToTrash = true;
$assignedExistingNodes = $object->attribute( 'assigned_nodes' );
$curentVersionNodeAssignments = $version->attribute( 'node_assignments' );
$removeParentNodeList = array();
$removeAssignmentsList = array();
foreach ( $curentVersionNodeAssignments as $nodeAssignment )
{
$nodeAssignmentOpcode = $nodeAssignment->attribute( 'op_code' );
if ( $nodeAssignmentOpcode == eZNodeAssignment::OP_CODE_REMOVE ||
$nodeAssignmentOpcode == eZNodeAssignment::OP_CODE_REMOVE_NOP )
{
$removeAssignmentsList[] = $nodeAssignment->attribute( 'id' );
if ( $nodeAssignmentOpcode == eZNodeAssignment::OP_CODE_REMOVE )
{
$removeParentNodeList[] = $nodeAssignment->attribute( 'parent_node' );
}
}
}
$db = eZDB::instance();
$db->begin();
foreach ( $assignedExistingNodes as $node )
{
if ( in_array( $node->attribute( 'parent_node_id' ), $removeParentNodeList ) )
{
eZContentObjectTreeNode::removeSubtrees( array( $node->attribute( 'node_id' ) ), $moveToTrash );
}
}
if ( count( $removeAssignmentsList ) > 0 )
{
eZNodeAssignment::purgeByID( $removeAssignmentsList );
}
$db->commit();
}
// New function which resets the op_code field when the object is published.
static public function resetNodeassignmentOpcodes( $objectID, $versionNum )
{
$object = eZContentObject::fetch( $objectID );
$version = $object->version( $versionNum );
$nodeAssignments = $version->attribute( 'node_assignments' );
foreach ( $nodeAssignments as $nodeAssignment )
{
if ( ( $nodeAssignment->attribute( 'op_code' ) & 1 ) == eZNodeAssignment::OP_CODE_EXECUTE )
{
$nodeAssignment->setAttribute( 'op_code', ( $nodeAssignment->attribute( 'op_code' ) & ~1 ) );
$nodeAssignment->store();
}
}
}
/**
* Registers the object in search engine.
*
* @note Transaction unsafe. If you call several transaction unsafe methods you must enclose
* the calls within a db transaction; thus within db->begin and db->commit.
*
* @param int $objectID Id of the object.
* @param int $version Operation collection passes this default param. Not used in the method
* @param bool $isMoved true if node is being moved
*/
static public function registerSearchObject( $objectID, $version = null, $isMoved = false )
{
$objectID = (int)$objectID;
eZDebug::createAccumulatorGroup( 'search_total', 'Search Total' );
$ini = eZINI::instance( 'site.ini' );
$insertPendingAction = false;
$object = null;
switch ( $ini->variable( 'SearchSettings', 'DelayedIndexing' ) )
{
case 'enabled':
$insertPendingAction = true;
break;
case 'classbased':
$classList = $ini->variable( 'SearchSettings', 'DelayedIndexingClassList' );
$object = eZContentObject::fetch( $objectID );
if ( is_array( $classList ) && in_array( $object->attribute( 'class_identifier' ), $classList ) )
{
$insertPendingAction = true;
}
}
if ( $insertPendingAction )
{
$action = $isMoved ? 'index_moved_node' : 'index_object';
eZDB::instance()->query( "INSERT INTO ezpending_actions( action, param ) VALUES ( '$action', '$objectID' )" );
return;
}
if ( $object === null )
$object = eZContentObject::fetch( $objectID );
// Register the object in the search engine.
$needCommit = eZSearch::needCommit();
if ( eZSearch::needRemoveWithUpdate() )
{
eZDebug::accumulatorStart( 'remove_object', 'search_total', 'remove object' );
eZSearch::removeObjectById( $objectID );
eZDebug::accumulatorStop( 'remove_object' );
}
eZDebug::accumulatorStart( 'add_object', 'search_total', 'add object' );
if ( !eZSearch::addObject( $object, $needCommit ) )
{
eZDebug::writeError( "Failed adding object ID {$object->attribute( 'id' )} in the search engine", __METHOD__ );
}
eZDebug::accumulatorStop( 'add_object' );
}
/*!
\note Transaction unsafe. If you call several transaction unsafe methods you must enclose
the calls within a db transaction; thus within db->begin and db->commit.
*/
static public function createNotificationEvent( $objectID, $versionNum )
{
$event = eZNotificationEvent::create( 'ezpublish', array( 'object' => $objectID,
'version' => $versionNum ) );
$event->store();
}
/*!
Copies missing translations from published version to the draft.
*/
static public function copyTranslations( $objectID, $versionNum )
{
$object = eZContentObject::fetch( $objectID );
if ( !$object instanceof eZContentObject )
{
return array( 'status' => eZModuleOperationInfo::STATUS_CANCELLED );
}
$publishedVersionNum = $object->attribute( 'current_version' );
if ( !$publishedVersionNum )
{
return;
}
$publishedVersion = $object->version( $publishedVersionNum );
$publishedVersionTranslations = $publishedVersion->translations();
$publishedLanguages = eZContentLanguage::languagesByMask( $object->attribute( 'language_mask' ) );
$publishedLanguageCodes = array_keys( $publishedLanguages );
$version = $object->version( $versionNum );
$versionTranslationList = $version->translationList( false, false );
foreach ( $publishedVersionTranslations as $translation )
{
if ( in_array( $translation->attribute( 'language_code' ), $versionTranslationList )
|| !in_array( $translation->attribute( 'language_code' ), $publishedLanguageCodes ) )
{
continue;
}
$contentObjectAttributes = $translation->objectAttributes();
foreach ( $contentObjectAttributes as $attribute )
{
$clonedAttribute = $attribute->cloneContentObjectAttribute( $versionNum, $publishedVersionNum, $objectID );
$clonedAttribute->sync();
}
}
$version->updateLanguageMask();
}
/*!
Updates non-translatable attributes.
*/
static public function updateNontranslatableAttributes( $objectID, $versionNum )
{
$object = eZContentObject::fetch( $objectID );
$version = $object->version( $versionNum );
$nonTranslatableAttributes = $version->nonTranslatableAttributesToUpdate();
if ( $nonTranslatableAttributes )
{
$attributes = $version->contentObjectAttributes( $version->initialLanguageCode() );
$attributeByClassAttrID = array();
foreach ( $attributes as $attribute )
{
$attributeByClassAttrID[$attribute->attribute( 'contentclassattribute_id' )] = $attribute;
}
foreach ( $nonTranslatableAttributes as $attributeToUpdate )
{
$originalAttribute =& $attributeByClassAttrID[$attributeToUpdate->attribute( 'contentclassattribute_id' )];
if ( $originalAttribute )
{
unset( $tmp );
$tmp = $attributeToUpdate;
$tmp->initialize( $attributeToUpdate->attribute( 'version' ), $originalAttribute );
$tmp->setAttribute( 'id', $attributeToUpdate->attribute( 'id' ) );
$tmp->setAttribute( 'language_code', $attributeToUpdate->attribute( 'language_code' ) );
$tmp->setAttribute( 'language_id', $attributeToUpdate->attribute( 'language_id' ) );
$tmp->setAttribute( 'attribute_original_id', $originalAttribute->attribute( 'id' ) );
$tmp->store();
$tmp->postInitialize( $attributeToUpdate->attribute( 'version' ), $originalAttribute );
}
}
}
}
static public function removeTemporaryDrafts( $objectID, $versionNum )
{
$object = eZContentObject::fetch( $objectID );
$object->cleanupInternalDrafts( eZUser::currentUserID() );
}
/**
* Moves a node
*
* @param int $nodeID
* @param int $objectID
* @param int $newParentNodeID
*
* @return array An array with operation status, always true
*/
static public function moveNode( $nodeID, $objectID, $newParentNodeID )
{
if( !eZContentObjectTreeNodeOperations::move( $nodeID, $newParentNodeID ) )
{
eZDebug::writeError( "Failed to move node $nodeID as child of parent node $newParentNodeID",
__METHOD__ );
return array( 'status' => false );
}
eZContentObject::fixReverseRelations( $objectID, 'move' );
return array( 'status' => true );
}
/**
* Adds a new nodeAssignment
*
* @param int $nodeID
* @param int $objectId
* @param array $selectedNodeIDArray
*
* @return array An array with operation status, always true
*/
static public function addAssignment( $nodeID, $objectID, $selectedNodeIDArray )
{
$userClassIDArray = eZUser::contentClassIDs();
$object = eZContentObject::fetch( $objectID );
$class = $object->contentClass();
$nodeAssignmentList = eZNodeAssignment::fetchForObject( $objectID, $object->attribute( 'current_version' ), 0, false );
$assignedNodes = $object->assignedNodes();
$parentNodeIDArray = array();
foreach ( $assignedNodes as $assignedNode )
{
$append = false;
foreach ( $nodeAssignmentList as $nodeAssignment )
{
if ( $nodeAssignment['parent_node'] == $assignedNode->attribute( 'parent_node_id' ) )
{
$append = true;
break;
}
}
if ( $append )
{
$parentNodeIDArray[] = $assignedNode->attribute( 'parent_node_id' );
}
}
$db = eZDB::instance();
$db->begin();
$locationAdded = false;
$node = eZContentObjectTreeNode::fetch( $nodeID );
foreach ( $selectedNodeIDArray as $selectedNodeID )
{
if ( !in_array( $selectedNodeID, $parentNodeIDArray ) )
{
$parentNode = eZContentObjectTreeNode::fetch( $selectedNodeID );
$parentNodeObject = $parentNode->attribute( 'object' );
$canCreate = ( ( $parentNode->checkAccess( 'create', $class->attribute( 'id' ), $parentNodeObject->attribute( 'contentclass_id' ) ) == 1 ) ||
( $parentNode->canAddLocation() && $node->canRead() ) );
if ( $canCreate )
{
$insertedNode = $object->addLocation( $selectedNodeID, true );
// Now set is as published and fix main_node_id
$insertedNode->setAttribute( 'contentobject_is_published', 1 );
$insertedNode->setAttribute( 'main_node_id', $node->attribute( 'main_node_id' ) );
$insertedNode->setAttribute( 'contentobject_version', $node->attribute( 'contentobject_version' ) );
// Make sure the url alias is set updated.
$insertedNode->updateSubTreePath();
$insertedNode->sync();
$locationAdded = true;
}
}
}
if ( $locationAdded )
{
//call appropriate method from search engine
eZSearch::addNodeAssignment( $nodeID, $objectID, $selectedNodeIDArray );
// clear user policy cache if this was a user object
if ( in_array( $object->attribute( 'contentclass_id' ), $userClassIDArray ) )
{
eZUser::purgeUserCacheByUserId( $object->attribute( 'id' ) );
}
}
$db->commit();
eZContentCacheManager::clearContentCacheIfNeeded( $objectID );
return array( 'status' => true );
}
/**
* Removes nodes
*
* This function does not check about permissions, this is the responsibility of the caller!
*
* @param array $removeNodeIdList Array of Node ID to remove
*
* @return array An array with operation status, always true
*/
static public function removeNodes( array $removeNodeIdList )
{
$mainNodeChanged = array();
$nodeAssignmentIdList = array();
$objectIdList = array();
$db = eZDB::instance();
$db->begin();
foreach ( $removeNodeIdList as $nodeId )
{
$node = eZContentObjectTreeNode::fetch($nodeId);
$objectId = $node->attribute( 'contentobject_id' );
foreach ( eZNodeAssignment::fetchForObject( $objectId,
eZContentObject::fetch( $objectId )->attribute( 'current_version' ),
0, false ) as $nodeAssignmentKey => $nodeAssignment )
{
if ( $nodeAssignment['parent_node'] == $node->attribute( 'parent_node_id' ) )
{
$nodeAssignmentIdList[$nodeAssignment['id']] = 1;
}
}
if ( $nodeId == $node->attribute( 'main_node_id' ) )
$mainNodeChanged[$objectId] = 1;
$node->removeThis();
if ( !isset( $objectIdList[$objectId] ) )
$objectIdList[$objectId] = eZContentObject::fetch( $objectId );
}
eZNodeAssignment::purgeByID( array_keys( $nodeAssignmentIdList ) );
foreach ( array_keys( $mainNodeChanged ) as $objectId )
{
$allNodes = $objectIdList[$objectId]->assignedNodes();
// Registering node that will be promoted as 'main'
$mainNodeChanged[$objectId] = $allNodes[0];
eZContentObjectTreeNode::updateMainNodeID( $allNodes[0]->attribute( 'node_id' ), $objectId, false, $allNodes[0]->attribute( 'parent_node_id' ) );
}
// Give other search engines that the default one a chance to reindex
// when removing locations.
if ( !eZSearch::getEngine() instanceof eZSearchEngine )
{
foreach ( array_keys( $objectIdList ) as $objectId )
eZContentOperationCollection::registerSearchObject( $objectId );
}
$db->commit();
//call appropriate method from search engine
eZSearch::removeNodes( $removeNodeIdList );
$userClassIdList = eZUser::contentClassIDs();
foreach ( $objectIdList as $objectId => $object )
{
eZContentCacheManager::clearObjectViewCacheIfNeeded( $objectId );
// clear user policy cache if this was a user object
if ( in_array( $object->attribute( 'contentclass_id' ), $userClassIdList ) )
{
eZUser::purgeUserCacheByUserId( $object->attribute( 'id' ) );
}
}
// Triggering content/cache filter for Http cache purge
ezpEvent::getInstance()->filter( 'content/cache', $removeNodeIdList );
// we don't clear template block cache here since it's cleared in eZContentObjectTreeNode::removeNode()
return array( 'status' => true );
}
/**
* Deletes a content object, or a list of content objects
*
* @param array $deleteIDArray
* @param bool $moveToTrash
*
* @return array An array with operation status, always true
*/
static public function deleteObject( $deleteIDArray, $moveToTrash = false )
{
$ini = eZINI::instance();
$aNodes = eZContentObjectTreeNode::fetch( $deleteIDArray );
if( !is_array( $aNodes ) )
{
$aNodes = array( $aNodes );
}
$delayedIndexingValue = $ini->variable( 'SearchSettings', 'DelayedIndexing' );
if ( $delayedIndexingValue === 'enabled' || $delayedIndexingValue === 'classbased' )
{
$pendingActionsToDelete = array();
$classList = $ini->variable( 'SearchSettings', 'DelayedIndexingClassList' ); // Will be used below if DelayedIndexing is classbased
$assignedNodesByObject = array();
$nodesToDeleteByObject = array();
foreach ( $aNodes as $node )
{
$object = $node->object();
$objectID = $object->attribute( 'id' );
$assignedNodes = $object->attribute( 'assigned_nodes' );
// Only delete pending action if this is the last object's node that is requested for deletion
// But $deleteIDArray can also contain all the object's node (mainly if this method is called programmatically)
// So if this is not the last node, then store its id in a temp array
// This temp array will then be compared to the whole object's assigned nodes array
if ( count( $assignedNodes ) > 1 )
{
// $assignedNodesByObject will be used as a referent to check if we want to delete all lasting nodes
if ( !isset( $assignedNodesByObject[$objectID] ) )
{
$assignedNodesByObject[$objectID] = array();
foreach ( $assignedNodes as $assignedNode )
{
$assignedNodesByObject[$objectID][] = $assignedNode->attribute( 'node_id' );
}
}
// Store the node assignment we want to delete
// Then compare the array to the referent node assignment array
$nodesToDeleteByObject[$objectID][] = $node->attribute( 'node_id' );
$diff = array_diff( $assignedNodesByObject[$objectID], $nodesToDeleteByObject[$objectID] );
if ( !empty( $diff ) ) // We still have more node assignments for object, pending action is not to be deleted considering this iteration
{
continue;
}
}
if ( $delayedIndexingValue !== 'classbased' ||
( is_array( $classList ) && in_array( $object->attribute( 'class_identifier' ), $classList ) ) )
{
$pendingActionsToDelete[] = $objectID;
}
}
if ( !empty( $pendingActionsToDelete ) )
{
$filterConds = array( 'param' => array ( $pendingActionsToDelete ) );
eZPendingActions::removeByAction( 'index_object', $filterConds );
}
}
// Add assigned nodes to the clear cache list
// This allows to clear assigned nodes separately (e.g. in reverse proxies)
// as once content is removed, there is no more assigned nodes, and http cache clear is not possible any more.
// See https://jira.ez.no/browse/EZP-22447
foreach ( $aNodes as $node )
{
eZContentCacheManager::addAdditionalNodeIDPerObject( $node->attribute( 'contentobject_id' ), $node->attribute( 'node_id' ) );
}
eZContentObjectTreeNode::removeSubtrees( $deleteIDArray, $moveToTrash );
return array( 'status' => true );
}
/**
* Changes an contentobject's status
*
* @param int $nodeID
*