diff --git a/src/Entity/Job.php b/src/Entity/Job.php index 878939c..2a69597 100644 --- a/src/Entity/Job.php +++ b/src/Entity/Job.php @@ -957,31 +957,28 @@ class Job extends ContentEntityBase implements EntityOwnerInterface, JobInterfac /** * {@inheritdoc} */ - public function getConflictingItemIds($check_target_language) { - $items = $this->getItems(); - $latest_items_of_chosen_source = array(); - $existing_items_ids = array(); - if ($check_target_language == FALSE || $check_target_language != $this->getTargetLangcode()) { - foreach ($items as $item) { - $latest_items_of_chosen_source[$item->id()] = tmgmt_job_item_load_latest($item->getPlugin(), $item->getItemType(), $item->getItemId(), $this->getSourceLangcode()); - } - foreach ($latest_items_of_chosen_source as $key => $item) { - if ($item && array_key_exists($this->getTargetLangcode(), $item)) { - $existing_items_ids[] = $key; - } - } - } - else { - foreach ($items as $item) { - $latest_items_of_chosen_source[$item->id()] = tmgmt_job_item_load_penultimate($item->getPlugin(), $item->getItemType(), $item->getItemId(), $this->getSourceLangcode(), $this->getTargetLangcode()); - } - foreach ($latest_items_of_chosen_source as $key => $item) { - if ($item && array_key_exists($this->getTargetLangcode(), $item)) { - $existing_items_ids[] = $key; - } + public function getConflictingItemIds() { + $conflicting_item_ids = array(); + foreach ($this->getItems() as $item) { + // Count existing job items that are have the same languages, same source, + // are active or in review and are not the job item that we are checking. + $existing_items_count = \Drupal::entityQuery('tmgmt_job_item') + ->condition('state', [JobItemInterface::STATE_ACTIVE, JobItemInterface::STATE_REVIEW], 'IN') + ->condition('plugin', $item->getPlugin()) + ->condition('item_type', $item->getItemType()) + ->condition('item_id', $item->getItemId()) + ->condition('tjiid', $item->id(), '<>') + ->condition('tjid.entity.source_language', $this->getSourceLangcode()) + ->condition('tjid.entity.target_language', $this->getTargetLangcode()) + ->count() + ->execute(); + + // If there are any, this is a conflicting job item. + if ($existing_items_count) { + $conflicting_item_ids[] = $item->id(); } } - return $existing_items_ids; + return $conflicting_item_ids; } } diff --git a/src/Form/JobForm.php b/src/Form/JobForm.php index 457a628..56f81b3 100644 --- a/src/Form/JobForm.php +++ b/src/Form/JobForm.php @@ -56,14 +56,10 @@ class JobForm extends TmgmtFormBase { $source = $job->getSourceLanguage() ? $job->getSourceLanguage()->getName() : '?'; if (!$job->getTargetLangcode() || $job->getTargetLangcode() == LanguageInterface::LANGCODE_NOT_SPECIFIED) { - $form_state->set('check_target_language', LanguageInterface::LANGCODE_NOT_SPECIFIED); $job->target_language = key($available['target_language']); $target = '?'; } else { - if($form_state->get('check_target_language') == FALSE) { - $form_state->set('check_target_language', $job->getTargetLangcode()); - } $target = $job->getTargetLanguage()->getName(); } @@ -214,7 +210,7 @@ class JobForm extends TmgmtFormBase { if(!$job->isContinuous()) { // Checkout whether given source already has items in translation. - $num_of_existing_items = count($job->getConflictingItemIds($form_state->get('check_target_language'))); + $num_of_existing_items = count($job->getConflictingItemIds()); $form['message'] = array( '#type' => 'html_tag', '#tag' => 'div', @@ -516,7 +512,7 @@ class JobForm extends TmgmtFormBase { } if (!$job->isContinuous() && isset($form['actions']['submit']) && $form_state->getTriggeringElement()['#value'] == $form['actions']['submit']['#value']) { - $existing_items_ids = $job->getConflictingItemIds($form_state->get('check_target_language')); + $existing_items_ids = $job->getConflictingItemIds(); $form_state->set('existing_item_ids', $existing_items_ids); // If the amount of existing items is the same as the total job item count @@ -566,7 +562,7 @@ class JobForm extends TmgmtFormBase { $storage = \Drupal::entityTypeManager()->getStorage('tmgmt_job_item'); $storage->delete($storage->loadMultiple($existing_items_ids)); $num_of_items = count($existing_items_ids); - drupal_set_message(\Drupal::translation()->formatPlural($num_of_items, '1 conflicting item has been dropped.', '@count conflicting items have been dropped.')); + drupal_set_message(\Drupal::translation()->formatPlural($num_of_items, '1 conflicting item has been dropped.', '@count conflicting items have been dropped.'), 'warning'); } if (!tmgmt_job_request_translation($this->entity)) { @@ -646,7 +642,7 @@ class JobForm extends TmgmtFormBase { * target / source language dropdowns. */ public function ajaxLanguageSelect(array $form, FormStateInterface $form_state) { - $number_of_existing_items = count($this->entity->getConflictingItemIds($form_state->get('check_target_language'))); + $number_of_existing_items = count($this->entity->getConflictingItemIds()); $replace = $form_state->getUserInput()['_triggering_element_name'] == 'source_language' ? 'target_language' : 'source_language'; $response = new AjaxResponse(); $response->addCommand(new ReplaceCommand('#tmgmt-ui-translator-wrapper', $form['translator_wrapper'])); diff --git a/src/JobInterface.php b/src/JobInterface.php index fd34021..1d974ba 100644 --- a/src/JobInterface.php +++ b/src/JobInterface.php @@ -726,12 +726,12 @@ interface JobInterface extends ContentEntityInterface, EntityOwnerInterface { /** * Returns conflicting job item IDs. * - * @param string|bool $check_target_language - * Target language. + * Conflicting job items are those that already have an identical item + * in another job that is not yet finished. * * @return int[] * List of conflicting job item IDs. */ - public function getConflictingItemIds($check_target_language); + public function getConflictingItemIds(); } diff --git a/src/Tests/TMGMTUiTest.php b/src/Tests/TMGMTUiTest.php index 0919ca1..09e2887 100644 --- a/src/Tests/TMGMTUiTest.php +++ b/src/Tests/TMGMTUiTest.php @@ -153,7 +153,7 @@ class TMGMTUiTest extends EntityTestBase { $this->assertTrue($job->isActive()); // Another job. - $job = tmgmt_job_match_item('en', 'de'); + $job = tmgmt_job_match_item('en', 'es'); $job->addItem('test_source', 'test', 10); // Go to checkout form. diff --git a/tmgmt.module b/tmgmt.module index a94ed79..deb0792 100644 --- a/tmgmt.module +++ b/tmgmt.module @@ -177,50 +177,6 @@ function tmgmt_job_item_load_latest($plugin, $item_type, $item_id, $source_langu } /** - * Loads penultimate 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. - * @param string $target_language - * The target language of the item. - * - * @return array - * An array of job entities. - */ -function tmgmt_job_item_load_penultimate($plugin, $item_type, $item_id, $source_language, $target_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('tj.target_language', $target_language) - // Only query for jobs that are currently active. - ->condition('tj.state', [Job::STATE_UNPROCESSED, Job::STATE_ACTIVE, Job::STATE_CONTINUOUS], 'IN') - // And only query for job items that are not yet finished. - ->condition('tji.state', JobItem::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') - ->range(1, 1) - ->execute(); - if ($items = $result->fetchAllKeyed()) { - $job_items = array(); - foreach (JobItem::loadMultiple(array_keys($items)) as $key => $item) { - $job_items[$items[$key]] = $item; - } - return $job_items; - } - return FALSE; -} - -/** * Loads all latest job entities that have a job item with the identifiers. * * @param $plugin