forked from ezsystems/ezpublish-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ezcontentclass.php
2026 lines (1783 loc) · 77 KB
/
ezcontentclass.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 eZContentClass 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 eZContentClass ezcontentclass.php
\ingroup eZKernel
\brief Handles eZ Publish content classes
\sa eZContentObject
*/
class eZContentClass extends eZPersistentObject
{
const VERSION_STATUS_DEFINED = 0;
const VERSION_STATUS_TEMPORARY = 1;
const VERSION_STATUS_MODIFIED = 2;
/**
* Max length of content object name.
* @var int
*/
const CONTENT_OBJECT_NAME_MAX_LENGTH = 255;
function eZContentClass( $row )
{
if ( is_array( $row ) )
{
$this->eZPersistentObject( $row );
$this->VersionCount = false;
$this->InGroups = null;
$this->AllGroups = null;
if ( isset( $row["version_count"] ) )
$this->VersionCount = $row["version_count"];
$this->NameList = new eZContentClassNameList();
if ( isset( $row['serialized_name_list'] ) )
$this->NameList->initFromSerializedList( $row['serialized_name_list'] );
else
$this->NameList->initDefault();
$this->DescriptionList = new eZSerializedObjectNameList();
if ( isset( $row['serialized_description_list'] ) )
$this->DescriptionList->initFromSerializedList( $row['serialized_description_list'] );
else
$this->DescriptionList->initDefault();
}
$this->DataMap = false;
}
static function definition()
{
static $definition = array( "fields" => array( "id" => array( 'name' => 'ID',
'datatype' => 'integer',
'default' => 0,
'required' => true ),
"version" => array( 'name' => 'Version',
'datatype' => 'integer',
'default' => 0,
'required' => true ),
"serialized_name_list" => array( 'name' => 'SerializedNameList',
'datatype' => 'string',
'default' => '',
'required' => true ),
'serialized_description_list' => array( 'name' => 'SerializedDescriptionList',
'datatype' => 'string',
'default' => '',
'required' => true ),
"identifier" => array( 'name' => "Identifier",
'datatype' => 'string',
'default' => '',
'required' => true,
'max_length' => 50 ),
"contentobject_name" => array( 'name' => "ContentObjectName",
'datatype' => 'string',
'default' => '',
'required' => true ),
"url_alias_name" => array( 'name' => "URLAliasName",
'datatype' => 'string',
'default' => '',
'required' => false ),
"creator_id" => array( 'name' => "CreatorID",
'datatype' => 'integer',
'default' => 0,
'required' => true,
'foreign_class' => 'eZUser',
'foreign_attribute' => 'contentobject_id',
'multiplicity' => '1..*' ),
"modifier_id" => array( 'name' => "ModifierID",
'datatype' => 'integer',
'default' => 0,
'required' => true,
'foreign_class' => 'eZUser',
'foreign_attribute' => 'contentobject_id',
'multiplicity' => '1..*' ),
"created" => array( 'name' => "Created",
'datatype' => 'integer',
'default' => 0,
'required' => true ),
"remote_id" => array( 'name' => "RemoteID",
'datatype' => 'string',
'default' => '',
'required' => true ),
"modified" => array( 'name' => "Modified",
'datatype' => 'integer',
'default' => 0,
'required' => true ),
"is_container" => array( 'name' => "IsContainer",
'datatype' => 'integer',
'default' => 0,
'required' => true ),
'always_available' => array( 'name' => "AlwaysAvailable",
'datatype' => 'integer',
'default' => 0,
'required' => true ),
'language_mask' => array( 'name' => "LanguageMask",
'datatype' => 'integer',
'default' => 0,
'required' => true ),
'initial_language_id' => array( 'name' => "InitialLanguageID",
'datatype' => 'integer',
'default' => 0,
'required' => true,
'foreign_class' => 'eZContentLanguage',
'foreign_attribute' => 'id',
'multiplicity' => '1..*' ),
'sort_field' => array( 'name' => 'SortField',
'datatype' => 'integer',
'default' => 1,
'required' => true ),
'sort_order' => array( 'name' => 'SortOrder',
'datatype' => 'integer',
'default' => 1,
'required' => true ) ),
"keys" => array( "id", "version" ),
"function_attributes" => array( "data_map" => "dataMap",
'object_count' => 'objectCount',
'object_list' => 'objectList',
'version_count' => 'versionCount',
'version_status' => 'versionStatus',
'remote_id' => 'remoteID', // Note: This overrides remote_id field
'ingroup_list' => 'fetchGroupList',
'ingroup_id_list' => 'fetchGroupIDList',
'match_ingroup_id_list' => 'fetchMatchGroupIDList',
'group_list' => 'fetchAllGroups',
'creator' => 'creator',
'modifier' => 'modifier',
'can_instantiate_languages' => 'canInstantiateLanguages',
'name' => 'name',
'nameList' => 'nameList',
'description' => 'description',
'descriptionList' => 'descriptionList',
'languages' => 'languages',
'prioritized_languages' => 'prioritizedLanguages',
'prioritized_languages_js_array' => 'prioritizedLanguagesJsArray',
'can_create_languages' => 'canCreateLanguages',
'top_priority_language_locale' => 'topPriorityLanguageLocale',
'always_available_language' => 'alwaysAvailableLanguage' ),
'set_functions' => array( 'name' => 'setName' ),
"increment_key" => "id",
"class_name" => "eZContentClass",
"sort" => array( "id" => "asc" ),
"name" => "ezcontentclass" );
return $definition;
}
function __clone()
{
unset( $this->Version );
unset( $this->InGroups );
unset( $this->AllGroups );
unset( $this->CanInstantiateLanguages );
unset( $this->VersionCount );
$this->ID = null;
$this->RemoteID = eZRemoteIdUtility::generate( 'class' );
}
/*!
Creates an 'eZContentClass' object.
To specify contentclass name use either $optionalValues['serialized_name_list'] or
combination of $optionalValues['name'] and/or $languageLocale.
In case of conflict(when both 'serialized_name_list' and 'name' with/without $languageLocale
are specified) 'serialized_name_list' has top priority. This means that 'name' and
$languageLocale will be ingnored because 'serialized_name_list' already has all needed info
about names and languages.
If 'name' is specified then the contentclass will have a name in $languageLocale(if specified) or
in default language.
If neither of 'serialized_name_list' or 'name' isn't specified then the contentclass will have an empty
name in 'languageLocale'(if specified) or in default language.
'language_mask' and 'initial_language_id' attributes will be set according to specified(either
in 'serialized_name_list' or by $languageLocale) languages.
\return 'eZContentClass' object.
*/
static function create( $userID = false, $optionalValues = array(), $languageLocale = false )
{
$dateTime = time();
if ( !$userID )
$userID = eZUser::currentUserID();
$nameList = new eZContentClassNameList();
if ( isset( $optionalValues['serialized_name_list'] ) )
$nameList->initFromSerializedList( $optionalValues['serialized_name_list'] );
else if ( isset( $optionalValues['name'] ) )
$nameList->initFromString( $optionalValues['name'], $languageLocale );
else
$nameList->initFromString( '', $languageLocale );
$descriptionList = new eZSerializedObjectNameList();
if ( isset( $optionalValues['serialized_description_list'] ) )
$descriptionList->initFromSerializedList( $optionalValues['serialized_description_list'] );
else if ( isset( $optionalValues['description'] ) )
$descriptionList->initFromString( $optionalValues['description'], $languageLocale );
else
$descriptionList->initFromString( '', $languageLocale );
$languageMask = $nameList->languageMask();
$initialLanguageID = $nameList->alwaysAvailableLanguageID();
$contentClassDefinition = eZContentClass::definition();
$row = array(
"id" => null,
"version" => 1,
"serialized_name_list" => $nameList->serializeNames(),
'serialized_description_list' => $descriptionList->serializeNames(),
"identifier" => "",
"contentobject_name" => "",
"creator_id" => $userID,
"modifier_id" => $userID,
"created" => $dateTime,
'remote_id' => eZRemoteIdUtility::generate( 'class' ),
"modified" => $dateTime,
"is_container" => $contentClassDefinition[ 'fields' ][ 'is_container' ][ 'default' ],
"always_available" => $contentClassDefinition[ 'fields' ][ 'always_available' ][ 'default' ],
'language_mask' => $languageMask,
'initial_language_id' => $initialLanguageID,
"sort_field" => $contentClassDefinition[ 'fields' ][ 'sort_field' ][ 'default' ],
"sort_order" => $contentClassDefinition[ 'fields' ][ 'sort_order' ][ 'default' ] );
$row = array_merge( $row, $optionalValues );
$contentClass = new eZContentClass( $row );
// setting 'dirtyData' to make sure the 'NameList' will be stored into db.
$contentClass->NameList->setHasDirtyData( true );
return $contentClass;
}
function instantiateIn( $lang, $userID = false, $sectionID = 0, $versionNumber = false, $versionStatus = eZContentObjectVersion::STATUS_INTERNAL_DRAFT )
{
return eZContentClass::instantiate( $userID, $sectionID, $versionNumber, $lang, $versionStatus );
}
/*!
Creates a new content object instance and stores it.
\param userID user ID (optional), current user if not set (also store object id in session if $userID = false)
\param sectionID section ID (optional), 0 if not set
\param versionNumber version number, create initial version if not set
\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.
*/
function instantiate( $userID = false, $sectionID = 0, $versionNumber = false, $languageCode = false, $versionStatus = eZContentObjectVersion::STATUS_INTERNAL_DRAFT )
{
$attributes = $this->fetchAttributes();
if ( $userID === false )
{
$user = eZUser::currentUser();
$userID = $user->attribute( 'contentobject_id' );
}
if ( $languageCode == false )
{
$languageCode = eZContentObject::defaultLanguage();
}
$object = eZContentObject::create( ezpI18n::tr( "kernel/contentclass", "New %1", null, array( $this->name( $languageCode ) ) ),
$this->attribute( "id" ),
$userID,
$sectionID,
1,
$languageCode );
if ( $this->attribute( 'always_available' ) )
{
$object->setAttribute( 'language_mask', (int)$object->attribute( 'language_mask') | 1 );
}
$db = eZDB::instance();
$db->begin();
$object->store();
$object->assignDefaultStates();
$object->setName( ezpI18n::tr( "kernel/contentclass", "New %1", null, array( $this->name( $languageCode ) ) ), false, $languageCode );
if ( !$versionNumber )
{
$version = $object->createInitialVersion( $userID, $languageCode );
}
else
{
$version = eZContentObjectVersion::create( $object->attribute( "id" ), $userID, $versionNumber, $languageCode );
}
if ( $versionStatus !== false )
{
$version->setAttribute( 'status', $versionStatus );
}
$version->store();
foreach ( $attributes as $attribute )
{
$attribute->instantiate( $object->attribute( 'id' ), $languageCode );
}
if ( isset( $user ) && $user instanceof eZUser && $user->isAnonymous() )
{
$createdObjectIDList = eZPreferences::value( 'ObjectCreationIDList' );
if ( !$createdObjectIDList )
{
$createdObjectIDList = array( $object->attribute( 'id' ) );
}
else
{
$createdObjectIDList = unserialize( $createdObjectIDList );
$createdObjectIDList[] = $object->attribute( 'id' );
}
eZPreferences::setValue( 'ObjectCreationIDList', serialize( $createdObjectIDList ) );
}
$db->commit();
return $object;
}
function canInstantiateClasses()
{
$accessResult = eZUser::currentUser()->hasAccessTo( 'content' , 'create' );
$canInstantiateClasses = 1;
if ( $accessResult['accessWord'] == 'no' )
{
$canInstantiateClasses = 0;
}
return $canInstantiateClasses;
}
// code-template::create-block: can-instantiate-class-list, group-filter, role-caching, class-policy-list, name-instantiate, object-creation, class-sql-creation, static-method
// code-template::auto-generated:START can-instantiate-class-list
// This code is automatically generated from templates/classcreatelist.ctpl
// DO NOT EDIT THIS CODE DIRECTLY, CHANGE THE TEMPLATE FILE INSTEAD
/*!
\static
Finds all classes that the current user can create objects from and returns.
It is also possible to filter the list event more with \a $includeFilter and \a $groupList.
\param $asObject If \c true then it return eZContentClass objects, if not it will
be an associative array with \c name and \c id keys.
\param $includeFilter If \c true then it will include only from class groups defined in
\a $groupList, if not it will exclude those groups.
\param $groupList An array with class group IDs that should be used in filtering, use
\c false if you do not wish to filter at all.
\param $fetchID A unique name for the current fetch, this must be supplied when filtering is
used if you want caching to work.
*/
static function canInstantiateClassList( $asObject = false, $includeFilter = true, $groupList = false, $fetchID = false )
{
$ini = eZINI::instance();
$groupArray = array();
$enableCaching = ( $ini->variable( 'RoleSettings', 'EnableCaching' ) == 'true' );
if ( is_array( $groupList ) )
{
if ( $fetchID == false )
$enableCaching = false;
}
if ( $enableCaching )
{
$http = eZHTTPTool::instance();
$handler = eZExpiryHandler::instance();
$expiredTimeStamp = 0;
if ( $handler->hasTimestamp( 'user-class-cache' ) )
$expiredTimeStamp = $handler->timestamp( 'user-class-cache' );
$classesCachedForUser = $http->sessionVariable( 'ClassesCachedForUser' );
$classesCachedTimestamp = $http->sessionVariable( 'ClassesCachedTimestamp' );
$cacheVar = 'CanInstantiateClassList';
if ( is_array( $groupList ) and $fetchID !== false )
{
$cacheVar = 'CanInstantiateClassListGroup';
}
$user = eZUser::currentUser();
$userID = $user->id();
if ( ( $classesCachedTimestamp >= $expiredTimeStamp ) && $classesCachedForUser == $userID )
{
if ( $http->hasSessionVariable( $cacheVar ) )
{
if ( $fetchID !== false )
{
// Check if the group contains our ID, if not we need to fetch from DB
$groupArray = $http->sessionVariable( $cacheVar );
if ( isset( $groupArray[$fetchID] ) )
{
return $groupArray[$fetchID];
}
}
else
{
return $http->sessionVariable( $cacheVar );
}
}
}
else
{
$http->setSessionVariable( 'ClassesCachedForUser' , $userID );
$http->setSessionVariable( 'ClassesCachedTimestamp', time() );
}
}
$languageCodeList = eZContentLanguage::fetchLocaleList();
$allowedLanguages = array( '*' => array() );
$user = eZUser::currentUser();
$accessResult = $user->hasAccessTo( 'content' , 'create' );
$accessWord = $accessResult['accessWord'];
$classIDArray = array();
$classList = array();
$fetchAll = false;
if ( $accessWord == 'yes' )
{
$fetchAll = true;
$allowedLanguages['*'] = $languageCodeList;
}
else if ( $accessWord == 'no' )
{
// Cannot create any objects, return empty list.
return $classList;
}
else
{
$policies = $accessResult['policies'];
foreach ( $policies as $policyKey => $policy )
{
$classIDArrayPart = '*';
if ( isset( $policy['Class'] ) )
{
$classIDArrayPart = $policy['Class'];
}
$languageCodeArrayPart = $languageCodeList;
if ( isset( $policy['Language'] ) )
{
$languageCodeArrayPart = array_intersect( $policy['Language'], $languageCodeList );
}
// No class limitation for this policy AND no previous limitation(s)
if ( $classIDArrayPart == '*' && empty( $classIDArray ) )
{
$fetchAll = true;
$allowedLanguages['*'] = array_unique( array_merge( $allowedLanguages['*'], $languageCodeArrayPart ) );
}
else if ( is_array( $classIDArrayPart ) )
{
$fetchAll = false;
foreach( $classIDArrayPart as $class )
{
if ( isset( $allowedLanguages[$class] ) )
{
$allowedLanguages[$class] = array_unique( array_merge( $allowedLanguages[$class], $languageCodeArrayPart ) );
}
else
{
$allowedLanguages[$class] = $languageCodeArrayPart;
}
}
$classIDArray = array_merge( $classIDArray, array_diff( $classIDArrayPart, $classIDArray ) );
}
}
}
$db = eZDB::instance();
$filterTableSQL = '';
$filterSQL = '';
// Create extra SQL statements for the class group filters.
if ( is_array( $groupList ) )
{
if ( count( $groupList ) == 0 )
{
return $classList;
}
$filterTableSQL = ', ezcontentclass_classgroup ccg';
$filterSQL = ( " AND" .
" cc.id = ccg.contentclass_id AND" .
" " );
$filterSQL .= $db->generateSQLINStatement( $groupList, 'ccg.group_id', !$includeFilter, true, 'int' );
}
$classNameFilter = eZContentClassName::sqlFilter( 'cc' );
$filterSQL .= " AND cc.id=" . $classNameFilter['from'] . ".contentclass_id";
if ( $fetchAll )
{
// If $asObject is true we fetch all fields in class
$fields = $asObject ? "cc.*, $classNameFilter[nameField]" : "cc.id, $classNameFilter[nameField]";
$rows = $db->arrayQuery( "SELECT DISTINCT $fields " .
"FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from] " .
"WHERE cc.version = " . eZContentClass::VERSION_STATUS_DEFINED . " $filterSQL " .
"ORDER BY $classNameFilter[nameField] ASC" );
$classList = eZPersistentObject::handleRows( $rows, 'eZContentClass', $asObject );
}
else
{
// If the constrained class list is empty we are not allowed to create any class
if ( count( $classIDArray ) == 0 )
{
return $classList;
}
$classIDCondition = $db->generateSQLINStatement( $classIDArray, 'cc.id' );
// If $asObject is true we fetch all fields in class
$fields = $asObject ? "cc.*, $classNameFilter[nameField]" : "cc.id, $classNameFilter[nameField]";
$rows = $db->arrayQuery( "SELECT DISTINCT $fields " .
"FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from] " .
"WHERE $classIDCondition AND" .
" cc.version = " . eZContentClass::VERSION_STATUS_DEFINED . " $filterSQL " .
"ORDER BY $classNameFilter[nameField] ASC" );
$classList = eZPersistentObject::handleRows( $rows, 'eZContentClass', $asObject );
}
if ( $asObject )
{
foreach ( $classList as $key => $class )
{
$id = $class->attribute( 'id' );
if ( isset( $allowedLanguages[$id] ) )
{
$languageCodes = array_unique( array_merge( $allowedLanguages['*'], $allowedLanguages[$id] ) );
}
else
{
$languageCodes = $allowedLanguages['*'];
}
$classList[$key]->setCanInstantiateLanguages( $languageCodes );
}
}
eZDebugSetting::writeDebug( 'kernel-content-class', $classList, "class list fetched from db" );
if ( $enableCaching )
{
if ( $fetchID !== false )
{
$groupArray[$fetchID] = $classList;
$http->setSessionVariable( $cacheVar, $groupArray );
}
else
{
$http->setSessionVariable( $cacheVar, $classList );
}
}
return $classList;
}
// This code is automatically generated from templates/classcreatelist.ctpl
// code-template::auto-generated:END can-instantiate-class-list
/*!
\return The creator of the class as an eZUser object by using the $CreatorID as user ID.
*/
function creator()
{
if ( isset( $this->CreatorID ) and $this->CreatorID )
{
return eZUser::fetch( $this->CreatorID );
}
return null;
}
/*!
\return The modifier of the class as an eZUser object by using the $ModifierID as user ID.
*/
function modifier()
{
if ( isset( $this->ModifierID ) and $this->ModifierID )
{
return eZUser::fetch( $this->ModifierID );
}
return null;
}
/*!
Find all groups the current class is placed in and returns a list of group objects.
\return An array with eZContentClassGroup objects.
\sa fetchGroupIDList()
*/
function fetchGroupList()
{
$this->InGroups = eZContentClassClassGroup::fetchGroupList( $this->attribute( "id" ),
$this->attribute( "version" ),
true );
return $this->InGroups;
}
/*!
Find all groups the current class is placed in and returns a list of group IDs.
\return An array with integers (ids).
\sa fetchGroupList()
*/
function fetchGroupIDList()
{
$list = eZContentClassClassGroup::fetchGroupList( $this->attribute( "id" ),
$this->attribute( "version" ),
false );
$this->InGroupIDs = array();
foreach ( $list as $item )
{
$this->InGroupIDs[] = $item['group_id'];
}
return $this->InGroupIDs;
}
/*!
Returns the result from fetchGroupIDList() if class group overrides is
enabled in content.ini.
\return An array with eZContentClassGroup objects or \c false if disabled.
\note \c EnableClassGroupOverride in group \c ContentOverrideSettings from INI file content.ini
controls this behaviour.
*/
function fetchMatchGroupIDList()
{
$contentINI = eZINI::instance( 'content.ini' );
if( $contentINI->variable( 'ContentOverrideSettings', 'EnableClassGroupOverride' ) == 'true' )
{
return $this->attribute( 'ingroup_id_list' );
}
return false;
}
/*!
Finds all Classes in the system and returns them.
\return An array with eZContentClass objects.
*/
static function fetchAllClasses( $asObject = true, $includeFilter = true, $groupList = false )
{
$filterTableSQL = '';
$filterSQL = '';
if ( is_array( $groupList ) )
{
$filterTableSQL = ', ezcontentclass_classgroup ccg';
$filterSQL = ( " AND" .
" cc.id = ccg.contentclass_id AND" .
" ccg.group_id " );
$groupText = implode( ', ', $groupList );
if ( $includeFilter )
$filterSQL .= "IN ( $groupText )";
else
$filterSQL .= "NOT IN ( $groupText )";
}
$classNameFilter = eZContentClassName::sqlFilter( 'cc' );
$classList = array();
$db = eZDB::instance();
// If $asObject is true we fetch all fields in class
$fields = $asObject ? "cc.*" : "cc.id, $classNameFilter[nameField]";
$rows = $db->arrayQuery( "SELECT DISTINCT $fields " .
"FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from] " .
"WHERE cc.version = " . eZContentClass::VERSION_STATUS_DEFINED . "$filterSQL AND $classNameFilter[where]" .
"ORDER BY $classNameFilter[nameField] ASC" );
$classList = eZPersistentObject::handleRows( $rows, 'eZContentClass', $asObject );
return $classList;
}
/*!
Finds all Class groups in the system and returns them.
\return An array with eZContentClassGroup objects.
\sa fetchGroupList(), fetchGroupIDList()
*/
function fetchAllGroups()
{
$this->AllGroups = eZContentClassGroup::fetchList();
return $this->AllGroups;
}
/*!
\return true if the class is part of the group \a $groupID
*/
function inGroup( $groupID )
{
return eZContentClassClassGroup::classInGroup( $this->attribute( 'id' ),
$this->attribute( 'version' ),
$groupID );
}
/*!
\static
Will remove all temporary classes from the database.
*/
static function removeTemporary()
{
$version = eZContentClass::VERSION_STATUS_TEMPORARY;
$temporaryClasses = eZContentClass::fetchList( $version, true );
$db = eZDB::instance();
$db->begin();
foreach ( $temporaryClasses as $class )
{
$class->remove( true, $version );
}
eZPersistentObject::removeObject( eZContentClassAttribute::definition(),
array( 'version' => $version ) );
$db->commit();
}
/*!
Get remote id of content node
*/
function remoteID()
{
$remoteID = $this->attribute( 'remote_id', true );
if ( !$remoteID &&
$this->Version == eZContentClass::VERSION_STATUS_DEFINED )
{
$this->setAttribute( 'remote_id', eZRemoteIdUtility::generate( 'class' ) );
$this->sync( array( 'remote_id' ) );
$remoteID = $this->attribute( 'remote_id', true );
}
return $remoteID;
}
/*!
\note If you want to remove a class with all data associated with it (objects/classMembers)
you should use eZContentClassOperations::remove()
*/
function remove( $removeAttributes = false, $version = eZContentClass::VERSION_STATUS_DEFINED )
{
// If we are not allowed to remove just return false
if ( $this->Version == eZContentClass::VERSION_STATUS_DEFINED && !$this->isRemovable() )
return false;
if ( is_array( $removeAttributes ) or $removeAttributes )
$this->removeAttributes( $removeAttributes );
$this->NameList->remove( $this );
parent::remove();
}
/*!
Checks if the class can be removed and returns \c true if it can, \c false otherwise.
\sa removableInformation()
*/
function isRemovable()
{
$info = $this->removableInformation( false );
return count( $info['list'] ) == 0;
}
/*!
Returns information on why the class cannot be removed,
it does the same checks as in isRemovable() but generates
some text in the return array.
\return An array which contains:
- text - Plain text description why this cannot be removed
- list - An array with reasons why this failed, each entry contains:
- text - Plain text description of the reason.
- list - A sublist of reason (e.g from an attribute), is optional.
\param $includeAll Controls whether the returned information will contain all
sources for not being to remove or just the first that it finds.
*/
function removableInformation( $includeAll = true )
{
$result = array( 'text' => ezpI18n::tr( 'kernel/contentclass', "Cannot remove class '%class_name':",
null, array( '%class_name' => $this->attribute( 'name' ) ) ),
'list' => array() );
$db = eZDB::instance();
// Check top-level nodes
$rows = $db->arrayQuery( "SELECT ezcot.node_id
FROM ezcontentobject_tree ezcot, ezcontentobject ezco
WHERE ezcot.depth = 1 AND
ezco.contentclass_id = $this->ID AND
ezco.id=ezcot.contentobject_id" );
if ( count( $rows ) > 0 )
{
$result['list'][] = array( 'text' => ezpI18n::tr( 'kernel/contentclass', 'The class is used by a top-level node and cannot be removed.
You will need to change the class of the node by using the swap functionality.' ) );
if ( !$includeAll )
return $result;
}
// Check class attributes
foreach ( $this->fetchAttributes() as $attribute )
{
$dataType = $attribute->dataType();
if ( $dataType === null )
{
eZDebug::writeError(
"Skipping removal of class attribute for non-existent datatype " . $attribute->DataTypeString,
__METHOD__
);
continue;
}
if ( $dataType->isClassAttributeRemovable( $attribute ) )
{
$info = $dataType->classAttributeRemovableInformation( $attribute, $includeAll );
$result['list'][] = $info;
if ( !$includeAll )
return $result;
}
}
return $result;
}
/*!
\note Removes class attributes
\param removeAttributes Array of attributes to remove
\param version Version to remove( optional )
*/
function removeAttributes( $removeAttributes = false, $version = false )
{
if ( is_array( $removeAttributes ) )
{
$db = eZDB::instance();
$db->begin();
foreach( $removeAttributes as $attribute )
{
$attribute->removeThis();
}
$db->commit();
}
else
{
$contentClassID = $this->ID;
if ( $version === false )
{
$version = $this->Version;
}
$classAttributes = $this->fetchAttributes( );
$db = eZDB::instance();
$db->begin();
foreach ( $classAttributes as $classAttribute )
{
$dataType = $classAttribute->dataType();
if ( $dataType === null )
{
eZDebug::writeError(
"Skipping removal of class attribute for non-existent datatype " . $classAttribute->DataTypeString,
__METHOD__
);
continue;
}
$dataType->deleteStoredClassAttribute( $classAttribute, $version );
}
eZPersistentObject::removeObject( eZContentClassAttribute::definition(),
array( 'contentclass_id' => $contentClassID,
'version' => $version ) );
$db->commit();
}
}
function compareAttributes( $attr1, $attr2 )
{
return ( $attr1->attribute( "placement" ) > $attr2->attribute( "placement" ) ) ? 1 : -1;
}
function adjustAttributePlacements( $attributes )
{
if ( !is_array( $attributes ) )
return;
usort( $attributes, array( $this, "compareAttributes" ) );
$i = 0;
foreach( $attributes as $attribute )
{
$attribute->setAttribute( "placement", ++$i );
}
}
function store( $store_childs = false, $fieldFilters = null )
{
self::expireCache();
$db = eZDB::instance();
$db->begin();
if ( is_array( $store_childs ) ||
$store_childs )
{
if ( is_array( $store_childs ) )
{
$attributes = $store_childs;
}
else
{
$attributes = $this->fetchAttributes();
}
foreach( $attributes as $attribute )
{
if ( is_object ( $attribute ) )
$attribute->store();
}
}
$handler = eZExpiryHandler::instance();
$handler->setTimestamp( 'user-class-cache', time() );
$handler->store();
$this->setAttribute( 'serialized_name_list', $this->NameList->serializeNames() );
$this->setAttribute( 'serialized_description_list', $this->DescriptionList->serializeNames() );
parent::store( $fieldFilters );
$this->NameList->store( $this );
$db->commit();
}
function sync( $fieldFilters = null )
{
if ( $this->hasDirtyData() )
$this->store( false, $fieldFilters );
}
/*!
Initializes this class as a copy of \a $originalClass by
creating new a new name and identifier.
It will check if there are other classes already with this name
in which case it will append a unique number to the name and identifier.
*/
function initializeCopy( &$originalClass )
{
$name = ezpI18n::tr( 'kernel/class', 'Copy of %class_name', null,
array( '%class_name' => $originalClass->attribute( 'name' ) ) );
$identifier = 'copy_of_' . $originalClass->attribute( 'identifier' );
$db = eZDB::instance();
$sql = "SELECT count( ezcontentclass_name.name ) AS count FROM ezcontentclass, ezcontentclass_name WHERE ezcontentclass.id = ezcontentclass_name.contentclass_id AND ezcontentclass_name.name like '" . $db->escapeString( $name ) . "%'";
$rows = $db->arrayQuery( $sql );
$count = $rows[0]['count'];
if ( $count > 0 )
{
++$count;
$name .= $count;
$identifier .= $count;
if ( strlen( $identifier ) > 50 )
{
$identifier = "copy_{$count}_of_" . $originalClass->attribute( 'identifier' );
}
}
$this->setName( $name );
$this->setAttribute( 'identifier', $identifier );
$this->setAttribute( 'created', time() );
$user = eZUser::currentUser();
$userID = $user->attribute( "contentobject_id" );
$this->setAttribute( 'creator_id', $userID );
}
/**
* Stores the current class as a defined version, updates the contentobject_name
* attribute and recreates the class group entries.
*
* @note It will remove any existing temporary or defined classes before storing.
*
* @param array $attributes array of attributes of the content class
*/
public function storeDefined( $attributes )
{
$this->storeVersioned( $attributes, self::VERSION_STATUS_DEFINED );
}
/**
* Stores the current class as a modified version, updates the contentobject_name
* attribute and recreates the class group entries.
*