function handle(&$params) { try { $app = Dataface_Application::getInstance(); $query = $app->getQuery(); $selectedRecords = df_get_selected_records($query); $isNewJob = false; if ($query['-job'] && is_numeric($query['-job'])) { $selectedJob = df_get_record('jobs', array('job_id' => '=' . $query['-job'])); } else { //no job was selected by user $site_id = $selectedRecords[0]->val('website_id'); $jobs = df_get_records_array('jobs', array('website_id' => $site_id, 'compiled' => 'false')); $createNewJob = false; if ($query['-job'] == "new") { $createNewJob = true; } if (count($jobs) == 0 || $createNewJob) { //create a new job $selectedJob = SweteJob::createJob(SweteSite::loadSiteById($site_id))->getRecord(); $isNewJob = true; } else { if (count($jobs) == 1) { //only one available job $selectedJob = $jobs[0]; } else { throw new Exception("No Job id was specified, but there are " . $count($jobs) . " available jobs to add to"); } } } if (!$selectedJob) { throw new Exception("Job could not be found", E_USER_ERROR); } if (!$selectedJob->checkPermission('edit')) { throw new Exception("You don't have permission to edit this job"); } $job = new SweteJob($selectedJob); $stringsAdded = array(); foreach ($selectedRecords as $record) { if (intval($record->val('website_id')) !== intval($selectedJob->val("website_id"))) { throw new Exception("The string " . $record->val('string') . " is not in the same site as the job."); } //If string was already added to ANOTHER job, it doesn't matter //It will also be added to this one //if string was already added to this job, do nothing if (!$job->containsString($record->val('string'))) { $job->addTranslationMiss($record->val('translation_miss_log_id')); array_push($stringsAdded, $record->val('string')); } } $results = array('stringsAdded' => $stringsAdded, 'jobId' => $selectedJob->val('job_id'), 'isNewJob' => $isNewJob); echo json_encode($results); } catch (Exception $e) { if ($e->getCode() == E_USER_ERROR) { echo $e->getMessage(); } else { throw $e; } } }
function testExtractDictionaryFromHtml() { //create a job so that we can use its translation memory $job = SweteJob::createJob($this->staticSite); $username = '******'; //add strings to the job $strings = array("Blah blah blah string", "Doop doop doop doop string", "Whee whee whee whee whee string", "Hey hey hey hey hey strings"); $this->addWebpageForStaticSite($url, $strings); $misses = df_get_records_array('translation_miss_log', array('website_id' => '=' . $this->staticSite->getRecord()->val('website_id'))); foreach ($misses as $miss) { $job->addTranslationMiss($miss->val('translation_miss_log_id')); } $job->compile(); $tm = $job->getTranslationMemory(); $trec = $tm->setTranslationStatus('cheese', 'fromage', XFTranslationMemory::TRANSLATION_SUBMITTED, $username); $trec = $tm->setTranslationStatus('ham', 'jambon', XFTranslationMemory::TRANSLATION_SUBMITTED, $username); $html = "<div>apple</div>\n\t\t\t<div>cheese</div>\n\t\t\t<div>Ham</div>\n\t\t\t<div>ham and cheese</div>"; $dict = $job->extractDictionaryFromHtml($job->getTranslationMemory(), $html); $this->assertTrue(isset($dict)); $this->assertEquals(4, sizeof($dict)); foreach ($dict as $k => $v) { error_log("extractDictionaryFromHtml result: {$k} => {$v}"); } $this->assertTrue(empty($dict[0])); $this->assertEquals("fromage", $dict[1]); $this->assertTrue(empty($dict[2])); $this->assertTrue(empty($dict[3])); }
function handle(&$params) { try { // First get the selected records $app =& Dataface_Application::getInstance(); $query =& $app->getQuery(); $isNewJob = false; if (!@$query['-record-id']) { throw new Exception("No record id was specified"); } $record = df_get_record_by_id($query['-record-id']); //get the selected job if ($query['-job'] && is_numeric($query['-job'])) { $selectedJob = df_get_record('jobs', array('job_id' => '=' . $query['-job'])); } else { //no job was selected by user $site_id = $record->val('website_id'); $jobs = df_get_records_array('jobs', array('website_id' => $site_id, 'compiled' => 'false')); $createNewJob = false; if ($query['-job'] == "new") { $createNewJob = true; } if (count($jobs) == 0 || $createNewJob) { //create a new job $selectedJob = SweteJob::createJob(SweteSite::loadSiteById($site_id))->getRecord(); $isNewJob = true; } else { if (count($jobs) == 1) { //only one available job $selectedJob = $jobs[0]; } else { throw new Exception("No Job id was specified, but there are " . $count($jobs) . " available jobs to add to"); } } } if (!$selectedJob) { throw new Exception("Job could not be found", E_USER_ERROR); } if (!$selectedJob->checkPermission('edit')) { throw new Exception("You don't have permission to edit this job"); } $job = new SweteJob($selectedJob); if (intval($record->val('website_id')) !== intval($selectedJob->val("website_id"))) { throw new Exception("The webpage " . $record->val('webpage_id') . " is not in the same site as the job."); } $webpage = new SweteWebpage($record); //if webpage was already added to this job, do nothing if (!$job->containsWebpage($webpage)) { $job->addWebpage($webpage, null, true); $results = array('pageAdded' => $record->val('webpage_id'), 'jobId' => $selectedJob->val('job_id'), 'isNewJob' => $isNewJob); } else { $results = array('pageAdded' => "false", 'jobId' => $selectedJob->val('job_id'), 'isNewJob' => $isNewJob); } echo json_encode($results); } catch (Exception $e) { if ($e->getCode() == E_USER_ERROR) { echo $e->getMessage(); } else { throw $e; } } }