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