tmgmt.module 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. <?php
  2. /**
  3. * @file
  4. * Main module file for the Translation Management module.
  5. */
  6. /**
  7. * @addtogroup tmgmt_job
  8. * @{
  9. */
  10. /**
  11. * A new translation job.
  12. *
  13. * In the default user interface, jobs with this state are so called cart jobs.
  14. * Each user gets his cart jobs listed in a block and can check them out.
  15. */
  16. define('TMGMT_JOB_STATE_UNPROCESSED', 0);
  17. /**
  18. * A translation job that has been submitted to the translator.
  19. *
  20. * Translator plugins are responsible for setting this state in their
  21. * implementation of
  22. * TMGMTTranslatorPluginControllerInterface::requestTranslation().
  23. */
  24. define('TMGMT_JOB_STATE_ACTIVE', 1);
  25. /**
  26. * A translation job that has been rejected by the translator.
  27. *
  28. * The translator plugin can use this state if the job has been actively
  29. * rejected. However, this should be avoided by doing the necessary checks
  30. * in the canTranslate() method and in the job configuration settings.
  31. *
  32. * A rejected job can be re-submitted.
  33. */
  34. define('TMGMT_JOB_STATE_REJECTED', 2);
  35. /**
  36. * The translation has been accepted and the job is finished.
  37. *
  38. * Once the job has been accepted, the source plugins are called to update their
  39. * sources with the translated data.
  40. */
  41. define('TMGMT_JOB_STATE_ACCEPTED', 3);
  42. /**
  43. * The translation job has been aborted.
  44. *
  45. * A job can be aborted at any time. If he is currently in the submitted state
  46. * the translator plugin is asked if this translation can be aborted and needs
  47. * to confirm it by returning TRUE in abortTranslation().
  48. */
  49. define('TMGMT_JOB_STATE_ABORTED', 4);
  50. /**
  51. * The translation job has been finished.
  52. *
  53. * A job is marked as 'finished' after every single attached job item has been
  54. * reviewed, accepted and saved.
  55. */
  56. define('TMGMT_JOB_STATE_FINISHED', 5);
  57. /**
  58. * The translation job item is active and waiting to be translated.
  59. *
  60. * A job item is marked as 'active' until every translatable piece of text in
  61. * the job item has been translated and cached on the job item entity.
  62. */
  63. define('TMGMT_JOB_ITEM_STATE_ACTIVE', 1);
  64. /**
  65. * The translation job item needs to be reviewed.
  66. *
  67. * A job item is marked as 'needs review' after every single piece of text in
  68. * the job item has been translated by the translation provider. After the
  69. * review procedure is finished the job item can be accepted and saved.
  70. */
  71. define('TMGMT_JOB_ITEM_STATE_REVIEW', 2);
  72. /**
  73. * The translation job item has been reviewed and accepted.
  74. *
  75. * After reviewing a job item it can be accepted by the reviewer. Once the user
  76. * has accepted the job item, the translated data will be propagated to the
  77. * source controller which will also take care of flagging the job item as
  78. * 'accepted' if the translated object could be saved successfully.
  79. */
  80. define('TMGMT_JOB_ITEM_STATE_ACCEPTED', 3);
  81. /**
  82. * The translation process of the job item is aborted.
  83. */
  84. define('TMGMT_JOB_ITEM_STATE_ABORTED', 4);
  85. /**
  86. * The translation data item has not been translated.
  87. */
  88. define('TMGMT_DATA_ITEM_STATE_PENDING', 0);
  89. /**
  90. * The translation data item has been reviewed.
  91. */
  92. define('TMGMT_DATA_ITEM_STATE_REVIEWED', 1);
  93. /**
  94. * The translation data item has been translated.
  95. */
  96. define('TMGMT_DATA_ITEM_STATE_TRANSLATED', 2);
  97. /**
  98. * The translation data item has been reviewed.
  99. */
  100. define('TMGMT_DATA_ITEM_STATE_ACCEPTED', 3);
  101. /**
  102. * Maximum length of a job or job item label.
  103. */
  104. define('TMGMT_JOB_LABEL_MAX_LENGTH', 128);
  105. /**
  106. * @} End of "addtogroup tmgmt_job".
  107. */
  108. /**
  109. * String used to delimit flattened array keys.
  110. */
  111. define('TMGMT_ARRAY_DELIMITER', '][');
  112. /**
  113. * Implements hook_entity_info().
  114. */
  115. function tmgmt_entity_info() {
  116. $info['tmgmt_job'] = array(
  117. 'label' => t('Translation Management Job'),
  118. 'module' => 'tmgmt',
  119. 'controller class' => 'TMGMTJobController',
  120. 'metadata controller class' => 'TMGMTJobMetadataController',
  121. 'views controller class' => 'TMGMTJobViewsController',
  122. 'entity class' => 'TMGMTJob',
  123. 'base table' => 'tmgmt_job',
  124. 'uri callback' => 'entity_class_uri',
  125. 'label callback' => 'entity_class_label',
  126. 'access callback' => 'tmgmt_job_access',
  127. 'entity keys' => array(
  128. 'id' => 'tjid',
  129. ),
  130. );
  131. $info['tmgmt_job_item'] = array(
  132. 'label' => t('Translation Management Job Item'),
  133. 'module' => 'tmgmt',
  134. 'controller class' => 'TMGMTJobItemController',
  135. 'metadata controller class' => 'TMGMTJobItemMetadataController',
  136. 'views controller class' => 'TMGMTJobItemViewsController',
  137. 'entity class' => 'TMGMTJobItem',
  138. 'base table' => 'tmgmt_job_item',
  139. 'label callback' => 'entity_class_label',
  140. 'uri callback' => 'entity_class_uri',
  141. 'access callback' => 'tmgmt_job_item_access',
  142. 'entity keys' => array(
  143. 'id' => 'tjiid',
  144. ),
  145. );
  146. $info['tmgmt_message'] = array(
  147. 'label' => t('Translation Management Message'),
  148. 'module' => 'tmgmt',
  149. 'controller class' => 'EntityAPIController',
  150. 'metadata controller class' => 'TMGMTMessageMetadataController',
  151. 'views controller class' => 'TMGMTMessageViewsController',
  152. 'entity class' => 'TMGMTMessage',
  153. 'base table' => 'tmgmt_message',
  154. 'label callback' => 'entity_class_label',
  155. 'access callback' => 'tmgmt_message_access',
  156. 'entity keys' => array(
  157. 'id' => 'mid',
  158. ),
  159. );
  160. $info['tmgmt_translator'] = array(
  161. 'label' => t('Translation Management Translator'),
  162. 'module' => 'tmgmt',
  163. 'controller class' => 'TMGMTTranslatorController',
  164. 'metadata controller class' => 'TMGMTTranslatorMetadataController',
  165. 'views controller class' => 'EntityDefaultViewsController',
  166. 'entity class' => 'TMGMTTranslator',
  167. 'base table' => 'tmgmt_translator',
  168. 'exportable' => TRUE,
  169. 'access callback' => 'tmgmt_translator_access',
  170. 'entity keys' => array(
  171. 'id' => 'tid',
  172. 'name' => 'name',
  173. 'label' => 'label',
  174. ),
  175. );
  176. // Make use of the entity cache module if it is enabled.
  177. if (module_exists('entitycache')) {
  178. $info['tmgmt_translator']['entity cache'] = TRUE;
  179. $info['tmgmt_translator']['field cache'] = FALSE;
  180. }
  181. $info['tmgmt_remote'] = array(
  182. 'label' => t('Remote job mapping'),
  183. 'module' => 'tmgmt',
  184. 'controller class' => 'TMGMTRemoteController',
  185. 'entity class' => 'TMGMTRemote',
  186. 'base table' => 'tmgmt_remote',
  187. 'uri callback' => 'entity_class_uri',
  188. 'label callback' => 'entity_class_label',
  189. 'access callback' => 'tmgmt_remote_access',
  190. 'entity keys' => array(
  191. 'id' => 'trid',
  192. ),
  193. );
  194. return $info;
  195. }
  196. /**
  197. * Implements hook_permission().
  198. */
  199. function tmgmt_permission() {
  200. $perms['administer tmgmt'] = array(
  201. 'title' => t('Administer translation management'),
  202. );
  203. $perms['create translation jobs'] = array(
  204. 'title' => t('Create translation jobs'),
  205. );
  206. $perms['submit translation jobs'] = array(
  207. 'title' => t('Submit translation jobs'),
  208. );
  209. $perms['accept translation jobs'] = array(
  210. 'title' => t('Accept and reject translation jobs'),
  211. );
  212. return $perms;
  213. }
  214. /**
  215. * Implements hook_modules_installed().
  216. */
  217. function tmgmt_modules_installed($modules) {
  218. foreach (tmgmt_translator_plugin_info() as $key => $info) {
  219. // Check if this translator plugin has been added by one of the recently
  220. // installed modules and doesn't prevent auto creation.
  221. if ((!isset($info['auto create']) || $info['auto create'] == TRUE) && in_array($info['module'], $modules)) {
  222. tmgmt_translator_auto_create($key);
  223. }
  224. }
  225. }
  226. /**
  227. * Implements hook_flush_caches().
  228. */
  229. function tmgmt_flush_caches() {
  230. return array('cache_tmgmt');
  231. }
  232. /**
  233. * Implements hook_cron().
  234. */
  235. function tmgmt_cron() {
  236. $offset = variable_get('tmgmt_purge_finished', '_never');
  237. if ($offset != '_never') {
  238. // Delete all finished translation jobs that haven't been changed for a
  239. // time span longer than the given offset.
  240. $query = new EntityFieldQuery();
  241. $result = $query->entityCondition('entity_type', 'tmgmt_job')
  242. ->propertyCondition('state', TMGMT_JOB_STATE_FINISHED)
  243. ->propertyCondition('changed', REQUEST_TIME - $offset, '<=')
  244. ->execute();
  245. if (!empty($result['tmgmt_job'])) {
  246. $controller = entity_get_controller('tmgmt_job');
  247. // Since the entity controller handles the deletion of the attached
  248. // entities (messages, job items) we just need to invoke it directly.
  249. $controller->delete(array_keys($result['tmgmt_job']));
  250. }
  251. }
  252. }
  253. /**
  254. * Implements hook_views_api().
  255. */
  256. function tmgmt_views_api() {
  257. return array(
  258. 'api' => 3.0,
  259. 'path' => drupal_get_path('module', 'tmgmt') . '/views',
  260. );
  261. }
  262. /**
  263. * Returns an array of languages that are available for translation.
  264. *
  265. * @return array
  266. * An array of languages in ISO format.
  267. */
  268. function tmgmt_available_languages($exclude = array()) {
  269. $languages = entity_metadata_language_list();
  270. // Remove LANGUAGE_NONE and the language in $exclude from the list of
  271. // available languages and then apply a filter that only leaves the supported
  272. // target languages on the list.
  273. unset($languages[LANGUAGE_NONE]);
  274. foreach ($exclude as $item) {
  275. unset($languages[$item]);
  276. }
  277. return $languages;
  278. }
  279. /**
  280. * Returns the label of a language.
  281. *
  282. * @param $language
  283. * A language in ISO format.
  284. * @return string
  285. * The label of the language or an empty string if the language or its label
  286. * are not defined.
  287. */
  288. function tmgmt_language_label($language) {
  289. $languages = entity_metadata_language_list();
  290. if (!empty($languages[$language])) {
  291. return $languages[$language];
  292. }
  293. return '';
  294. }
  295. /**
  296. * @addtogroup tmgmt_job
  297. * @{
  298. */
  299. /**
  300. * Loads a translation job.
  301. *
  302. * @param int $tjid
  303. * Translation job id.
  304. *
  305. * @return TMGMTJob
  306. * Loaded translation job entity.
  307. */
  308. function tmgmt_job_load($tjid) {
  309. $jobs = tmgmt_job_load_multiple(array($tjid), array());
  310. return $jobs ? reset($jobs) : FALSE;
  311. }
  312. /**
  313. * Loads translation jobs.
  314. */
  315. function tmgmt_job_load_multiple(array $tjids = array(), $conditions = array()) {
  316. return entity_load('tmgmt_job', $tjids, $conditions);
  317. }
  318. /**
  319. * Loads active job entities that have a job item with the identifiers.
  320. *
  321. * @param $plugin
  322. * The source plugin.
  323. * @param $item_type
  324. * The source item type.
  325. * @param $item_id
  326. * The source item id.
  327. * @param string $source_language
  328. * The source language of the item.
  329. *
  330. * @return array
  331. * An array of job entities.
  332. */
  333. function tmgmt_job_item_load_latest($plugin, $item_type, $item_id, $source_language) {
  334. $query = db_select('tmgmt_job_item', 'tji');
  335. $query->innerJoin('tmgmt_job', 'tj', 'tj.tjid = tji.tjid');
  336. $result = $query->condition('tj.source_language', $source_language)
  337. // Only query for jobs that are currently active.
  338. ->condition('tj.state', array(TMGMT_JOB_STATE_UNPROCESSED, TMGMT_JOB_STATE_ACTIVE))
  339. // And only query for job items that are not yet finished.
  340. ->condition('tji.state', TMGMT_JOB_ITEM_STATE_ACCEPTED, '<>')
  341. ->condition('tji.plugin', $plugin)
  342. ->condition('tji.item_type', $item_type)
  343. ->condition('tji.item_id', $item_id)
  344. ->fields('tji', array('tjiid'))
  345. ->fields('tj', array('target_language'))
  346. ->orderBy('tji.changed', 'DESC')
  347. ->groupBy('tj.target_language')
  348. ->groupBy('tji.tjiid')
  349. ->groupBy('tji.changed')
  350. ->execute();
  351. if ($items = $result->fetchAllKeyed()) {
  352. $return = array();
  353. foreach (tmgmt_job_item_load_multiple(array_keys($items)) as $key => $item) {
  354. $return[$items[$key]] = $item;
  355. }
  356. return $return;
  357. }
  358. return FALSE;
  359. }
  360. /**
  361. * Loads all latest job entities that have a job item with the identifiers.
  362. *
  363. * @param $plugin
  364. * The source plugin.
  365. * @param $item_type
  366. * The source item type.
  367. * @param $item_id
  368. * The source item id.
  369. * @param string $source_language
  370. * The source language of the item.
  371. *
  372. * @return array
  373. * An array of job entities.
  374. */
  375. function tmgmt_job_item_load_all_latest($plugin, $item_type, $item_id, $source_language) {
  376. $query = db_select('tmgmt_job_item', 'tji');
  377. $query->innerJoin('tmgmt_job', 'tj', 'tj.tjid = tji.tjid');
  378. $result = $query->condition('tj.source_language', $source_language)
  379. ->condition('tji.state', TMGMT_JOB_ITEM_STATE_ACCEPTED, '<>')
  380. ->condition('tji.plugin', $plugin)
  381. ->condition('tji.item_type', $item_type)
  382. ->condition('tji.item_id', $item_id)
  383. ->fields('tji', array('tjiid'))
  384. ->fields('tj', array('target_language'))
  385. ->orderBy('tji.changed', 'DESC')
  386. ->groupBy('tj.target_language')
  387. ->groupBy('tji.tjiid')
  388. ->execute();
  389. if ($items = $result->fetchAllKeyed()) {
  390. $return = array();
  391. foreach (tmgmt_job_item_load_multiple(array_keys($items)) as $key => $item) {
  392. $return[$items[$key]] = $item;
  393. }
  394. return $return;
  395. }
  396. return FALSE;
  397. }
  398. /**
  399. * Returns a job which matches the requested source- and target language by
  400. * user. If no job exists, a new job object will be created.
  401. *
  402. * @param $source_language
  403. * The source language from which should be translated.
  404. * @param $target_language
  405. * The target language into which should be translated.
  406. * @param $account
  407. * (Optional) A user object. Defaults to the currently logged in user.
  408. *
  409. * @return TMGMTJob
  410. * The job entity.
  411. */
  412. function tmgmt_job_match_item($source_language, $target_language, $account = NULL) {
  413. $account = isset($account) ? $account : $GLOBALS['user'];
  414. $query = new EntityFieldQuery();
  415. $result = $query->entityCondition('entity_type', 'tmgmt_job')
  416. ->propertyCondition('source_language', $source_language)
  417. ->propertyCondition('target_language', $target_language)
  418. ->propertyCondition('uid', $account->uid)
  419. ->propertyCondition('state', TMGMT_JOB_STATE_UNPROCESSED)
  420. ->execute();
  421. if (!empty($result['tmgmt_job'])) {
  422. $job = reset($result['tmgmt_job']);
  423. return tmgmt_job_load($job->tjid);
  424. }
  425. return tmgmt_job_create($source_language, $target_language, $account->uid);
  426. }
  427. /**
  428. * Checks whether a job is finished by querying the job item table for
  429. * unfinished job items.
  430. *
  431. * @param $tjid
  432. * The identifier of the job.
  433. * @return bool
  434. * TRUE if the job is finished, FALSE otherwise.
  435. */
  436. function tmgmt_job_check_finished($tjid) {
  437. $query = new EntityFieldQuery();
  438. return !(boolean) $query->entityCondition('entity_type', 'tmgmt_job_item')
  439. ->propertyCondition('tjid', $tjid)
  440. ->propertyCondition('state', TMGMT_JOB_ITEM_STATE_ACCEPTED, '<>')
  441. ->range(0, 1)
  442. ->count()
  443. ->execute();
  444. }
  445. /**
  446. * Creates a translation job.
  447. *
  448. * @param $source_language
  449. * The source language from which should be translated.
  450. * @param $target_language
  451. * The target language into which should be translated.
  452. * @param $values
  453. * (Optional) An array of additional entity values.
  454. *
  455. * @return TMGMTJob
  456. * The job entity.
  457. */
  458. function tmgmt_job_create($source_language, $target_language, $uid = NULL, array $values = array()) {
  459. return entity_create('tmgmt_job', array_merge($values, array(
  460. 'source_language' => $source_language,
  461. 'target_language' => $target_language,
  462. 'uid' => $uid,
  463. )));
  464. }
  465. /**
  466. * Access callback for the job entity.
  467. *
  468. *
  469. * @param $op
  470. * The operation being performed.
  471. * @param $item
  472. * (Optional) A TMGMTJob entity to check access for. If no entity is given, it
  473. * will be determined whether access is allowed for all entities.
  474. * @param $account
  475. * (Optional) The user to check for. Leave it to NULL to check for the global
  476. * user.
  477. *
  478. * @return boolean
  479. * TRUE if access is allowed, FALSE otherwise.
  480. */
  481. function tmgmt_job_access($op, $job = NULL, $account = NULL) {
  482. if (user_access('administer tmgmt', $account)) {
  483. // Administrators can do everything.
  484. return TRUE;
  485. }
  486. switch ($op) {
  487. case 'create':
  488. return user_access('create translation jobs', $account);
  489. break;
  490. case 'view':
  491. case 'update':
  492. return user_access('create translation jobs', $account) || user_access('submit translation jobs', $account) || user_access('accept translation jobs', $account);
  493. break;
  494. case 'delete':
  495. // Only administrators can delete jobs.
  496. return FALSE;
  497. break;
  498. // Custom operations.
  499. case 'submit':
  500. return user_access('submit translation jobs');
  501. break;
  502. case 'abort':
  503. case 'resubmit':
  504. return user_access('submit translation jobs');
  505. break;
  506. case 'accept':
  507. return user_access('accept translation jobs');
  508. break;
  509. }
  510. }
  511. /**
  512. * Access callback for tmgmt remote entity.
  513. */
  514. function tmgmt_remote_access($op, $tmgmt_remote = NULL, $account = NULL) {
  515. return user_access('administer tmgmt', $account);
  516. }
  517. /**
  518. * Loads an array with the word and status statistics of a job.
  519. *
  520. * @param $tjids
  521. * An array of job ids.
  522. *
  523. * @return
  524. * An array of objects with the keys word_count, count_pending,
  525. * count_accepted, count_reviewed and count_translated.
  526. */
  527. function tmgmt_job_statistics_load(array $tjids) {
  528. $statistics = &drupal_static(__FUNCTION__, array());
  529. // First try to get the values from the cache.
  530. $return = array();
  531. $tjids_to_load = array();
  532. foreach ($tjids as $tjid) {
  533. if (isset($statistics[$tjid])) {
  534. // Info exists in cache, get it from there.
  535. $return[$tjid] = $statistics[$tjid];
  536. }
  537. else {
  538. // Info doesn't exist in cache, add job to the list that needs to be
  539. // fetched.
  540. $tjids_to_load[] = $tjid;
  541. }
  542. }
  543. // If there are remaining jobs, build a query to fetch them.
  544. if (!empty($tjids_to_load)) {
  545. // Build the query to fetch the statistics.
  546. $query = db_select('tmgmt_job_item', 'tji')
  547. ->fields('tji', array('tjid'));
  548. $query->addExpression('SUM(word_count)', 'word_count');
  549. $query->addExpression('SUM(count_accepted)', 'count_accepted');
  550. $query->addExpression('SUM(count_reviewed)', 'count_reviewed');
  551. $query->addExpression('SUM(count_pending)', 'count_pending');
  552. $query->addExpression('SUM(count_translated)', 'count_translated');
  553. $result = $query->groupBy('tjid')
  554. ->condition('tjid', $tjids_to_load)
  555. ->execute();
  556. foreach ($result as $row) {
  557. $return[$row->tjid] = $statistics[$row->tjid] = $row;
  558. }
  559. }
  560. return $return;
  561. }
  562. /**
  563. * Returns a specific statistic of a job.
  564. *
  565. * @param $job
  566. * The translation job entity.
  567. * @param $key
  568. * One of word_count, count_pending, count_accepted, count_reviewed and
  569. * count_translated.
  570. *
  571. * @return
  572. * The requested information as an integer.
  573. */
  574. function tmgmt_job_statistic(TMGMTJob $job, $key) {
  575. $statistics = tmgmt_job_statistics_load(array($job->tjid));
  576. if (isset($statistics[$job->tjid]->$key)) {
  577. return $statistics[$job->tjid]->$key;
  578. }
  579. return 0;
  580. }
  581. /**
  582. * Access callback for the job item entity.
  583. *
  584. * @param $op
  585. * The operation being performed.
  586. * @param $item
  587. * (Optional) A TMGMTJobItem entity to check access for. If no entity is
  588. * given, it will be determined whether access is allowed for all entities.
  589. * @param $account
  590. * (Optional) The user to check for. Leave it to NULL to check for the global
  591. * user.
  592. *
  593. * @return boolean
  594. * TRUE if access is allowed, FALSE otherwise.
  595. */
  596. function tmgmt_job_item_access($op, TMGMTJobItem $item = NULL, $account = NULL) {
  597. // There are no item specific permissions yet.
  598. return tmgmt_job_access($op, $item ? $item->getJob() : NULL, $account);
  599. }
  600. /**
  601. * Access callback wrapper for reviewing a job item entity.
  602. *
  603. * @param TMGMTJobItem $item
  604. * The job item to check access for.
  605. * @param $account
  606. * (Optional) The user to check for. Leave it to NULL to check for the global
  607. * user.
  608. *
  609. * @return boolean
  610. * TRUE if access is allowed, FALSE otherwise.
  611. */
  612. function tmgmt_job_item_review_access(TMGMTJobItem $item, $account = NULL) {
  613. if ($item->isNeedsReview() && $item->getSourceController() && $item->getTranslatorController()) {
  614. return tmgmt_job_item_access('accept', $item, $account);
  615. }
  616. return FALSE;
  617. }
  618. /**
  619. * Access callback for the job message entity.
  620. *
  621. * @param $op
  622. * The operation being performed.
  623. * @param $item
  624. * (Optional) A TMGMTJobMessage entity to check access for. If no entity is
  625. * given, it will be determined whether access is allowed for all entities.
  626. * @param $account
  627. * (Optional) The user to check for. Leave it to NULL to check for the global
  628. * user.
  629. *
  630. * @return boolean
  631. * TRUE if access is allowed, FALSE otherwise.
  632. */
  633. function tmgmt_message_access($op, TMGMTMessage $message = NULL, $account = NULL) {
  634. // All users that can see jobs can see messages as well.
  635. if ($op == 'view') {
  636. $job = NULL;
  637. if ($message) {
  638. $job = $message->getJob();
  639. }
  640. return tmgmt_job_access('view', $job, $account);
  641. }
  642. // Changing or creating messages is only possible for admins.
  643. return user_access('administer tmgmt');
  644. }
  645. /**
  646. * Static method to retrieve a labeled list of all available states.
  647. *
  648. * @return array
  649. * A list of all available states.
  650. */
  651. function tmgmt_job_states() {
  652. return array(
  653. TMGMT_JOB_STATE_UNPROCESSED => t('Unprocessed'),
  654. TMGMT_JOB_STATE_ACTIVE => t('Active'),
  655. TMGMT_JOB_STATE_REJECTED => t('Rejected'),
  656. TMGMT_JOB_STATE_ABORTED => t('Aborted'),
  657. TMGMT_JOB_STATE_FINISHED => t('Finished'),
  658. );
  659. }
  660. /**
  661. * Static method to retrieve a labeled list of all available states.
  662. *
  663. * @return array
  664. * A list of all available states.
  665. */
  666. function tmgmt_job_item_states() {
  667. return array(
  668. TMGMT_JOB_ITEM_STATE_ACTIVE => t('In progress'),
  669. TMGMT_JOB_ITEM_STATE_REVIEW => t('Needs review'),
  670. TMGMT_JOB_ITEM_STATE_ACCEPTED => t('Accepted'),
  671. TMGMT_JOB_ITEM_STATE_ABORTED => t('Aborted'),
  672. );
  673. }
  674. /**
  675. * Loads a translation job item.
  676. *
  677. * @param $tjiid
  678. * A job item id.
  679. *
  680. * @return TMGMTJobItem
  681. * The loaded job item or FALSE if the query returned no results.
  682. */
  683. function tmgmt_job_item_load($tjiid) {
  684. $jobs = tmgmt_job_item_load_multiple(array($tjiid), array());
  685. return $jobs ? reset($jobs) : FALSE;
  686. }
  687. /**
  688. * Loads translation job items.
  689. *
  690. * @param $tjiids
  691. * An array of job item ids.
  692. * @param $conditions
  693. * An array of additional conditions.
  694. *
  695. * @return TMGMTJobItem[]
  696. * An array of job item entities or an empty array if the query returned no
  697. * results.
  698. */
  699. function tmgmt_job_item_load_multiple($tjiids = array(), $conditions = array()) {
  700. return entity_load('tmgmt_job_item', $tjiids, $conditions);
  701. }
  702. /**
  703. * Creates a translation job item.
  704. *
  705. * @param $plugin
  706. * The plugin name.
  707. * @param $item_type
  708. * The source item type.
  709. * @param $item_id
  710. * The source item id.
  711. * @param $values
  712. * (Optional) An array of additional entity values to be set.
  713. *
  714. * @return TMGMTJobItem
  715. * The created, not yet saved, job item entity.
  716. */
  717. function tmgmt_job_item_create($plugin, $item_type, $item_id, array $values = array()) {
  718. return entity_create('tmgmt_job_item', array_merge($values, array(
  719. 'plugin' => $plugin,
  720. 'item_type' => $item_type,
  721. 'item_id' => $item_id,
  722. )));
  723. }
  724. /**
  725. * Loads a translation job message.
  726. *
  727. * @param $mid
  728. * A job message id.
  729. *
  730. * @return TMGMTMessage
  731. * A job message entity or FALSE if the query didn't yield any results.
  732. */
  733. function tmgmt_message_load($mid) {
  734. // Avoid collision with the message module because this looks like the module
  735. // implements hook_ENTITY_TYPE_load() for message.
  736. if (!is_array($mid)) {
  737. $jobs = tmgmt_message_load_multiple(array($mid));
  738. return $jobs ? reset($jobs) : FALSE;
  739. }
  740. }
  741. /**
  742. * Loads translation job messages.
  743. */
  744. function tmgmt_message_load_multiple($mids = array(), $conditions = array()) {
  745. return entity_load('tmgmt_message', $mids, $conditions);
  746. }
  747. /**
  748. * Creates a translation job message.
  749. *
  750. * @param $message
  751. * (Optional) The message to be saved.
  752. * @param $variables
  753. * (Optional) An array of variables to replace in the message on display.
  754. * @param $values
  755. * (Optional) An array of additional entity values to be set.
  756. *
  757. * @return TMGMTJobItem
  758. * The created, not yet saved, job item entity.
  759. */
  760. function tmgmt_message_create($message = '', $variables = array(), $values = array()) {
  761. return entity_create('tmgmt_message', array_merge($values, array(
  762. 'message' => $message,
  763. 'variables' => $variables,
  764. )));
  765. }
  766. /**
  767. * @} End of "addtogroup tmgmt_job".
  768. */
  769. /**
  770. * @addtogroup tmgmt_translator
  771. * @{
  772. */
  773. /**
  774. * Access callback for the translator entity.
  775. */
  776. function tmgmt_translator_access($op, TMGMTTranslator $translator = NULL, $account = NULL) {
  777. if (isset($translator) && !$translator->getController()) {
  778. return FALSE;
  779. }
  780. // Only administrators are allowed to manage translator entities.
  781. return user_access('administer tmgmt', $account);
  782. }
  783. /**
  784. * Checks whether a translator entity with the supplied name already exists.
  785. *
  786. * We can't use entity_load or any of its wrapper functions for that as our
  787. * translator entity controller filters out broken translator entities (e.g. if
  788. * the translator plugin of the translator entity doesn't exist (anymore).
  789. *
  790. * @param $name
  791. * The machine-readable name of the translator entity that we are trying to
  792. * save.
  793. *
  794. * @return boolean
  795. * TRUE if a translator entity with the same machine-readable name already
  796. * exists FALSE otherwise.
  797. */
  798. function tmgmt_translator_exists($name) {
  799. $query = new EntityFieldQuery();
  800. return (boolean) $query->entityCondition('entity_type', 'tmgmt_translator')
  801. ->propertyCondition('name', $name)
  802. ->count()
  803. ->range(0, 1)
  804. ->execute();
  805. }
  806. /**
  807. * Loads a translator based on the name.
  808. *
  809. * @param $name
  810. * The machine-readable name of the translator entity to load.
  811. *
  812. * @return TMGMTTranslator
  813. * A translator entity.
  814. */
  815. function tmgmt_translator_load($name) {
  816. $translators = entity_load_multiple_by_name('tmgmt_translator', array($name));
  817. return $translators ? reset($translators) : FALSE;
  818. }
  819. /**
  820. * Loads multiple translators based on their name.
  821. *
  822. * @param $names
  823. * (Optional) An array of machine-readable names of the translator entities to
  824. * load or FALSE to load all available translator entities.
  825. *
  826. * @return array
  827. * An array of translators with the machine-readable name of the translators
  828. * as array keys.
  829. */
  830. function tmgmt_translator_load_multiple($names = array()) {
  831. return entity_load_multiple_by_name('tmgmt_translator', $names);
  832. }
  833. /**
  834. * Loads all translators that are available and, if a translation job is given,
  835. * support translations for that job with its current configuration.
  836. *
  837. * @param TMGMTJob $job
  838. * (Optional) A translation job.
  839. *
  840. * @return array
  841. * An array of translators with the machine-readable name of the translators
  842. * as array keys.
  843. */
  844. function tmgmt_translator_load_available($job) {
  845. $translators = tmgmt_translator_load_multiple(FALSE);
  846. foreach ($translators as $name => $translator) {
  847. if (!$translator->isAvailable() || (isset($job) && !$translator->canTranslate($job))) {
  848. unset($translators[$name]);
  849. }
  850. }
  851. return $translators;
  852. }
  853. /**
  854. * Checks whether a translator with a certain name is busy and therefore can't
  855. * be modified or deleted. A translator is considered 'busy' if there are jobs
  856. * attached to it that are in an active state.
  857. *
  858. * @param $translator
  859. * The machine-readable name of a translator.
  860. *
  861. * @return boolean
  862. * TRUE if the translator is busy, FALSE otherwise.
  863. */
  864. function tmgmt_translator_busy($translator) {
  865. $query = new EntityFieldQuery();
  866. return (boolean) $query->entityCondition('entity_type', 'tmgmt_job')
  867. ->propertyCondition('state', TMGMT_JOB_STATE_ACTIVE)
  868. ->propertyCondition('translator', $translator)
  869. ->range(0, 1)
  870. ->count()
  871. ->execute();
  872. }
  873. /**
  874. * Creates a translator entity.
  875. *
  876. * @param $plugin
  877. * The plugin of the translator.
  878. * @param $name
  879. * The machine-readable name of the translator.
  880. * @param $label
  881. * The label of the translator.
  882. * @param $description
  883. * (Optional) The description of the translator. Defaults to an empty string.
  884. * @param $settings
  885. * (Optional) An array of settings for the translator.
  886. * @param $values
  887. * (Optional) Array of additional entity values.
  888. *
  889. * @return TMGMTTranslator
  890. * The created, not yet saved, translator entity.
  891. */
  892. function tmgmt_translator_create($plugin, $name, $label, $description = '', $settings = array(), $values = array()) {
  893. return entity_create('tmgmt_translator', array_merge($values, array(
  894. 'plugin' => $plugin,
  895. 'name' => $name,
  896. 'label' => $label,
  897. 'description' => $description,
  898. 'settings' => $settings,
  899. )));
  900. }
  901. /**
  902. * Auto creates a translator from a translator plugin definition.
  903. *
  904. * @param $plugin
  905. * The machine-readable name of a translator plugin.
  906. */
  907. function tmgmt_translator_auto_create($plugin) {
  908. if ($info = tmgmt_translator_plugin_info($plugin)) {
  909. if (!tmgmt_translator_exists($plugin)) {
  910. $label = $info['label'] . ' (auto created)';
  911. $translator = tmgmt_translator_create($plugin, $plugin, $label, $info['description']);
  912. // Append some default settings from the translator plugin definition.
  913. $translator->settings = $translator->getController()->defaultSettings();
  914. $translator->save();
  915. }
  916. }
  917. }
  918. /**
  919. * Determines all available service plugins.
  920. *
  921. * @param $plugin
  922. * (Optional) The machine-readable name of a service plugin.
  923. *
  924. * @return array
  925. * An array of translator plugin definitions.
  926. */
  927. function tmgmt_translator_plugin_info($plugin = NULL) {
  928. return _tmgmt_plugin_info('translator', $plugin);
  929. }
  930. /**
  931. * Determines the controller class for a given service plugin.
  932. *
  933. * @param $plugin
  934. * (Optional) The machine-readable name of a service plugin.
  935. *
  936. * @return array|TMGMTTranslatorPluginControllerInterface
  937. * - If the translator exists the controller object for the given source plugin
  938. * or an array containing all available translator plugin controller objects
  939. * if no plugin name was given.
  940. * - Array of existing Translators if a translator with given name does not
  941. * exists.
  942. */
  943. function tmgmt_translator_plugin_controller($plugin = NULL) {
  944. return _tmgmt_plugin_controller('translator', $plugin);
  945. }
  946. /**
  947. * Get the ui controller class for a given translator plugin.
  948. *
  949. * @param $plugin
  950. * (Optional) The machine-readable name of a translator plugin.
  951. *
  952. * @return TMGMTTranslatorUIControllerInterface
  953. * The ui controller object for the given translator plugin or an array
  954. * containing all available translator plugin controller objects if no plugin
  955. * name was given.
  956. */
  957. function tmgmt_translator_ui_controller($plugin = NULL) {
  958. return _tmgmt_plugin_controller('translator', $plugin, 'ui', 'TMGMTDefaultTranslatorUIController');
  959. }
  960. /**
  961. * Returns an array of all available translator plugins with the labels as
  962. * values and the machine-readable name as the key.
  963. *
  964. * @return array
  965. * An array of the labels of all available plugins.
  966. */
  967. function tmgmt_translator_plugin_labels() {
  968. return _tmgmt_plugin_labels('translator');
  969. }
  970. /**
  971. * Returns a list of all available translator labels.
  972. *
  973. * @return array
  974. * An array containing all available translator labels.
  975. */
  976. function tmgmt_translator_labels() {
  977. $labels = array();
  978. foreach (tmgmt_translator_load_multiple(FALSE) as $translator) {
  979. $labels[$translator->name] = $translator->label();
  980. }
  981. return $labels;
  982. }
  983. /**
  984. * Returns a list of flagged translator labels. If a translator is not available
  985. * it will be suffixed with a short text explaining why it is not available.
  986. * This can either be because the configuration of the passed job is not
  987. * supported or because the translator service can't be reached.
  988. *
  989. * @param TMGMTJob $job
  990. * (Optional) A translation job.
  991. *
  992. * @return array
  993. * An array of flagged translator labels.
  994. */
  995. function tmgmt_translator_labels_flagged($job = NULL) {
  996. $labels = array();
  997. foreach (tmgmt_translator_load_multiple(FALSE) as $translator) {
  998. if (!$translator->isAvailable()) {
  999. $labels[$translator->name] = t('@label (not available)', array('@label' => $translator->label()));
  1000. }
  1001. elseif (isset($job) && !$translator->canTranslate($job)) {
  1002. $labels[$translator->name] = t('@label (unsupported)', array('@label' => $translator->label()));
  1003. }
  1004. else {
  1005. $labels[$translator->name] = $translator->label();
  1006. }
  1007. }
  1008. return $labels;
  1009. }
  1010. /**
  1011. * Determines if the translator plugin supports remote language mappings.
  1012. *
  1013. * @param TMGMTTranslator $translator
  1014. * Translator entity.
  1015. *
  1016. * @return bool
  1017. * In case translator does not explicitly state that it does not provide the
  1018. * mapping feature it will return TRUE.
  1019. */
  1020. function tmgmt_provide_remote_languages_mappings(TMGMTTranslator $translator) {
  1021. $info = tmgmt_translator_plugin_info($translator->plugin);
  1022. if (!isset($info['map remote languages'])) {
  1023. return TRUE;
  1024. }
  1025. return $info['map remote languages'];
  1026. }
  1027. /**
  1028. * Determines if job settings of the translator will be handled by its plugin.
  1029. *
  1030. * @param TMGMTTranslator $translator
  1031. * Translator entity.
  1032. *
  1033. * @return bool
  1034. * If job settings are to be handled by the plugin.
  1035. */
  1036. function tmgmt_job_settings_custom_handling(TMGMTTranslator $translator) {
  1037. $info = tmgmt_translator_plugin_info($translator->plugin);
  1038. if (isset($info['job settings custom handling'])) {
  1039. return $info['job settings custom handling'];
  1040. }
  1041. return FALSE;
  1042. }
  1043. /**
  1044. * @} End of "addtogroup tmgmt_translator".
  1045. */
  1046. /**
  1047. * @addtogroup tmgmt_source
  1048. * @{
  1049. */
  1050. /**
  1051. * Determines all available source object plugins.
  1052. *
  1053. * @param $plugin
  1054. * (Optional) The machine-readable name of a source plugin.
  1055. *
  1056. * @return array
  1057. * An array of source plugin definitions.
  1058. */
  1059. function tmgmt_source_plugin_info($plugin = NULL) {
  1060. return _tmgmt_plugin_info('source', $plugin);
  1061. }
  1062. /**
  1063. * Get the plugin controller class for a given source plugin.
  1064. *
  1065. * @param $plugin
  1066. * (Optional) The machine-readable name of a source plugin.
  1067. *
  1068. * @return TMGMTSourcePluginControllerInterface
  1069. * The controller object for the given source plugin or an array containing
  1070. * all available source plugin controller objects if no plugin name was given.
  1071. */
  1072. function tmgmt_source_plugin_controller($plugin = NULL) {
  1073. return _tmgmt_plugin_controller('source', $plugin);
  1074. }
  1075. /**
  1076. * Get the ui controller class for a given source plugin.
  1077. *
  1078. * @param $plugin
  1079. * (Optional) The machine-readable name of a source plugin.
  1080. *
  1081. * @return TMGMTSourceUIControllerInterface
  1082. * The ui controller object for the given source plugin or an array containing
  1083. * all available source ui controller objects if no plugin name was given.
  1084. */
  1085. function tmgmt_source_ui_controller($plugin = NULL) {
  1086. return _tmgmt_plugin_controller('source', $plugin, 'ui', 'TMGMTDefaultSourceUIController');
  1087. }
  1088. /**
  1089. * Get the views controller class for a given source plugin.
  1090. *
  1091. * @param $plugin
  1092. * (Optional) The machine-readable name of a source plugin.
  1093. *
  1094. * @return TMGMTSourceViewsControllerInterface
  1095. * The views controller object for the given source plugin or an array
  1096. * containing all available source views controller objects if no plugin name
  1097. * was given.
  1098. */
  1099. function tmgmt_source_views_controller($plugin = NULL) {
  1100. return _tmgmt_plugin_controller('source', $plugin, 'views', 'TMGMTDefaultSourceViewsController');
  1101. }
  1102. /**
  1103. * Returns an array of all available source plugins with the labels as
  1104. * values and the machine-readable name as the key.
  1105. *
  1106. * @return array
  1107. * An array of the labels of all available plugins.
  1108. */
  1109. function tmgmt_source_plugin_labels() {
  1110. return _tmgmt_plugin_labels('source');
  1111. }
  1112. /**
  1113. * Returns an array of translatable item types of a source plugin.
  1114. *
  1115. * @param $plugin
  1116. * The machine-readable name of a source plugin.
  1117. *
  1118. * @return array
  1119. * The array of translatable item types.
  1120. *
  1121. * @see TMGMTSourcePluginControllerInterface::getItemTypes()
  1122. */
  1123. function tmgmt_source_translatable_item_types($plugin) {
  1124. $controller = tmgmt_source_plugin_controller($plugin);
  1125. return $controller->getItemTypes();
  1126. }
  1127. /**
  1128. * @param $plugin
  1129. * @param $item_type
  1130. * @return bool
  1131. */
  1132. function tmgmt_source_is_translatable_item_type($plugin, $item_type) {
  1133. return array_key_exists($item_type, tmgmt_source_translatable_item_types($plugin));
  1134. }
  1135. /**
  1136. * @} End of "addtogroup tmgmt_source".
  1137. */
  1138. /**
  1139. * Discovers all available source and/or translator plugins.
  1140. * @param $type
  1141. * The type of the plugin. Can be 'translator' or 'source'.
  1142. * @param $plugin
  1143. * (Optional) The machine-readable name of a source plugin.
  1144. *
  1145. * @return array
  1146. * An array of source and/or translator plugins.
  1147. */
  1148. function _tmgmt_plugin_info($type, $plugin = NULL) {
  1149. $info = &drupal_static(__FUNCTION__);
  1150. if (!isset($info[$type])) {
  1151. $info[$type] = array();
  1152. foreach (module_implements('tmgmt_' . $type . '_plugin_info') as $module) {
  1153. foreach (module_invoke($module, 'tmgmt_' . $type . '_plugin_info') as $key => $item) {
  1154. $info[$type][$key] = $item;
  1155. $info[$type][$key]['module'] = $module;
  1156. $info[$type][$key]['plugin'] = $key;
  1157. }
  1158. }
  1159. drupal_alter('tmgmt_' . $type . '_plugin_info', $info[$type]);
  1160. }
  1161. if (isset($plugin) && isset($info[$type][$plugin])) {
  1162. return $info[$type][$plugin];
  1163. }
  1164. elseif (!isset($plugin)) {
  1165. return $info[$type];
  1166. }
  1167. }
  1168. /**
  1169. * Determines the controller class for a given plugin type.
  1170. *
  1171. * @param $type
  1172. * The type of the plugin. Can be 'translator' or 'source'.
  1173. * @param $plugin
  1174. * (Optional) The machine-readable name of a source plugin.
  1175. *
  1176. * @return TMGMTPluginBaseInterface
  1177. * The controller object for the given plugin or an array containing all
  1178. * available plugin controller objects if no plugin name was given.
  1179. */
  1180. function _tmgmt_plugin_controller($type, $plugin = NULL, $controller = 'plugin', $default = NULL) {
  1181. $key = $controller . ' controller class';
  1182. $cache = &drupal_static(__FUNCTION__);
  1183. if (!isset($plugin) && !isset($cache[$type][$controller])) {
  1184. $cache[$type][$controller] = array();
  1185. foreach (_tmgmt_plugin_info($type) as $name => $info) {
  1186. if (!isset($cache[$type][$controller][$name])) {
  1187. $class = isset($default) && !isset($info[$key]) ? $default : $info[$key];
  1188. $cache[$type][$controller][$name] = new $class($type, $name);
  1189. }
  1190. }
  1191. }
  1192. elseif (isset($plugin) && !isset($cache[$type][$controller][$plugin])) {
  1193. $info = _tmgmt_plugin_info($type, $plugin);
  1194. if (empty($info[$key]) && empty($default)) {
  1195. $cache[$type][$controller][$plugin] = FALSE;
  1196. }
  1197. else {
  1198. $class = empty($info[$key]) ? $default : $info[$key];
  1199. $cache[$type][$controller][$plugin] = new $class($type, $plugin);
  1200. }
  1201. }
  1202. if (isset($plugin)) {
  1203. return $cache[$type][$controller][$plugin];
  1204. }
  1205. else {
  1206. return array_filter($cache[$type][$controller]);
  1207. }
  1208. }
  1209. /**
  1210. * Returns an array of labels of all available plugins of a given type with the
  1211. * machine-readable name as the key.
  1212. *
  1213. * @return array
  1214. * An array of the labels of all available plugins.
  1215. */
  1216. function _tmgmt_plugin_labels($type) {
  1217. $list = array();
  1218. $plugin_info = 'tmgmt_' . $type . '_plugin_info';
  1219. foreach ($plugin_info() as $key => $info) {
  1220. $list[$key] = $info['label'];
  1221. }
  1222. return $list;
  1223. }
  1224. /**
  1225. * Converts a nested data array into a flattened structure with a combined key.
  1226. *
  1227. * This function can be used by translators to help with the data conversion.
  1228. *
  1229. * Nested keys will be joined together using a colon, so for example
  1230. * $data['key1']['key2']['key3'] will be converted into
  1231. * $flattened_data['key1][key2][key3'].
  1232. *
  1233. * @param $data
  1234. * The nested array structure that should be flattened.
  1235. * @param $prefix
  1236. * Internal use only, indicates the current key prefix when recursing into
  1237. * the data array.
  1238. *
  1239. * @return array
  1240. * The flattened data array.
  1241. *
  1242. * @see tmgmt_unflatten_data()
  1243. */
  1244. function tmgmt_flatten_data($data, $prefix = NULL, $label = array()) {
  1245. $flattened_data = array();
  1246. if (isset($data['#label'])) {
  1247. $label[] = $data['#label'];
  1248. }
  1249. // Each element is either a text (has #text property defined) or has children,
  1250. // not both.
  1251. if (!empty($data['#text'])) {
  1252. $flattened_data[$prefix] = $data;
  1253. $flattened_data[$prefix]['#parent_label'] = $label;
  1254. }
  1255. else {
  1256. $prefix = isset($prefix) ? $prefix . TMGMT_ARRAY_DELIMITER : '';
  1257. foreach (element_children($data) as $key) {
  1258. $flattened_data += tmgmt_flatten_data($data[$key], $prefix . $key, $label);
  1259. }
  1260. }
  1261. return $flattened_data;
  1262. }
  1263. /**
  1264. * Converts string keys to array keys.
  1265. *
  1266. * There are three conventions for data keys in use. This function accepts each
  1267. * of it an ensures a array of keys.
  1268. *
  1269. * @param $key
  1270. * The key can be either be an array containing the keys of a nested array
  1271. * hierarchy path or a string with '][' or '|' as delimiter.
  1272. *
  1273. * @return
  1274. * Array of keys.
  1275. */
  1276. function tmgmt_ensure_keys_array($key) {
  1277. if (empty($key)) {
  1278. return array();
  1279. }
  1280. if (!is_array($key)) {
  1281. if (strstr($key, '|')) {
  1282. $key = str_replace('|', TMGMT_ARRAY_DELIMITER, $key);
  1283. }
  1284. $key = explode(TMGMT_ARRAY_DELIMITER, $key);
  1285. }
  1286. return $key;
  1287. }
  1288. /**
  1289. * Converts keys array to string key.
  1290. *
  1291. * There are three conventions for data keys in use. This function accepts each
  1292. * of it an ensures a sting keys.
  1293. *
  1294. * @param $key
  1295. * The key can be either be an array containing the keys of a nested array
  1296. * hierarchy path or a string.
  1297. * @param
  1298. * Delimiter to be use in the keys string. Default is ']['.
  1299. *
  1300. * @return
  1301. * Keys string.
  1302. */
  1303. function tmgmt_ensure_keys_string($key, $delimiter = TMGMT_ARRAY_DELIMITER) {
  1304. if (is_array($key)) {
  1305. $key = implode($delimiter, $key);
  1306. }
  1307. return $key;
  1308. }
  1309. /**
  1310. * Converts a flattened data structure into a nested array.
  1311. *
  1312. * This function can be used by translators to help with the data conversion.
  1313. *
  1314. * Nested keys will be created based on the colon, so for example
  1315. * $flattened_data['key1][key2][key3'] will be converted into
  1316. * $data['key1']['key2']['key3'].
  1317. *
  1318. * @param $data
  1319. * The flattened data array.
  1320. *
  1321. * @return array
  1322. * The nested data array.
  1323. *
  1324. * @see tmgmt_flatten_data()
  1325. */
  1326. function tmgmt_unflatten_data($flattened_data) {
  1327. $data = array();
  1328. foreach ($flattened_data as $key => $flattened_data_entry) {
  1329. drupal_array_set_nested_value($data, explode(TMGMT_ARRAY_DELIMITER, $key), $flattened_data_entry);
  1330. }
  1331. return $data;
  1332. }
  1333. /**
  1334. * Array filter callback for filtering untranslatable source data elements.
  1335. */
  1336. function _tmgmt_filter_data($value) {
  1337. return !(empty($value['#text']) || (isset($value['#translate']) && $value['#translate'] === FALSE));
  1338. }
  1339. /**
  1340. * Fetches an array of exportables from files.
  1341. *
  1342. * @param $module
  1343. * The module invoking this request. (Can be called by other modules.)
  1344. * @param $directory
  1345. * The subdirectory in the custom module.
  1346. * @param $extension
  1347. * The file extension.
  1348. * @param $name
  1349. * The name of the variable found in each file. Defaults to the same as
  1350. * $extension.
  1351. *
  1352. * @return array
  1353. * Array of $name objects.
  1354. */
  1355. function _tmgmt_load_exports($module, $directory, $extension, $name = NULL) {
  1356. if (!$name) {
  1357. $name = $extension;
  1358. }
  1359. $return = array();
  1360. // Find all the files in the directory with the correct extension.
  1361. $files = file_scan_directory(drupal_get_path('module', $module) . "/$directory", "/\.{$extension}$/");
  1362. foreach ($files as $path => $file) {
  1363. require DRUPAL_ROOT . '/' . $path;
  1364. if (isset($name)) {
  1365. $return[$$name->name] = $$name;
  1366. }
  1367. }
  1368. return $return;
  1369. }
  1370. /**
  1371. * Returns a label for a data item.
  1372. *
  1373. * @param array $data_item
  1374. * The data item array.
  1375. * @param int $max_length
  1376. * (optional) Specify the max length that the resulting label string should
  1377. * be cut to.
  1378. *
  1379. * @return string
  1380. * A label for the data item.
  1381. */
  1382. function tmgmt_data_item_label(array $data_item, $max_length = NULL) {
  1383. if (!empty($data_item['#parent_label'])) {
  1384. if ($max_length) {
  1385. // When having multiple label parts, we don't know how long each of them is,
  1386. // truncating each to the same length might result in a considerably shorter
  1387. // length than max length when there are short and long labels. Instead,
  1388. // start with the max length and repeat until the whole string is less than
  1389. // max_length. Remove 4 characters per part to avoid unecessary loops.
  1390. $current_max_length = $max_length - (count($data_item['#parent_label']) * 4);
  1391. do {
  1392. $current_max_length--;
  1393. $labels = array();
  1394. foreach ($data_item['#parent_label'] as $label_part) {
  1395. // If this not the last part, reserve 3 characters for the delimiter.
  1396. $labels[] = truncate_utf8($label_part, $current_max_length, FALSE, TRUE);
  1397. }
  1398. $label = implode(t(' > '), $labels);
  1399. } while (drupal_strlen($label) > $max_length);
  1400. return $label;
  1401. }
  1402. else {
  1403. return implode(t(' > '), $data_item['#parent_label']);
  1404. }
  1405. }
  1406. elseif (!empty($data_item['#label'])) {
  1407. return $max_length ? truncate_utf8($data_item['#label'], $max_length, FALSE, TRUE) : $data_item['#label'];
  1408. }
  1409. else {
  1410. // As a last resort, fall back to a shortened version of the text. Default
  1411. // to a limit of 50 characters.
  1412. return truncate_utf8($data_item['#text'], $max_length ? $max_length : 50, FALSE, TRUE);
  1413. }
  1414. }
  1415. /**
  1416. * Implements hook_views_plugins().
  1417. */
  1418. function tmgmt_views_plugins() {
  1419. $plugins = array(
  1420. 'access' => array(
  1421. 'tmgmt_views_job_access' => array(
  1422. 'title' => t('Job view access'),
  1423. 'help' => t('Check if the user is allowed to view jobs'),
  1424. 'handler' => 'tmgmt_views_job_access',
  1425. 'path' => drupal_get_path('module', 'tmgmt') . '/views/plugins',
  1426. ),
  1427. ),
  1428. );
  1429. return $plugins;
  1430. }
  1431. /**
  1432. * Calculates number of words, which a text consists of.
  1433. * Is placed as a separately function to be coverable by unit tests.
  1434. * @see TMGMTWordCountUnitTestCase
  1435. *
  1436. * @param string $text
  1437. * @return int
  1438. * Returns count of words of text.
  1439. */
  1440. function tmgmt_word_count($text) {
  1441. // Strip tags in case it is requested to not include them in the count.
  1442. if (variable_get('tmgmt_word_count_exclude_tags', TRUE)) {
  1443. $text = strip_tags($text);
  1444. }
  1445. // Replace each punctuation mark with space.
  1446. $text = str_replace(array('`', '~', '!', '@', '"', '#', '$', ';', '%', '^', ':', '?', '&', '*', '(', ')', '-', '_', '+', '=', '{', '}', '[', ']', '\\', '|', '/', '\'', '<', '>', ',', '.'), ' ', $text);
  1447. // Remove duplicate spaces.
  1448. $text = trim(preg_replace('/ {2,}/', ' ', $text));
  1449. // Turn into an array.
  1450. $array = ($text) ? explode(' ', $text) : array();
  1451. // How many are they?
  1452. $count = count($array);
  1453. // That is what we need.
  1454. return $count;
  1455. }