forked from Berdir/tmgmt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmgmt.module
1549 lines (1454 loc) · 45.4 KB
/
tmgmt.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Main module file for the Translation Management module.
*/
/**
* @addtogroup tmgmt_job
* @{
*/
/**
* A new translation job.
*
* In the default user interface, jobs with this state are so called cart jobs.
* Each user gets his cart jobs listed in a block and can check them out.
*/
define('TMGMT_JOB_STATE_UNPROCESSED', 0);
/**
* A translation job that has been submitted to the translator.
*
* Translator plugins are responsible for setting this state in their
* implementation of
* TMGMTTranslatorPluginControllerInterface::requestTranslation().
*/
define('TMGMT_JOB_STATE_ACTIVE', 1);
/**
* A translation job that has been rejected by the translator.
*
* The translator plugin can use this state if the job has been actively
* rejected. However, this should be avoided by doing the necessary checks
* in the canTranslate() method and in the job configuration settings.
*
* A rejected job can be re-submitted.
*/
define('TMGMT_JOB_STATE_REJECTED', 2);
/**
* The translation has been accepted and the job is finished.
*
* Once the job has been accepted, the source plugins are called to update their
* sources with the translated data.
*/
define('TMGMT_JOB_STATE_ACCEPTED', 3);
/**
* The translation job has been aborted.
*
* A job can be aborted at any time. If he is currently in the submitted state
* the translator plugin is asked if this translation can be aborted and needs
* to confirm it by returning TRUE in abortTranslation().
*/
define('TMGMT_JOB_STATE_ABORTED', 4);
/**
* The translation job has been finished.
*
* A job is marked as 'finished' after every single attached job item has been
* reviewed, accepted and saved.
*/
define('TMGMT_JOB_STATE_FINISHED', 5);
/**
* The translation job item is active and waiting to be translated.
*
* A job item is marked as 'active' until every translatable piece of text in
* the job item has been translated and cached on the job item entity.
*/
define('TMGMT_JOB_ITEM_STATE_ACTIVE', 1);
/**
* The translation job item needs to be reviewed.
*
* A job item is marked as 'needs review' after every single piece of text in
* the job item has been translated by the translation provider. After the
* review procedure is finished the job item can be accepted and saved.
*/
define('TMGMT_JOB_ITEM_STATE_REVIEW', 2);
/**
* The translation job item has been reviewed and accepted.
*
* After reviewing a job item it can be accepted by the reviewer. Once the user
* has accepted the job item, the translated data will be propagated to the
* source controller which will also take care of flagging the job item as
* 'accepted' if the translated object could be saved successfully.
*/
define('TMGMT_JOB_ITEM_STATE_ACCEPTED', 3);
/**
* The translation process of the job item is aborted.
*/
define('TMGMT_JOB_ITEM_STATE_ABORTED', 4);
/**
* The translation data item has not been translated.
*/
define('TMGMT_DATA_ITEM_STATE_PENDING', 0);
/**
* The translation data item has been reviewed.
*/
define('TMGMT_DATA_ITEM_STATE_REVIEWED', 1);
/**
* The translation data item has been translated.
*/
define('TMGMT_DATA_ITEM_STATE_TRANSLATED', 2);
/**
* The translation data item has been reviewed.
*/
define('TMGMT_DATA_ITEM_STATE_ACCEPTED', 3);
/**
* Maximum length of a job or job item label.
*/
define('TMGMT_JOB_LABEL_MAX_LENGTH', 128);
/**
* @} End of "addtogroup tmgmt_job".
*/
/**
* String used to delimit flattened array keys.
*/
define('TMGMT_ARRAY_DELIMITER', '][');
/**
* Implements hook_entity_info().
*/
function tmgmt_entity_info() {
$info['tmgmt_job'] = array(
'label' => t('Translation Management Job'),
'module' => 'tmgmt',
'controller class' => 'TMGMTJobController',
'metadata controller class' => 'TMGMTJobMetadataController',
'views controller class' => 'TMGMTJobViewsController',
'entity class' => 'TMGMTJob',
'base table' => 'tmgmt_job',
'uri callback' => 'entity_class_uri',
'label callback' => 'entity_class_label',
'access callback' => 'tmgmt_job_access',
'entity keys' => array(
'id' => 'tjid',
),
);
$info['tmgmt_job_item'] = array(
'label' => t('Translation Management Job Item'),
'module' => 'tmgmt',
'controller class' => 'TMGMTJobItemController',
'metadata controller class' => 'TMGMTJobItemMetadataController',
'views controller class' => 'TMGMTJobItemViewsController',
'entity class' => 'TMGMTJobItem',
'base table' => 'tmgmt_job_item',
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'access callback' => 'tmgmt_job_item_access',
'entity keys' => array(
'id' => 'tjiid',
),
);
$info['tmgmt_message'] = array(
'label' => t('Translation Management Message'),
'module' => 'tmgmt',
'controller class' => 'EntityAPIController',
'metadata controller class' => 'TMGMTMessageMetadataController',
'views controller class' => 'TMGMTMessageViewsController',
'entity class' => 'TMGMTMessage',
'base table' => 'tmgmt_message',
'label callback' => 'entity_class_label',
'access callback' => 'tmgmt_message_access',
'entity keys' => array(
'id' => 'mid',
),
);
$info['tmgmt_translator'] = array(
'label' => t('Translation Management Translator'),
'module' => 'tmgmt',
'controller class' => 'TMGMTTranslatorController',
'metadata controller class' => 'TMGMTTranslatorMetadataController',
'views controller class' => 'EntityDefaultViewsController',
'entity class' => 'TMGMTTranslator',
'base table' => 'tmgmt_translator',
'exportable' => TRUE,
'access callback' => 'tmgmt_translator_access',
'entity keys' => array(
'id' => 'tid',
'name' => 'name',
'label' => 'label',
),
);
// Make use of the entity cache module if it is enabled.
if (module_exists('entitycache')) {
$info['tmgmt_translator']['entity cache'] = TRUE;
$info['tmgmt_translator']['field cache'] = FALSE;
}
$info['tmgmt_remote'] = array(
'label' => t('Remote job mapping'),
'module' => 'tmgmt',
'controller class' => 'TMGMTRemoteController',
'entity class' => 'TMGMTRemote',
'base table' => 'tmgmt_remote',
'uri callback' => 'entity_class_uri',
'label callback' => 'entity_class_label',
'access callback' => 'tmgmt_remote_access',
'entity keys' => array(
'id' => 'trid',
),
);
return $info;
}
/**
* Implements hook_permission().
*/
function tmgmt_permission() {
$perms['administer tmgmt'] = array(
'title' => t('Administer translation management'),
);
$perms['create translation jobs'] = array(
'title' => t('Create translation jobs'),
);
$perms['submit translation jobs'] = array(
'title' => t('Submit translation jobs'),
);
$perms['accept translation jobs'] = array(
'title' => t('Accept and reject translation jobs'),
);
return $perms;
}
/**
* Implements hook_modules_installed().
*/
function tmgmt_modules_installed($modules) {
foreach (tmgmt_translator_plugin_info() as $key => $info) {
// Check if this translator plugin has been added by one of the recently
// installed modules and doesn't prevent auto creation.
if ((!isset($info['auto create']) || $info['auto create'] == TRUE) && in_array($info['module'], $modules)) {
tmgmt_translator_auto_create($key);
}
}
}
/**
* Implements hook_flush_caches().
*/
function tmgmt_flush_caches() {
return array('cache_tmgmt');
}
/**
* Implements hook_cron().
*/
function tmgmt_cron() {
$offset = variable_get('tmgmt_purge_finished', '_never');
if ($offset != '_never') {
// Delete all finished translation jobs that haven't been changed for a
// time span longer than the given offset.
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'tmgmt_job')
->propertyCondition('state', TMGMT_JOB_STATE_FINISHED)
->propertyCondition('changed', REQUEST_TIME - $offset, '<=')
->execute();
if (!empty($result['tmgmt_job'])) {
$controller = entity_get_controller('tmgmt_job');
// Since the entity controller handles the deletion of the attached
// entities (messages, job items) we just need to invoke it directly.
$controller->delete(array_keys($result['tmgmt_job']));
}
}
}
/**
* Implements hook_views_api().
*/
function tmgmt_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'tmgmt') . '/views',
);
}
/**
* Returns an array of languages that are available for translation.
*
* @return array
* An array of languages in ISO format.
*/
function tmgmt_available_languages($exclude = array()) {
$languages = entity_metadata_language_list();
// Remove LANGUAGE_NONE and the language in $exclude from the list of
// available languages and then apply a filter that only leaves the supported
// target languages on the list.
unset($languages[LANGUAGE_NONE]);
foreach ($exclude as $item) {
unset($languages[$item]);
}
return $languages;
}
/**
* Returns the label of a language.
*
* @param $language
* A language in ISO format.
* @return string
* The label of the language or an empty string if the language or its label
* are not defined.
*/
function tmgmt_language_label($language) {
$languages = entity_metadata_language_list();
if (!empty($languages[$language])) {
return $languages[$language];
}
return '';
}
/**
* @addtogroup tmgmt_job
* @{
*/
/**
* Loads a translation job.
*
* @param int $tjid
* Translation job id.
*
* @return TMGMTJob
* Loaded translation job entity.
*/
function tmgmt_job_load($tjid) {
$jobs = tmgmt_job_load_multiple(array($tjid), array());
return $jobs ? reset($jobs) : FALSE;
}
/**
* Loads translation jobs.
*/
function tmgmt_job_load_multiple(array $tjids = array(), $conditions = array()) {
return entity_load('tmgmt_job', $tjids, $conditions);
}
/**
* Loads active job entities that have a job item with the identifiers.
*
* @param $plugin
* The source plugin.
* @param $item_type
* The source item type.
* @param $item_id
* The source item id.
* @param string $source_language
* The source language of the item.
*
* @return array
* An array of job entities.
*/
function tmgmt_job_item_load_latest($plugin, $item_type, $item_id, $source_language) {
$query = db_select('tmgmt_job_item', 'tji');
$query->innerJoin('tmgmt_job', 'tj', 'tj.tjid = tji.tjid');
$result = $query->condition('tj.source_language', $source_language)
// Only query for jobs that are currently active.
->condition('tj.state', array(TMGMT_JOB_STATE_UNPROCESSED, TMGMT_JOB_STATE_ACTIVE))
// And only query for job items that are not yet finished.
->condition('tji.state', TMGMT_JOB_ITEM_STATE_ACCEPTED, '<>')
->condition('tji.plugin', $plugin)
->condition('tji.item_type', $item_type)
->condition('tji.item_id', $item_id)
->fields('tji', array('tjiid'))
->fields('tj', array('target_language'))
->orderBy('tji.changed', 'DESC')
->groupBy('tj.target_language')
->groupBy('tji.tjiid')
->execute();
if ($items = $result->fetchAllKeyed()) {
$return = array();
foreach (tmgmt_job_item_load_multiple(array_keys($items)) as $key => $item) {
$return[$items[$key]] = $item;
}
return $return;
}
return FALSE;
}
/**
* Loads all latest job entities that have a job item with the identifiers.
*
* @param $plugin
* The source plugin.
* @param $item_type
* The source item type.
* @param $item_id
* The source item id.
* @param string $source_language
* The source language of the item.
*
* @return array
* An array of job entities.
*/
function tmgmt_job_item_load_all_latest($plugin, $item_type, $item_id, $source_language) {
$query = db_select('tmgmt_job_item', 'tji');
$query->innerJoin('tmgmt_job', 'tj', 'tj.tjid = tji.tjid');
$result = $query->condition('tj.source_language', $source_language)
->condition('tji.state', TMGMT_JOB_ITEM_STATE_ACCEPTED, '<>')
->condition('tji.plugin', $plugin)
->condition('tji.item_type', $item_type)
->condition('tji.item_id', $item_id)
->fields('tji', array('tjiid'))
->fields('tj', array('target_language'))
->orderBy('tji.changed', 'DESC')
->groupBy('tj.target_language')
->groupBy('tji.tjiid')
->execute();
if ($items = $result->fetchAllKeyed()) {
$return = array();
foreach (tmgmt_job_item_load_multiple(array_keys($items)) as $key => $item) {
$return[$items[$key]] = $item;
}
return $return;
}
return FALSE;
}
/**
* Returns a job which matches the requested source- and target language by
* user. If no job exists, a new job object will be created.
*
* @param $source_language
* The source language from which should be translated.
* @param $target_language
* The target language into which should be translated.
* @param $account
* (Optional) A user object. Defaults to the currently logged in user.
*
* @return TMGMTJob
* The job entity.
*/
function tmgmt_job_match_item($source_language, $target_language, $account = NULL) {
$account = isset($account) ? $account : $GLOBALS['user'];
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'tmgmt_job')
->propertyCondition('source_language', $source_language)
->propertyCondition('target_language', $target_language)
->propertyCondition('uid', $account->uid)
->propertyCondition('state', TMGMT_JOB_STATE_UNPROCESSED)
->execute();
if (!empty($result['tmgmt_job'])) {
$job = reset($result['tmgmt_job']);
return tmgmt_job_load($job->tjid);
}
return tmgmt_job_create($source_language, $target_language, $account->uid);
}
/**
* Checks whether a job is finished by querying the job item table for
* unfinished job items.
*
* @param $tjid
* The identifier of the job.
* @return bool
* TRUE if the job is finished, FALSE otherwise.
*/
function tmgmt_job_check_finished($tjid) {
$query = new EntityFieldQuery();
return !(boolean) $query->entityCondition('entity_type', 'tmgmt_job_item')
->propertyCondition('tjid', $tjid)
->propertyCondition('state', TMGMT_JOB_ITEM_STATE_ACCEPTED, '<>')
->range(0, 1)
->count()
->execute();
}
/**
* Creates a translation job.
*
* @param $source_language
* The source language from which should be translated.
* @param $target_language
* The target language into which should be translated.
* @param $values
* (Optional) An array of additional entity values.
*
* @return TMGMTJob
* The job entity.
*/
function tmgmt_job_create($source_language, $target_language, $uid = NULL, array $values = array()) {
return entity_create('tmgmt_job', array_merge($values, array(
'source_language' => $source_language,
'target_language' => $target_language,
'uid' => $uid,
)));
}
/**
* Access callback for the job entity.
*
*
* @param $op
* The operation being performed.
* @param $item
* (Optional) A TMGMTJob entity to check access for. If no entity is given, it
* will be determined whether access is allowed for all entities.
* @param $account
* (Optional) The user to check for. Leave it to NULL to check for the global
* user.
*
* @return boolean
* TRUE if access is allowed, FALSE otherwise.
*/
function tmgmt_job_access($op, $job = NULL, $account = NULL) {
if (user_access('administer tmgmt', $account)) {
// Administrators can do everything.
return TRUE;
}
switch ($op) {
case 'create':
return user_access('create translation jobs', $account);
break;
case 'view':
case 'update':
return user_access('create translation jobs', $account) || user_access('submit translation jobs', $account) || user_access('accept translation jobs', $account);
break;
case 'delete':
// Only administrators can delete jobs.
return FALSE;
break;
// Custom operations.
case 'submit':
return user_access('submit translation jobs');
break;
case 'abort':
case 'resubmit':
return user_access('submit translation jobs');
break;
case 'accept':
return user_access('accept translation jobs');
break;
}
}
/**
* Access callback for tmgmt remote entity.
*/
function tmgmt_remote_access($op, $tmgmt_remote = NULL, $account = NULL) {
return user_access('administer tmgmt', $account);
}
/**
* Loads an array with the word and status statistics of a job.
*
* @param $tjids
* An array of job ids.
*
* @return
* An array of objects with the keys word_count, count_pending,
* count_accepted, count_reviewed and count_translated.
*/
function tmgmt_job_statistics_load(array $tjids) {
$statistics = &drupal_static(__FUNCTION__, array());
// First try to get the values from the cache.
$return = array();
$tjids_to_load = array();
foreach ($tjids as $tjid) {
if (isset($statistics[$tjid])) {
// Info exists in cache, get it from there.
$return[$tjid] = $statistics[$tjid];
}
else {
// Info doesn't exist in cache, add job to the list that needs to be
// fetched.
$tjids_to_load[] = $tjid;
}
}
// If there are remaining jobs, build a query to fetch them.
if (!empty($tjids_to_load)) {
// Build the query to fetch the statistics.
$query = db_select('tmgmt_job_item', 'tji')
->fields('tji', array('tjid'));
$query->addExpression('SUM(word_count)', 'word_count');
$query->addExpression('SUM(count_accepted)', 'count_accepted');
$query->addExpression('SUM(count_reviewed)', 'count_reviewed');
$query->addExpression('SUM(count_pending)', 'count_pending');
$query->addExpression('SUM(count_translated)', 'count_translated');
$result = $query->groupBy('tjid')
->condition('tjid', $tjids_to_load)
->execute();
foreach ($result as $row) {
$return[$row->tjid] = $statistics[$row->tjid] = $row;
}
}
return $return;
}
/**
* Returns a specific statistic of a job.
*
* @param $job
* The translation job entity.
* @param $key
* One of word_count, count_pending, count_accepted, count_reviewed and
* count_translated.
*
* @return
* The requested information as an integer.
*/
function tmgmt_job_statistic(TMGMTJob $job, $key) {
$statistics = tmgmt_job_statistics_load(array($job->tjid));
if (isset($statistics[$job->tjid]->$key)) {
return $statistics[$job->tjid]->$key;
}
return 0;
}
/**
* Access callback for the job item entity.
*
* @param $op
* The operation being performed.
* @param $item
* (Optional) A TMGMTJobItem entity to check access for. If no entity is
* given, it will be determined whether access is allowed for all entities.
* @param $account
* (Optional) The user to check for. Leave it to NULL to check for the global
* user.
*
* @return boolean
* TRUE if access is allowed, FALSE otherwise.
*/
function tmgmt_job_item_access($op, TMGMTJobItem $item = NULL, $account = NULL) {
// There are no item specific permissions yet.
return tmgmt_job_access($op, $item ? $item->getJob() : NULL, $account);
}
/**
* Access callback wrapper for reviewing a job item entity.
*
* @param TMGMTJobItem $item
* The job item to check access for.
* @param $account
* (Optional) The user to check for. Leave it to NULL to check for the global
* user.
*
* @return boolean
* TRUE if access is allowed, FALSE otherwise.
*/
function tmgmt_job_item_review_access(TMGMTJobItem $item, $account = NULL) {
if ($item->isNeedsReview() && $item->getSourceController() && $item->getTranslatorController()) {
return tmgmt_job_item_access('accept', $item, $account);
}
return FALSE;
}
/**
* Access callback for the job message entity.
*
* @param $op
* The operation being performed.
* @param $item
* (Optional) A TMGMTJobMessage entity to check access for. If no entity is
* given, it will be determined whether access is allowed for all entities.
* @param $account
* (Optional) The user to check for. Leave it to NULL to check for the global
* user.
*
* @return boolean
* TRUE if access is allowed, FALSE otherwise.
*/
function tmgmt_message_access($op, TMGMTMessage $message = NULL, $account = NULL) {
// All users that can see jobs can see messages as well.
if ($op == 'view') {
$job = NULL;
if ($message) {
$job = $message->getJob();
}
return tmgmt_job_access('view', $job, $account);
}
// Changing or creating messages is only possible for admins.
return user_access('administer tmgmt');
}
/**
* Static method to retrieve a labeled list of all available states.
*
* @return array
* A list of all available states.
*/
function tmgmt_job_states() {
return array(
TMGMT_JOB_STATE_UNPROCESSED => t('Unprocessed'),
TMGMT_JOB_STATE_ACTIVE => t('Active'),
TMGMT_JOB_STATE_REJECTED => t('Rejected'),
TMGMT_JOB_STATE_ABORTED => t('Aborted'),
TMGMT_JOB_STATE_FINISHED => t('Finished'),
);
}
/**
* Static method to retrieve a labeled list of all available states.
*
* @return array
* A list of all available states.
*/
function tmgmt_job_item_states() {
return array(
TMGMT_JOB_ITEM_STATE_ACTIVE => t('In progress'),
TMGMT_JOB_ITEM_STATE_REVIEW => t('Needs review'),
TMGMT_JOB_ITEM_STATE_ACCEPTED => t('Accepted'),
TMGMT_JOB_ITEM_STATE_ABORTED => t('Aborted'),
);
}
/**
* Loads a translation job item.
*
* @param $tjiid
* A job item id.
*
* @return TMGMTJobItem
* The loaded job item or FALSE if the query returned no results.
*/
function tmgmt_job_item_load($tjiid) {
$jobs = tmgmt_job_item_load_multiple(array($tjiid), array());
return $jobs ? reset($jobs) : FALSE;
}
/**
* Loads translation job items.
*
* @param $tjiids
* An array of job item ids.
* @param $conditions
* An array of additional conditions.
*
* @return TMGMTJobItem[]
* An array of job item entities or an empty array if the query returned no
* results.
*/
function tmgmt_job_item_load_multiple($tjiids = array(), $conditions = array()) {
return entity_load('tmgmt_job_item', $tjiids, $conditions);
}
/**
* Creates a translation job item.
*
* @param $plugin
* The plugin name.
* @param $item_type
* The source item type.
* @param $item_id
* The source item id.
* @param $values
* (Optional) An array of additional entity values to be set.
*
* @return TMGMTJobItem
* The created, not yet saved, job item entity.
*/
function tmgmt_job_item_create($plugin, $item_type, $item_id, array $values = array()) {
return entity_create('tmgmt_job_item', array_merge($values, array(
'plugin' => $plugin,
'item_type' => $item_type,
'item_id' => $item_id,
)));
}
/**
* Loads a translation job message.
*
* @param $mid
* A job message id.
*
* @return TMGMTMessage
* A job message entity or FALSE if the query didn't yield any results.
*/
function tmgmt_message_load($mid) {
// Avoid collision with the message module because this looks like the module
// implements hook_ENTITY_TYPE_load() for message.
if (!is_array($mid)) {
$jobs = tmgmt_message_load_multiple(array($mid));
return $jobs ? reset($jobs) : FALSE;
}
}
/**
* Loads translation job messages.
*/
function tmgmt_message_load_multiple($mids = array(), $conditions = array()) {
return entity_load('tmgmt_message', $mids, $conditions);
}
/**
* Creates a translation job message.
*
* @param $message
* (Optional) The message to be saved.
* @param $variables
* (Optional) An array of variables to replace in the message on display.
* @param $values
* (Optional) An array of additional entity values to be set.
*
* @return TMGMTJobItem
* The created, not yet saved, job item entity.
*/
function tmgmt_message_create($message = '', $variables = array(), $values = array()) {
return entity_create('tmgmt_message', array_merge($values, array(
'message' => $message,
'variables' => $variables,
)));
}
/**
* @} End of "addtogroup tmgmt_job".
*/
/**
* @addtogroup tmgmt_translator
* @{
*/
/**
* Access callback for the translator entity.
*/
function tmgmt_translator_access($op, TMGMTTranslator $translator = NULL, $account = NULL) {
if (isset($translator) && !$translator->getController()) {
return FALSE;
}
// Only administrators are allowed to manage translator entities.
return user_access('administer tmgmt', $account);
}
/**
* Checks whether a translator entity with the supplied name already exists.
*
* We can't user entity_load or any of its wrapper functions for that as our
* translator entity controller filters out broken translator entities (e.g. if
* the translator plugin of the translator entity doesn't exist (anymore).
*
* @param $name
* The machine-readable name of the translator entity that we are trying to
* save.
*
* @return boolean
* TRUE if a translator entity with the same machine-readable name already
* exists FALSE otherwise.
*/
function tmgmt_translator_exists($name) {
$query = new EntityFieldQuery();
return (boolean) $query->entityCondition('entity_type', 'tmgmt_translator')
->propertyCondition('name', $name)
->count()
->range(0, 1)
->execute();
}
/**
* Loads a translator based on the name.
*
* @param $name
* The machine-readable name of the translator entity to load.
*
* @return TMGMTTranslator
* A translator entity.
*/
function tmgmt_translator_load($name) {
$translators = entity_load_multiple_by_name('tmgmt_translator', array($name));
return $translators ? reset($translators) : FALSE;
}
/**
* Loads multiple translators based on their name.
*
* @param $names
* (Optional) An array of machine-readable names of the translator entities to
* load or FALSE to load all available translator entities.
*
* @return array
* An array of translators with the machine-readable name of the translators
* as array keys.
*/
function tmgmt_translator_load_multiple($names = array()) {
return entity_load_multiple_by_name('tmgmt_translator', $names);
}
/**
* Loads all translators that are available and, if a translation job is given,
* support translations for that job with its current configuration.
*
* @param TMGMTJob $job
* (Optional) A translation job.
*
* @return array
* An array of translators with the machine-readable name of the translators
* as array keys.
*/
function tmgmt_translator_load_available($job) {
$translators = tmgmt_translator_load_multiple(FALSE);
foreach ($translators as $name => $translator) {
if (!$translator->isAvailable() || (isset($job) && !$translator->canTranslate($job))) {
unset($translators[$name]);
}
}
return $translators;
}
/**
* Checks whether a translator with a certain name is busy and therefore can't
* be modified or deleted. A translator is considered 'busy' if there are jobs
* attached to it that are in an active state.
*
* @param $translator
* The machine-readable name of a translator.
*
* @return boolean
* TRUE if the translator is busy, FALSE otherwise.
*/
function tmgmt_translator_busy($translator) {
$query = new EntityFieldQuery();
return (boolean) $query->entityCondition('entity_type', 'tmgmt_job')
->propertyCondition('state', TMGMT_JOB_STATE_ACTIVE)
->propertyCondition('translator', $translator)
->range(0, 1)
->count()
->execute();
}
/**
* Creates a translator entity.
*
* @param $plugin
* The plugin of the translator.
* @param $name
* The machine-readable name of the translator.
* @param $label
* The label of the translator.
* @param $description
* (Optional) The description of the translator. Defaults to an empty string.
* @param $settings
* (Optional) An array of settings for the translator.
* @param $values
* (Optional) Array of additional entity values.
*
* @return TMGMTTranslator
* The created, not yet saved, translator entity.
*/
function tmgmt_translator_create($plugin, $name, $label, $description = '', $settings = array(), $values = array()) {
return entity_create('tmgmt_translator', array_merge($values, array(
'plugin' => $plugin,
'name' => $name,
'label' => $label,
'description' => $description,
'settings' => $settings,
)));
}
/**
* Auto creates a translator from a translator plugin definition.
*
* @param $plugin
* The machine-readable name of a translator plugin.
*/
function tmgmt_translator_auto_create($plugin) {
if ($info = tmgmt_translator_plugin_info($plugin)) {
if (!tmgmt_translator_exists($plugin)) {
$label = $info['label'] . ' (auto created)';
$translator = tmgmt_translator_create($plugin, $plugin, $label, $info['description']);
// Append some default settings from the translator plugin definition.
$translator->settings = $translator->getController()->defaultSettings();
$translator->save();
}
}
}
/**
* Determines all available service plugins.
*
* @param $plugin
* (Optional) The machine-readable name of a service plugin.
*
* @return array
* An array of translator plugin definitions.
*/
function tmgmt_translator_plugin_info($plugin = NULL) {
return _tmgmt_plugin_info('translator', $plugin);
}
/**
* Determines the controller class for a given service plugin.
*
* @param $plugin
* (Optional) The machine-readable name of a service plugin.
*