PostJobsRepository.php
2.39 KB
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
<?php
namespace WPML\TM\Menu\Dashboard;
use WPML\FP\Lst;
use WPML\TM\ATE\Review\ReviewStatus;
class PostJobsRepository {
/**
* @param int $original_element_id
* @param string $element_type
*
* @return array
*/
public function getJobsGroupedByLang( $original_element_id, $element_type ) {
return $this->getJobsFor( $original_element_id, $element_type )
->map( [ $this, 'mapJob' ] )
->keyBy( 'targetLanguage' )
->toArray();
}
/**
* @param int $original_element_id
* @param string $element_type
*
* @return \WPML\Collect\Support\Collection
*/
private function getJobsFor( $original_element_id, $element_type ) {
return \wpml_collect(
wpml_tm_get_jobs_repository()->get( $this->buildSearchParams( $original_element_id, $element_type ) )
);
}
/**
* @param int $original_element_id
* @param string $element_type
*
* @return \WPML_TM_Jobs_Search_Params
*/
private function buildSearchParams( $original_element_id, $element_type ) {
$params = new \WPML_TM_Jobs_Search_Params();
$params->set_original_element_id( $original_element_id );
$params->set_job_types( $element_type );
return $params;
}
/**
* @param \WPML_TM_Post_Job_Entity $job
*
* @return array
*/
public function mapJob( \WPML_TM_Post_Job_Entity $job ) {
return [
'entity_id' => $job->get_id(),
'job_id' => $job->get_translate_job_id(),
'type' => $job->get_type(),
'status' => $this->getJobStatus( $job ),
'targetLanguage' => $job->get_target_language(),
'isLocal' => 'local' === $job->get_translation_service(),
'needsReview' => Lst::includes( $job->get_review_status(), [ ReviewStatus::NEEDS_REVIEW, ReviewStatus::EDITING ] ),
'automatic' => $job->is_automatic(),
'editor' => $job->get_editor(),
];
}
/**
* @param \WPML_TM_Job_Entity $job
*
* @return int
*/
private function getJobStatus( \WPML_TM_Job_Entity $job ) {
if ( $job->does_need_update() ) {
return ICL_TM_NEEDS_UPDATE;
}
if ( $this->postHasTranslationButLatestJobCancelled( $job ) ) {
return ICL_TM_COMPLETE;
}
return $job->get_status();
}
/**
* @param \WPML_TM_Job_Entity $job
*
* @return bool
*/
private function postHasTranslationButLatestJobCancelled( \WPML_TM_Job_Entity $job ) {
return $job->get_status() === ICL_TM_NOT_TRANSLATED && $job->has_completed_translation();
}
}