View source
<?php
define('TMGMT_LOCAL_TRANSLATION_ACCESS_ALLOW', 'allow');
define('TMGMT_LOCAL_TRANSLATION_ACCESS_DENY', 'deny');
define('TMGMT_LOCAL_TRANSLATION_ACCESS_IGNORE', NULL);
define('TMGMT_LOCAL_TASK_STATUS_UNASSIGNED', 0);
define('TMGMT_LOCAL_TASK_STATUS_PENDING', 1);
define('TMGMT_LOCAL_TASK_STATUS_COMPLETED', 2);
define('TMGMT_LOCAL_TASK_STATUS_REJECTED', 3);
define('TMGMT_LOCAL_TASK_STATUS_CLOSED', 4);
define('TMGMT_LOCAL_TASK_ITEM_STATUS_PENDING', 0);
define('TMGMT_LOCAL_TASK_ITEM_STATUS_COMPLETED', 1);
define('TMGMT_LOCAL_TASK_ITEM_STATUS_REJECTED', 2);
define('TMGMT_LOCAL_TASK_ITEM_STATUS_CLOSED', 3);
define('TMGMT_DATA_ITEM_STATE_UNTRANSLATED', 0);
function tmgmt_local_entity_info() {
$info = array(
'tmgmt_local_task' => array(
'label' => t('Translation Task'),
'module' => 'tmgmt_local',
'entity class' => 'TMGMTLocalTask',
'controller class' => 'TMGMTLocalTaskController',
'metadata controller class' => 'TMGMTLocalTaskMetadataController',
'views controller class' => 'TMGMTLocalTaskViewsController',
'base table' => 'tmgmt_local_task',
'uri callback' => 'entity_class_uri',
'label callback' => 'entity_class_label',
'access callback' => 'tmgmt_local_task_access',
'entity keys' => array(
'id' => 'tltid',
),
'admin ui' => array(
'controller class' => 'TMGMTLocalTaskUIController',
'path' => 'translate',
),
),
'tmgmt_local_task_item' => array(
'label' => t('Translation Task Item'),
'module' => 'tmgmt_local',
'entity class' => 'TMGMTLocalTaskItem',
'controller class' => 'TMGMTLocalTaskItemController',
'metadata controller class' => 'TMGMTLocalTaskItemMetadataController',
'views controller class' => 'TMGMTLocalTaskItemViewsController',
'base table' => 'tmgmt_local_task_item',
'uri callback' => 'entity_class_uri',
'label callback' => 'entity_class_label',
'access callback' => 'tmgmt_local_task_item_access',
'entity keys' => array(
'id' => 'tltiid',
),
'admin ui' => array(
'controller class' => 'TMGMTLocalTaskItemUIController',
'path' => 'translate',
),
),
);
return $info;
}
function tmgmt_local_theme() {
return array(
'tmgmt_local_translation_form' => array(
'render element' => 'element',
'file' => 'includes/tmgmt_local.theme.inc',
),
'tmgmt_local_translation_form_element' => array(
'render element' => 'element',
'file' => 'includes/tmgmt_local.theme.inc',
),
'tmgmt_local_translation_form_element_status' => array(
'render element' => 'status',
'file' => 'includes/tmgmt_local.theme.inc',
),
);
}
function tmgmt_local_menu() {
$items['translate/%tmgmt_local_task/assign-to-me'] = array(
'title' => 'Assign translation task to me',
'description' => 'Assign translation task to current translator user.',
'page callback' => 'tmgmt_local_translation_assign_to_me',
'page arguments' => array(
1,
),
'access callback' => 'tmgmt_local_translation_access',
'access arguments' => array(
1,
),
'file' => 'includes/tmgmt_local.pages.inc',
);
$items['manage-translate/assign-tasks'] = array(
'title' => 'Assign translation task',
'description' => 'Assign translation tasks to specific translator.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tmgmt_local_translation_assign_form',
2,
),
'access arguments' => array(
'administer translation tasks',
),
'file' => 'includes/tmgmt_local.pages.inc',
'type' => MENU_CALLBACK,
);
$items['manage-translate/reassign-tasks'] = array(
'title' => 'Reassign translation task',
'description' => 'Ressign translation tasks to specific translator.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tmgmt_local_translation_reassign_form',
2,
),
'access arguments' => array(
'administer translation tasks',
),
'file' => 'includes/tmgmt_local.pages.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function tmgmt_local_permission() {
return array(
'provide translation services' => array(
'title' => t('Provide translation services'),
'descriptions' => t('Root permission for translation access: Users with this permission are eligible to be granted translation access to a translation task.'),
),
'administer translation tasks' => array(
'title' => t('Administer translation tasks'),
'description' => t('Administer translation tasks.'),
),
);
}
function tmgmt_local_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'tmgmt_local') . '/views',
);
}
function tmgmt_local_views_default_views() {
return _tmgmt_load_exports('tmgmt_local', 'views', 'view.inc', 'view');
}
function tmgmt_local_tmgmt_translator_plugin_info() {
$info['local'] = array(
'label' => t('Local Translator'),
'description' => t('Allows local users to process translation jobs.'),
'plugin controller class' => 'TMGMTLocalTranslatorPluginController',
'ui controller class' => 'TMGMTLocalTranslatorUIController',
'map remote languages' => FALSE,
);
return $info;
}
function tmgmt_local_translation_access(TMGMTLocalTask $task, $account = NULL) {
$job = $task
->getJob();
if (!$job || !$job
->isActive()) {
return FALSE;
}
$rights =& drupal_static(__FUNCTION__);
if (empty($account)) {
$account = $GLOBALS['user'];
}
if (isset($rights[$account->uid][$job->tjid])) {
return $rights[$account->uid][$job->tjid];
}
if (!user_access('provide translation services')) {
$rights[$account->uid][$job->tjid] = FALSE;
return FALSE;
}
if ($task->tuid == $account->uid) {
$rights[$account->uid][$job->tjid] = TRUE;
return TRUE;
}
$access = module_invoke_all('tmgmt_local_translation_access', $job, $account);
if (in_array(TMGMT_LOCAL_TRANSLATION_ACCESS_DENY, $access, TRUE)) {
$rights[$account->uid][$job->tjid] = FALSE;
return FALSE;
}
elseif (in_array(TMGMT_LOCAL_TRANSLATION_ACCESS_ALLOW, $access, TRUE)) {
$rights[$account->uid][$job->tjid] = TRUE;
return TRUE;
}
$target_languages = tmgmt_local_supported_target_languages($job->source_language, array(
$account->uid,
));
$rights[$account->uid][$job->tjid] = in_array($job->target_language, $target_languages);
return $rights[$account->uid][$job->tjid];
}
function tmgmt_local_clear_languages_cache() {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'tmgmt_translator');
$query
->propertyCondition('plugin', 'local');
$result = $query
->execute();
if ($result) {
foreach (tmgmt_translator_load_multiple(array_keys($result['tmgmt_translator'])) as $translator) {
$translator
->clearLanguageCache();
}
}
}
function tmgmt_local_task_load($tltid) {
return entity_load_single('tmgmt_local_task', $tltid);
}
function tmgmt_local_task_load_multiple(array $tltids = array(), $conditions = array()) {
return entity_load('tmgmt_local_task', $tltids, $conditions);
}
function tmgmt_local_task_item_load($tltiid) {
return entity_load_single('tmgmt_local_task_item', $tltiid);
}
function tmgmt_local_task_item_load_multiple(array $tltiids = array(), $conditions = array()) {
return entity_load('tmgmt_local_task_item', $tltiids, $conditions);
}
function tmgmt_local_task_create(array $values = array()) {
return entity_create('tmgmt_local_task', $values);
}
function tmgmt_local_task_delete_multiple(array $tltids) {
entity_get_controller('tmgmt_local_task')
->delete($tltids);
}
function tmgmt_local_task_access($op, $task = NULL, $account = NULL) {
if (user_access('administer tmgmt', $account) || user_access('administer translation tasks', $account)) {
return TRUE;
}
if (!$account) {
global $user;
$account = $user;
}
switch ($op) {
case 'view':
case 'update':
return user_access('provide translation services', $account);
break;
case 'unassign':
return !empty($task->tuid) && $task->tuid == $account->uid && user_access('provide translation services', $account);
}
}
function tmgmt_local_task_item_access($op, TMGMTLocalTaskItem $item = NULL, $account = NULL) {
$task = NULL;
if ($item) {
$task = $item
->getTask();
}
return entity_access($op, 'tmgmt_local_task', $task, $account);
}
function tmgmt_local_task_statistics_load(array $tltids) {
$statistics =& drupal_static(__FUNCTION__, array());
$return = array();
$tltids_to_load = array();
foreach ($tltids as $tltid) {
if (isset($statistics[$tltid])) {
$return[$tltid] = $statistics[$tltid];
}
else {
$tltids_to_load[] = $tltid;
}
}
if (!empty($tltids_to_load)) {
$query = db_select('tmgmt_local_task_item', 'tlti');
$query
->join('tmgmt_local_task', 'tlt', 'tlt.tltid = tlti.tltid');
$query
->join('tmgmt_job_item', 'tji', 'tji.tjiid = tlti.tjiid');
$query
->fields('tlt', array(
'tltid',
));
$query
->addExpression('SUM(tji.word_count)', 'word_count');
$query
->addExpression('SUM(tlti.count_untranslated)', 'count_untranslated');
$query
->addExpression('SUM(tlti.count_translated)', 'count_translated');
$query
->addExpression('SUM(tlti.count_completed)', 'count_completed');
$result = $query
->groupBy('tlt.tltid')
->condition('tlt.tltid', $tltids_to_load)
->execute();
foreach ($result as $row) {
$return[$row->tltid] = $statistics[$row->tltid] = $row;
}
}
return $return;
}
function tmgmt_local_task_statistic(TMGMTLocalTask $task, $key) {
$statistics = tmgmt_local_task_statistics_load(array(
$task->tltid,
));
if (isset($statistics[$task->tltid]->{$key})) {
return $statistics[$task->tltid]->{$key};
}
return 0;
}
function tmgmt_local_task_statuses() {
return $statuses = array(
TMGMT_LOCAL_TASK_STATUS_UNASSIGNED => t('Unassigned'),
TMGMT_LOCAL_TASK_STATUS_PENDING => t('Pending'),
TMGMT_LOCAL_TASK_STATUS_COMPLETED => t('Completed'),
TMGMT_LOCAL_TASK_STATUS_REJECTED => t('Rejected'),
TMGMT_LOCAL_TASK_STATUS_CLOSED => t('Closed'),
);
}
function tmgmt_local_task_item_statuses() {
return $statuses = array(
TMGMT_LOCAL_TASK_ITEM_STATUS_PENDING => t('Untranslated'),
TMGMT_LOCAL_TASK_ITEM_STATUS_COMPLETED => t('Translated'),
TMGMT_LOCAL_TASK_ITEM_STATUS_REJECTED => t('Rejected'),
TMGMT_LOCAL_TASK_ITEM_STATUS_CLOSED => t('Completed'),
);
}
function tmgmt_local_tasks_languages($tasks) {
$query = db_select('tmgmt_local_task', 't');
$query
->condition('tltid', $tasks);
$query
->join('tmgmt_job', 'j', 't.tjid = j.tjid');
$query
->fields('j', array(
'source_language',
'target_language',
));
$query
->groupBy('target_language');
$result = $query
->execute();
$languages = array();
foreach ($result as $row) {
if (empty($languages[$row->source_language]) || !in_array($row->target_language, $languages[$row->source_language])) {
$languages[$row->source_language][] = $row->target_language;
}
}
return $languages;
}
function tmgmt_local_get_translators_for_tasks($tasks) {
$translators = array();
foreach (tmgmt_local_tasks_languages($tasks) as $source_language => $target_languages) {
$translators[] = tmgmt_local_translators($source_language, $target_languages);
}
if (count($translators) > 1) {
return call_user_func_array('array_intersect', $translators);
}
elseif (count($translators) == 1) {
return array_shift($translators);
}
return array();
}
function tmgmt_local_tmgmt_job_item_update(TMGMTJobItem $job_item) {
if ($job_item
->isAccepted() && !$job_item->original
->isAccepted()) {
$tltiid = db_query('SELECT tltiid FROM {tmgmt_local_task_item} ti INNER JOIN {tmgmt_local_task} t ON ti.tltid = t.tltid WHERE t.tjid = :tjid AND ti.tjiid = :tjiid', array(
':tjid' => $job_item->tjid,
':tjiid' => $job_item->tjiid,
))
->fetchField();
if ($tltiid) {
$task_item = tmgmt_local_task_item_load($tltiid);
$task_item
->closed();
$task_item
->save();
$query = new EntityFieldQuery();
$unclosed_tasks = $query
->entityCondition('entity_type', 'tmgmt_local_task_item')
->propertyCondition('tltid', $task_item->tltid)
->propertyCondition('status', TMGMT_LOCAL_TASK_ITEM_STATUS_CLOSED, '<>')
->count()
->execute();
if ($unclosed_tasks == 0) {
$task = $task_item
->getTask();
$task
->setStatus(TMGMT_LOCAL_TASK_STATUS_CLOSED);
$task
->save();
}
}
}
}
function tmgmt_local_tmgmt_job_delete(TMGMTJob $job) {
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'tmgmt_local_task')
->propertyCondition('tjid', $job->tjid)
->execute();
if (!empty($result['tmgmt_local_task'])) {
entity_delete_multiple('tmgmt_local_task', array_keys($result['tmgmt_local_task']));
}
}
function tmgmt_local_field_access($op, $field, $entity_type, $entity = NULL, $account = NULL) {
if ($field['field_name'] == 'tmgmt_translation_skills' && $entity_type == 'user') {
$account = !empty($account) ? $account : $GLOBALS['user'];
if ($op == 'view') {
return TRUE;
}
if (!empty($entity) && $entity->uid == $account->uid) {
return user_access('provide translation services', $entity);
}
if (user_access('administer tmgmt', $account)) {
return TRUE;
}
return FALSE;
}
}
function tmgmt_local_field_attach_insert($entity_type, $entity) {
if ($entity_type != 'user' || !array_intersect_key(user_roles(TRUE, 'provide translation services'), $entity->roles)) {
return;
}
tmgmt_local_clear_languages_cache();
}
function tmgmt_local_field_attach_update($entity_type, $entity) {
if ($entity_type != 'user' || !array_intersect_key(user_roles(TRUE, 'provide translation services'), $entity->roles)) {
return;
}
tmgmt_local_clear_languages_cache();
}
function tmgmt_local_field_attach_delete($entity_type, $entity) {
if ($entity_type != 'user' || !array_intersect_key(user_roles(TRUE, 'provide translation services'), $entity->roles)) {
return;
}
tmgmt_local_clear_languages_cache();
}
function tmgmt_local_supported_language_pairs($source_language = NULL, $uids = array()) {
$language_pairs =& drupal_static(__FUNCTION__);
$cache_key = $source_language . '_' . implode('_', $uids);
if (isset($language_pairs[$cache_key])) {
return $language_pairs[$cache_key];
}
$language_pairs[$cache_key] = array();
foreach (tmgmt_local_capabilities($source_language, NULL, $uids) as $row) {
$pair_key = $row->tmgmt_translation_skills_language_from . '__' . $row->tmgmt_translation_skills_language_to;
$language_pairs[$cache_key][$pair_key] = array(
'source_language' => $row->tmgmt_translation_skills_language_from,
'target_language' => $row->tmgmt_translation_skills_language_to,
);
}
return $language_pairs[$cache_key];
}
function tmgmt_local_supported_target_languages($source_language, $uids = array()) {
$pairs = tmgmt_local_supported_language_pairs($source_language, $uids);
$supported_languages = array();
foreach ($pairs as $pair) {
$supported_languages[$pair['target_language']] = $pair['target_language'];
}
return $supported_languages;
}
function tmgmt_local_translators($source_language = NULL, array $target_languages = array()) {
$translators =& drupal_static(__FUNCTION__);
$key = $source_language . '_' . implode('_', $target_languages);
if (isset($translators[$key])) {
return $translators[$key];
}
$translators_capabilities = array();
foreach (tmgmt_local_capabilities($source_language) as $row) {
$translators_capabilities[$row->uid][] = $row->tmgmt_translation_skills_language_to;
}
$translators_uids = array();
foreach ($translators_capabilities as $uid => $translator_capabilities) {
if (!empty($target_languages) && count(array_diff($target_languages, $translator_capabilities)) > 0) {
continue;
}
$translators_uids[] = $uid;
}
$translators[$key] = array();
if (!empty($translators_uids)) {
foreach (user_load_multiple($translators_uids) as $account) {
$translators[$key][$account->uid] = entity_label('user', $account);
}
}
return $translators[$key];
}
function tmgmt_local_capabilities($source_language = NULL, $target_language = NULL, $uids = array()) {
$roles = tmgmt_local_translator_roles();
if (empty($roles)) {
return array();
}
$query = db_select('field_data_tmgmt_translation_skills', 'ts')
->fields('ts', array(
'tmgmt_translation_skills_language_from',
'tmgmt_translation_skills_language_to',
))
->condition('ts.deleted', 0)
->condition('ts.entity_type', 'user');
if ($source_language) {
$query
->condition('ts.tmgmt_translation_skills_language_from', $source_language);
}
if ($target_language) {
$query
->condition('ts.tmgmt_translation_skills_language_to', $target_language);
}
$query
->innerJoin('users', 'u', 'u.uid = ts.entity_id AND u.status = 1');
$query
->fields('u', array(
'uid',
'name',
'mail',
));
if (!empty($uids)) {
$query
->condition('u.uid', $uids);
}
if (!in_array('authenticated user', $roles)) {
$query
->leftJoin('users_roles', 'ur', "ur.uid = u.uid AND ur.rid IN (:roles)", array(
':roles' => array_keys($roles),
));
}
$query
->addTag('tmgmt_translation_combination');
return $query
->execute()
->fetchAll();
}
function tmgmt_local_translator_roles() {
return user_roles(TRUE, 'provide translation services');
}
function tmgmt_local_rules_action_info_alter(&$actions) {
if (isset($actions['component_rules_tmgmt_local_task_assign_to_me'])) {
$actions['component_rules_tmgmt_local_task_assign_to_me']['access callback'] = 'tmgmt_local_rules_component_access';
}
if (isset($actions['component_rules_tmgmt_local_task_assign'])) {
$actions['component_rules_tmgmt_local_task_assign']['access callback'] = 'tmgmt_local_rules_component_access';
}
}
function tmgmt_local_rules_component_access($type, $name) {
switch ($name) {
case 'component_rules_tmgmt_local_task_assign_to_me':
return user_access('provide translation services');
case 'component_rules_tmgmt_local_task_assign':
return user_access('administer translation tasks');
}
}