Completed
Push — lazy-loading-not-avail-content ( 189e64...1dbb7d )
by André
17:36
created

DoctrineDatabase::generateLanguageMask()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the DoctrineDatabase ObjectState Gateway class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Gateway;
10
11
use eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Gateway;
12
use eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator;
13
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
14
use eZ\Publish\SPI\Persistence\Content\ObjectState;
15
use eZ\Publish\SPI\Persistence\Content\ObjectState\Group;
16
17
/**
18
 * ObjectState Doctrine database Gateway.
19
 */
20
class DoctrineDatabase extends Gateway
21
{
22
    /**
23
     * Database handler.
24
     *
25
     * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
26
     */
27
    protected $dbHandler;
28
29
    /**
30
     * Language mask generator.
31
     *
32
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator
33
     */
34
    protected $maskGenerator;
35
36
    /**
37
     * Creates a new Doctrine database ObjectState Gateway.
38
     *
39
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler
40
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator $maskGenerator
41
     */
42
    public function __construct(DatabaseHandler $dbHandler, MaskGenerator $maskGenerator)
43
    {
44
        $this->dbHandler = $dbHandler;
45
        $this->maskGenerator = $maskGenerator;
46
    }
47
48
    /**
49
     * Loads data for an object state.
50
     *
51
     * @param mixed $stateId
52
     *
53
     * @return array
54
     */
55 View Code Duplication
    public function loadObjectStateData($stateId)
56
    {
57
        $query = $this->createObjectStateFindQuery();
58
        $query->where(
59
            $query->expr->eq(
60
                $this->dbHandler->quoteColumn('id', 'ezcobj_state'),
61
                $query->bindValue($stateId, null, \PDO::PARAM_INT)
62
            )
63
        );
64
65
        $statement = $query->prepare();
66
        $statement->execute();
67
68
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
69
    }
70
71
    /**
72
     * Loads data for an object state by identifier.
73
     *
74
     * @param string $identifier
75
     * @param mixed $groupId
76
     *
77
     * @return array
78
     */
79 View Code Duplication
    public function loadObjectStateDataByIdentifier($identifier, $groupId)
80
    {
81
        $query = $this->createObjectStateFindQuery();
82
        $query->where(
83
            $query->expr->lAnd(
84
                $query->expr->eq(
85
                    $this->dbHandler->quoteColumn('identifier', 'ezcobj_state'),
86
                    $query->bindValue($identifier, null, \PDO::PARAM_STR)
87
                ),
88
                $query->expr->eq(
89
                    $this->dbHandler->quoteColumn('group_id', 'ezcobj_state'),
90
                    $query->bindValue($groupId, null, \PDO::PARAM_INT)
91
                )
92
            )
93
        );
94
95
        $statement = $query->prepare();
96
        $statement->execute();
97
98
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
99
    }
100
101
    /**
102
     * Loads data for all object states belonging to group with $groupId ID.
103
     *
104
     * @param mixed $groupId
105
     *
106
     * @return array
107
     */
108
    public function loadObjectStateListData($groupId)
109
    {
110
        $query = $this->createObjectStateFindQuery();
111
        $query->where(
112
            $query->expr->eq(
113
                $this->dbHandler->quoteColumn('group_id', 'ezcobj_state'),
114
                $query->bindValue($groupId, null, \PDO::PARAM_INT)
115
            )
116
        )->orderBy($this->dbHandler->quoteColumn('priority', 'ezcobj_state'), $query::ASC);
117
118
        $statement = $query->prepare();
119
        $statement->execute();
120
121
        $rows = array();
122
        while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
123
            $rows[$row['ezcobj_state_id']][] = $row;
124
        }
125
126
        return array_values($rows);
127
    }
128
129
    /**
130
     * Loads data for an object state group.
131
     *
132
     * @param mixed $groupId
133
     *
134
     * @return array
135
     */
136 View Code Duplication
    public function loadObjectStateGroupData($groupId)
137
    {
138
        $query = $this->createObjectStateGroupFindQuery();
139
        $query->where(
140
            $query->expr->eq(
141
                $this->dbHandler->quoteColumn('id', 'ezcobj_state_group'),
142
                $query->bindValue($groupId, null, \PDO::PARAM_INT)
143
            )
144
        );
145
146
        $statement = $query->prepare();
147
        $statement->execute();
148
149
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
150
    }
151
152
    /**
153
     * Loads data for an object state group by identifier.
154
     *
155
     * @param string $identifier
156
     *
157
     * @return array
158
     */
159 View Code Duplication
    public function loadObjectStateGroupDataByIdentifier($identifier)
160
    {
161
        $query = $this->createObjectStateGroupFindQuery();
162
        $query->where(
163
            $query->expr->eq(
164
                $this->dbHandler->quoteColumn('identifier', 'ezcobj_state_group'),
165
                $query->bindValue($identifier, null, \PDO::PARAM_STR)
166
            )
167
        );
168
169
        $statement = $query->prepare();
170
        $statement->execute();
171
172
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
173
    }
174
175
    /**
176
     * Loads data for all object state groups, filtered by $offset and $limit.
177
     *
178
     * @param int $offset
179
     * @param int $limit
180
     *
181
     * @return array
182
     */
183
    public function loadObjectStateGroupListData($offset, $limit)
184
    {
185
        $query = $this->createObjectStateGroupFindQuery();
186
        $query->limit($limit > 0 ? $limit : PHP_INT_MAX, $offset);
187
188
        $statement = $query->prepare();
189
        $statement->execute();
190
191
        $rows = array();
192
        while ($row = $statement->fetch(\PDO::FETCH_ASSOC)) {
193
            $rows[$row['ezcobj_state_group_id']][] = $row;
194
        }
195
196
        return array_values($rows);
197
    }
198
199
    /**
200
     * Inserts a new object state into database.
201
     *
202
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $objectState
203
     * @param int $groupId
204
     */
205
    public function insertObjectState(ObjectState $objectState, $groupId)
206
    {
207
        $query = $this->dbHandler->createSelectQuery();
208
        $query->select(
209
            $query->expr->max($this->dbHandler->quoteColumn('priority'))
210
        )->from(
211
            $this->dbHandler->quoteTable('ezcobj_state')
212
        )->where(
213
            $query->expr->eq(
214
                $this->dbHandler->quoteColumn('group_id'),
215
                $query->bindValue($groupId, null, \PDO::PARAM_INT)
216
            )
217
        );
218
219
        $statement = $query->prepare();
220
        $statement->execute();
221
222
        $maxPriority = $statement->fetchColumn();
223
224
        $objectState->priority = $maxPriority === null ? 0 : (int)$maxPriority + 1;
225
        $objectState->groupId = (int)$groupId;
226
227
        $query = $this->dbHandler->createInsertQuery();
228
        $query->insertInto(
229
            $this->dbHandler->quoteTable('ezcobj_state')
230
        )->set(
231
            $this->dbHandler->quoteColumn('id'),
232
            $this->dbHandler->getAutoIncrementValue('ezcobj_state', 'id')
233
        )->set(
234
            $this->dbHandler->quoteColumn('group_id'),
235
            $query->bindValue($objectState->groupId, null, \PDO::PARAM_INT)
236
        )->set(
237
            $this->dbHandler->quoteColumn('default_language_id'),
238
            $query->bindValue(
239
                $this->maskGenerator->generateLanguageIndicator($objectState->defaultLanguage, false),
240
                null,
241
                \PDO::PARAM_INT
242
            )
243
        )->set(
244
            $this->dbHandler->quoteColumn('identifier'),
245
            $query->bindValue($objectState->identifier)
246
        )->set(
247
            $this->dbHandler->quoteColumn('language_mask'),
248
            $query->bindValue(
249
                $this->maskGenerator->generateLanguageMaskFromLanguageCodes($objectState->languageCodes, true),
250
                null,
251
                \PDO::PARAM_INT
252
            )
253
        )->set(
254
            $this->dbHandler->quoteColumn('priority'),
255
            $query->bindValue($objectState->priority, null, \PDO::PARAM_INT)
256
        );
257
258
        $query->prepare()->execute();
259
260
        $objectState->id = (int)$this->dbHandler->lastInsertId(
261
            $this->dbHandler->getSequenceName('ezcobj_state', 'id')
262
        );
263
264
        $this->insertObjectStateTranslations($objectState);
265
266
        // If this is a first state in group, assign it to all content objects
267
        if ($maxPriority === null) {
268
            // @todo Hm... How do we perform this with query object?
269
            $this->dbHandler->prepare(
270
                "INSERT INTO ezcobj_state_link (contentobject_id, contentobject_state_id)
271
                SELECT id, {$objectState->id} FROM ezcontentobject"
272
            )->execute();
273
        }
274
    }
275
276
    /**
277
     * Updates the stored object state with provided data.
278
     *
279
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $objectState
280
     */
281 View Code Duplication
    public function updateObjectState(ObjectState $objectState)
282
    {
283
        // First update the state
284
        $query = $this->dbHandler->createUpdateQuery();
285
        $query->update(
286
            $this->dbHandler->quoteTable('ezcobj_state')
287
        )->set(
288
            $this->dbHandler->quoteColumn('default_language_id'),
289
            $query->bindValue(
290
                $this->maskGenerator->generateLanguageIndicator($objectState->defaultLanguage, false),
291
                null,
292
                \PDO::PARAM_INT
293
            )
294
        )->set(
295
            $this->dbHandler->quoteColumn('identifier'),
296
            $query->bindValue($objectState->identifier)
297
        )->set(
298
            $this->dbHandler->quoteColumn('language_mask'),
299
            $query->bindValue(
300
                $this->maskGenerator->generateLanguageMaskFromLanguageCodes($objectState->languageCodes, true),
301
                null,
302
                \PDO::PARAM_INT
303
            )
304
        )->where(
305
            $query->expr->eq(
306
                $this->dbHandler->quoteColumn('id'),
307
                $query->bindValue($objectState->id, null, \PDO::PARAM_INT)
308
            )
309
        );
310
311
        $query->prepare()->execute();
312
313
        // And then refresh object state translations
314
        // by removing existing ones and adding new ones
315
        $this->deleteObjectStateTranslations($objectState->id);
316
        $this->insertObjectStateTranslations($objectState);
317
    }
318
319
    /**
320
     * Deletes object state identified by $stateId.
321
     *
322
     * @param int $stateId
323
     */
324
    public function deleteObjectState($stateId)
325
    {
326
        $this->deleteObjectStateTranslations($stateId);
327
328
        $query = $this->dbHandler->createDeleteQuery();
329
        $query->deleteFrom(
330
            $this->dbHandler->quoteTable('ezcobj_state')
331
        )->where(
332
            $query->expr->eq(
333
                $this->dbHandler->quoteColumn('id'),
334
                $query->bindValue($stateId, null, \PDO::PARAM_INT)
335
            )
336
        );
337
338
        $query->prepare()->execute();
339
    }
340
341
    /**
342
     * Update object state links to $newStateId.
343
     *
344
     * @param int $oldStateId
345
     * @param int $newStateId
346
     */
347 View Code Duplication
    public function updateObjectStateLinks($oldStateId, $newStateId)
348
    {
349
        $query = $this->dbHandler->createUpdateQuery();
350
        $query->update(
351
            $this->dbHandler->quoteTable('ezcobj_state_link')
352
        )->set(
353
            $this->dbHandler->quoteColumn('contentobject_state_id'),
354
            $query->bindValue($newStateId, null, \PDO::PARAM_INT)
355
        )->where(
356
            $query->expr->eq(
357
                $this->dbHandler->quoteColumn('contentobject_state_id'),
358
                $query->bindValue($oldStateId, null, \PDO::PARAM_INT)
359
            )
360
        );
361
362
        $query->prepare()->execute();
363
    }
364
365
    /**
366
     * Deletes object state links identified by $stateId.
367
     *
368
     * @param int $stateId
369
     */
370
    public function deleteObjectStateLinks($stateId)
371
    {
372
        $query = $this->dbHandler->createDeleteQuery();
373
        $query->deleteFrom(
374
            $this->dbHandler->quoteTable('ezcobj_state_link')
375
        )->where(
376
            $query->expr->eq(
377
                $this->dbHandler->quoteColumn('contentobject_state_id'),
378
                $query->bindValue($stateId, null, \PDO::PARAM_INT)
379
            )
380
        );
381
382
        $query->prepare()->execute();
383
    }
384
385
    /**
386
     * Inserts a new object state group into database.
387
     *
388
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $objectStateGroup
389
     */
390
    public function insertObjectStateGroup(Group $objectStateGroup)
391
    {
392
        $query = $this->dbHandler->createInsertQuery();
393
        $query->insertInto(
394
            $this->dbHandler->quoteTable('ezcobj_state_group')
395
        )->set(
396
            $this->dbHandler->quoteColumn('id'),
397
            $this->dbHandler->getAutoIncrementValue('ezcobj_state_group', 'id')
398
        )->set(
399
            $this->dbHandler->quoteColumn('default_language_id'),
400
            $query->bindValue(
401
                $this->maskGenerator->generateLanguageIndicator($objectStateGroup->defaultLanguage, false),
402
                null,
403
                \PDO::PARAM_INT
404
            )
405
        )->set(
406
            $this->dbHandler->quoteColumn('identifier'),
407
            $query->bindValue($objectStateGroup->identifier)
408
        )->set(
409
            $this->dbHandler->quoteColumn('language_mask'),
410
            $query->bindValue(
411
                $this->maskGenerator->generateLanguageMaskFromLanguageCodes($objectStateGroup->languageCodes, true),
412
                null,
413
                \PDO::PARAM_INT
414
            )
415
        );
416
417
        $query->prepare()->execute();
418
419
        $objectStateGroup->id = (int)$this->dbHandler->lastInsertId(
420
            $this->dbHandler->getSequenceName('ezcobj_state_group', 'id')
421
        );
422
423
        $this->insertObjectStateGroupTranslations($objectStateGroup);
424
    }
425
426
    /**
427
     * Updates the stored object state group with provided data.
428
     *
429
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $objectStateGroup
430
     */
431 View Code Duplication
    public function updateObjectStateGroup(Group $objectStateGroup)
432
    {
433
        // First update the group
434
        $query = $this->dbHandler->createUpdateQuery();
435
        $query->update(
436
            $this->dbHandler->quoteTable('ezcobj_state_group')
437
        )->set(
438
            $this->dbHandler->quoteColumn('default_language_id'),
439
            $query->bindValue(
440
                $this->maskGenerator->generateLanguageIndicator($objectStateGroup->defaultLanguage, false),
441
                null,
442
                \PDO::PARAM_INT
443
            )
444
        )->set(
445
            $this->dbHandler->quoteColumn('identifier'),
446
            $query->bindValue($objectStateGroup->identifier)
447
        )->set(
448
            $this->dbHandler->quoteColumn('language_mask'),
449
            $query->bindValue(
450
                $this->maskGenerator->generateLanguageMaskFromLanguageCodes($objectStateGroup->languageCodes, true),
451
                null,
452
                \PDO::PARAM_INT
453
            )
454
        )->where(
455
            $query->expr->eq(
456
                $this->dbHandler->quoteColumn('id'),
457
                $query->bindValue($objectStateGroup->id, null, \PDO::PARAM_INT)
458
            )
459
        );
460
461
        $query->prepare()->execute();
462
463
        // And then refresh group translations
464
        // by removing old ones and adding new ones
465
        $this->deleteObjectStateGroupTranslations($objectStateGroup->id);
466
        $this->insertObjectStateGroupTranslations($objectStateGroup);
467
    }
468
469
    /**
470
     * Deletes the object state group identified by $groupId.
471
     *
472
     * @param mixed $groupId
473
     */
474
    public function deleteObjectStateGroup($groupId)
475
    {
476
        $this->deleteObjectStateGroupTranslations($groupId);
477
478
        $query = $this->dbHandler->createDeleteQuery();
479
        $query->deleteFrom(
480
            $this->dbHandler->quoteTable('ezcobj_state_group')
481
        )->where(
482
            $query->expr->eq(
483
                $this->dbHandler->quoteColumn('id'),
484
                $query->bindValue($groupId, null, \PDO::PARAM_INT)
485
            )
486
        );
487
488
        $query->prepare()->execute();
489
    }
490
491
    /**
492
     * Sets the object state $stateId to content with $contentId ID.
493
     *
494
     * @param mixed $contentId
495
     * @param mixed $groupId
496
     * @param mixed $stateId
497
     */
498
    public function setContentState($contentId, $groupId, $stateId)
499
    {
500
        // First find out if $contentId is related to existing states in $groupId
501
        $query = $this->dbHandler->createSelectQuery();
502
        $query->select(
503
            $this->dbHandler->aliasedColumn($query, 'id', 'ezcobj_state')
504
        )->from(
505
            $this->dbHandler->quoteTable('ezcobj_state')
506
        )->innerJoin(
507
            $this->dbHandler->quoteTable('ezcobj_state_link'),
508
            $query->expr->eq(
509
                $this->dbHandler->quoteColumn('id', 'ezcobj_state'),
510
                $this->dbHandler->quoteColumn('contentobject_state_id', 'ezcobj_state_link')
511
            )
512
        )->where(
513
            $query->expr->lAnd(
514
                $query->expr->eq(
515
                    $this->dbHandler->quoteColumn('group_id', 'ezcobj_state'),
516
                    $query->bindValue($groupId, null, \PDO::PARAM_INT)
517
                ),
518
                $query->expr->eq(
519
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcobj_state_link'),
520
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
521
                )
522
            )
523
        );
524
525
        $statement = $query->prepare();
526
        $statement->execute();
527
528
        $rows = $statement->fetchAll(\PDO::FETCH_ASSOC);
529
530
        if (!empty($rows)) {
531
            // We already have a state assigned to $contentId, update to new one
532
            $query = $this->dbHandler->createUpdateQuery();
533
            $query->update(
534
                $this->dbHandler->quoteTable('ezcobj_state_link')
535
            )->set(
536
                $this->dbHandler->quoteColumn('contentobject_state_id'),
537
                $query->bindValue($stateId, null, \PDO::PARAM_INT)
538
            )->where(
539
                $query->expr->lAnd(
540
                    $query->expr->eq(
541
                        $this->dbHandler->quoteColumn('contentobject_id'),
542
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
543
                    ),
544
                    $query->expr->eq(
545
                        $this->dbHandler->quoteColumn('contentobject_state_id'),
546
                        $query->bindValue($rows[0]['ezcobj_state_id'], null, \PDO::PARAM_INT)
547
                    )
548
                )
549
            );
550
551
            $query->prepare()->execute();
552
        } else {
553
            // No state assigned to $contentId from specified group, create assignment
554
            $query = $this->dbHandler->createInsertQuery();
555
            $query->insertInto(
556
                $this->dbHandler->quoteTable('ezcobj_state_link')
557
            )->set(
558
                $this->dbHandler->quoteColumn('contentobject_id'),
559
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
560
            )->set(
561
                $this->dbHandler->quoteColumn('contentobject_state_id'),
562
                $query->bindValue($stateId, null, \PDO::PARAM_INT)
563
            );
564
565
            $query->prepare()->execute();
566
        }
567
    }
568
569
    /**
570
     * Loads object state data for $contentId content from $stateGroupId state group.
571
     *
572
     * @param int $contentId
573
     * @param int $stateGroupId
574
     *
575
     * @return array
576
     */
577 View Code Duplication
    public function loadObjectStateDataForContent($contentId, $stateGroupId)
578
    {
579
        $query = $this->createObjectStateFindQuery();
580
        $query->innerJoin(
581
            $this->dbHandler->quoteTable('ezcobj_state_link'),
582
            $query->expr->eq(
583
                $this->dbHandler->quoteColumn('id', 'ezcobj_state'),
584
                $this->dbHandler->quoteColumn('contentobject_state_id', 'ezcobj_state_link')
585
            )
586
        )->where(
587
            $query->expr->lAnd(
588
                $query->expr->eq(
589
                    $this->dbHandler->quoteColumn('group_id', 'ezcobj_state'),
590
                    $query->bindValue($stateGroupId, null, \PDO::PARAM_INT)
591
                ),
592
                $query->expr->eq(
593
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcobj_state_link'),
594
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
595
                )
596
            )
597
        );
598
599
        $statement = $query->prepare();
600
        $statement->execute();
601
602
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
603
    }
604
605
    /**
606
     * Returns the number of objects which are in this state.
607
     *
608
     * @param mixed $stateId
609
     *
610
     * @return int
611
     */
612
    public function getContentCount($stateId)
613
    {
614
        $query = $this->dbHandler->createSelectQuery();
615
        $query->select(
616
            $query->alias($query->expr->count('*'), 'count')
617
        )->from(
618
            $this->dbHandler->quoteTable('ezcobj_state_link')
619
        )->where(
620
            $query->expr->eq(
621
                $this->dbHandler->quoteColumn('contentobject_state_id'),
622
                $query->bindValue($stateId, null, \PDO::PARAM_INT)
623
            )
624
        );
625
626
        $statement = $query->prepare();
627
        $statement->execute();
628
629
        $count = $statement->fetchColumn();
630
631
        return $count !== null ? (int)$count : 0;
632
    }
633
634
    /**
635
     * Updates the object state priority to provided value.
636
     *
637
     * @param mixed $stateId
638
     * @param int $priority
639
     */
640 View Code Duplication
    public function updateObjectStatePriority($stateId, $priority)
641
    {
642
        $query = $this->dbHandler->createUpdateQuery();
643
        $query->update(
644
            $this->dbHandler->quoteTable('ezcobj_state')
645
        )->set(
646
            $this->dbHandler->quoteColumn('priority'),
647
            $query->bindValue($priority, null, \PDO::PARAM_INT)
648
        )->where(
649
            $query->expr->eq(
650
                $this->dbHandler->quoteColumn('id'),
651
                $query->bindValue($stateId, null, \PDO::PARAM_INT)
652
            )
653
        );
654
655
        $query->prepare()->execute();
656
    }
657
658
    /**
659
     * Creates a generalized query for fetching object state(s).
660
     *
661
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
662
     */
663 View Code Duplication
    protected function createObjectStateFindQuery()
664
    {
665
        $query = $this->dbHandler->createSelectQuery();
666
        $query->select(
667
            // Object state
668
            $this->dbHandler->aliasedColumn($query, 'default_language_id', 'ezcobj_state'),
669
            $this->dbHandler->aliasedColumn($query, 'group_id', 'ezcobj_state'),
670
            $this->dbHandler->aliasedColumn($query, 'id', 'ezcobj_state'),
671
            $this->dbHandler->aliasedColumn($query, 'identifier', 'ezcobj_state'),
672
            $this->dbHandler->aliasedColumn($query, 'language_mask', 'ezcobj_state'),
673
            $this->dbHandler->aliasedColumn($query, 'priority', 'ezcobj_state'),
674
            // Object state language
675
            $this->dbHandler->aliasedColumn($query, 'description', 'ezcobj_state_language'),
676
            $this->dbHandler->aliasedColumn($query, 'language_id', 'ezcobj_state_language'),
677
            $this->dbHandler->aliasedColumn($query, 'name', 'ezcobj_state_language')
678
        )->from(
679
            $this->dbHandler->quoteTable('ezcobj_state')
680
        )->innerJoin(
681
            $this->dbHandler->quoteTable('ezcobj_state_language'),
682
            $query->expr->eq(
683
                $this->dbHandler->quoteColumn('id', 'ezcobj_state'),
684
                $this->dbHandler->quoteColumn('contentobject_state_id', 'ezcobj_state_language')
685
            )
686
        );
687
688
        return $query;
689
    }
690
691
    /**
692
     * Creates a generalized query for fetching object state group(s).
693
     *
694
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
695
     */
696 View Code Duplication
    protected function createObjectStateGroupFindQuery()
697
    {
698
        $query = $this->dbHandler->createSelectQuery();
699
        $query->select(
700
            // Object state group
701
            $this->dbHandler->aliasedColumn($query, 'default_language_id', 'ezcobj_state_group'),
702
            $this->dbHandler->aliasedColumn($query, 'id', 'ezcobj_state_group'),
703
            $this->dbHandler->aliasedColumn($query, 'identifier', 'ezcobj_state_group'),
704
            $this->dbHandler->aliasedColumn($query, 'language_mask', 'ezcobj_state_group'),
705
            // Object state group language
706
            $this->dbHandler->aliasedColumn($query, 'description', 'ezcobj_state_group_language'),
707
            $this->dbHandler->aliasedColumn($query, 'language_id', 'ezcobj_state_group_language'),
708
            $this->dbHandler->aliasedColumn($query, 'real_language_id', 'ezcobj_state_group_language'),
709
            $this->dbHandler->aliasedColumn($query, 'name', 'ezcobj_state_group_language')
710
        )->from(
711
            $this->dbHandler->quoteTable('ezcobj_state_group')
712
        )->innerJoin(
713
            $this->dbHandler->quoteTable('ezcobj_state_group_language'),
714
            $query->expr->eq(
715
                $this->dbHandler->quoteColumn('id', 'ezcobj_state_group'),
716
                $this->dbHandler->quoteColumn('contentobject_state_group_id', 'ezcobj_state_group_language')
717
            )
718
        );
719
720
        return $query;
721
    }
722
723
    /**
724
     * Inserts object state group translations into database.
725
     *
726
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $objectState
727
     */
728
    protected function insertObjectStateTranslations(ObjectState $objectState)
729
    {
730
        foreach ($objectState->languageCodes as $languageCode) {
731
            $query = $this->dbHandler->createInsertQuery();
732
            $query->insertInto(
733
                $this->dbHandler->quoteTable('ezcobj_state_language')
734
            )->set(
735
                $this->dbHandler->quoteColumn('contentobject_state_id'),
736
                $query->bindValue($objectState->id, null, \PDO::PARAM_INT)
737
            )->set(
738
                $this->dbHandler->quoteColumn('description'),
739
                $query->bindValue($objectState->description[$languageCode])
740
            )->set(
741
                $this->dbHandler->quoteColumn('name'),
742
                $query->bindValue($objectState->name[$languageCode])
743
            )->set(
744
                $this->dbHandler->quoteColumn('language_id'),
745
                $query->bindValue(
746
                    $this->maskGenerator->generateLanguageIndicator(
747
                        $languageCode,
748
                        $languageCode === $objectState->defaultLanguage
749
                    ),
750
                    null,
751
                    \PDO::PARAM_INT
752
                )
753
            );
754
755
            $query->prepare()->execute();
756
        }
757
    }
758
759
    /**
760
     * Deletes all translations of the $stateId state.
761
     *
762
     * @param mixed $stateId
763
     */
764
    protected function deleteObjectStateTranslations($stateId)
765
    {
766
        $query = $this->dbHandler->createDeleteQuery();
767
        $query->deleteFrom(
768
            $this->dbHandler->quoteTable('ezcobj_state_language')
769
        )->where(
770
            $query->expr->eq(
771
                $this->dbHandler->quoteColumn('contentobject_state_id'),
772
                $query->bindValue($stateId, null, \PDO::PARAM_INT)
773
            )
774
        );
775
776
        $query->prepare()->execute();
777
    }
778
779
    /**
780
     * Inserts object state group translations into database.
781
     *
782
     * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $objectStateGroup
783
     */
784
    protected function insertObjectStateGroupTranslations(Group $objectStateGroup)
785
    {
786
        foreach ($objectStateGroup->languageCodes as $languageCode) {
787
            $languageId = $this->maskGenerator->generateLanguageIndicator(
788
                $languageCode,
789
                $languageCode === $objectStateGroup->defaultLanguage
790
            );
791
792
            $query = $this->dbHandler->createInsertQuery();
793
            $query->insertInto(
794
                $this->dbHandler->quoteTable('ezcobj_state_group_language')
795
            )->set(
796
                $this->dbHandler->quoteColumn('contentobject_state_group_id'),
797
                $query->bindValue($objectStateGroup->id, null, \PDO::PARAM_INT)
798
            )->set(
799
                $this->dbHandler->quoteColumn('description'),
800
                $query->bindValue($objectStateGroup->description[$languageCode])
801
            )->set(
802
                $this->dbHandler->quoteColumn('name'),
803
                $query->bindValue($objectStateGroup->name[$languageCode])
804
            )->set(
805
                $this->dbHandler->quoteColumn('language_id'),
806
                $query->bindValue($languageId, null, \PDO::PARAM_INT)
807
            )->set(
808
                $this->dbHandler->quoteColumn('real_language_id'),
809
                $query->bindValue($languageId & ~1, null, \PDO::PARAM_INT)
810
            );
811
812
            $query->prepare()->execute();
813
        }
814
    }
815
816
    /**
817
     * Deletes all translations of the $groupId state group.
818
     *
819
     * @param mixed $groupId
820
     */
821
    protected function deleteObjectStateGroupTranslations($groupId)
822
    {
823
        $query = $this->dbHandler->createDeleteQuery();
824
        $query->deleteFrom(
825
            $this->dbHandler->quoteTable('ezcobj_state_group_language')
826
        )->where(
827
            $query->expr->eq(
828
                $this->dbHandler->quoteColumn('contentobject_state_group_id'),
829
                $query->bindValue($groupId, null, \PDO::PARAM_INT)
830
            )
831
        );
832
833
        $query->prepare()->execute();
834
    }
835
}
836