public function doAction()
 {
     if (is_null($this->source) || $this->source === '') {
         $this->result['errors'][] = array("code" => -1, "message" => "missing source segment");
     }
     if (is_null($this->target) || $this->target === '') {
         $this->result['errors'][] = array("code" => -2, "message" => "missing target segment");
     }
     if (empty($this->source_lang)) {
         $this->result['errors'][] = array("code" => -3, "message" => "missing source lang");
     }
     if (empty($this->target_lang)) {
         $this->result['errors'][] = array("code" => -2, "message" => "missing target lang");
     }
     if (empty($this->id_job)) {
         $this->result['errors'][] = array("code" => -4, "message" => "id_job not valid");
         $msg = "\n\n Critical. Quit. \n\n " . var_export(array_merge($this->result, $_POST), true);
         Log::doLog($msg);
         Utils::sendErrMailReport($msg);
         // critical. Quit.
         return -1;
     }
     //get Job Infos, we need only a row of jobs ( split )
     $job_data = getJobData((int) $this->id_job, $this->password);
     $pCheck = new AjaxPasswordCheck();
     //check for Password correctness
     if (empty($job_data)) {
         $this->result['errors'][] = array("code" => -101, "message" => "error fetching job data");
     }
     if (empty($this->result['errors']) && !$pCheck->grantJobAccessByJobData($job_data, $this->password)) {
         $this->result['errors'][] = array("code" => -10, "message" => "wrong password");
     }
     if (!empty($this->result['errors'])) {
         $msg = "\n\n Error \n\n " . var_export(array_merge($this->result, $_POST), true);
         Log::doLog($msg);
         Utils::sendErrMailReport($msg);
         return -1;
     }
     $id_tms = $job_data['id_tms'];
     $tm_keys = $job_data['tm_keys'];
     if ($id_tms != 0) {
         $tms = Engine::getInstance(1);
         /**
          * @var $tms Engines_MyMemory
          */
         $config = $tms->getConfigStruct();
         $config['segment'] = CatUtils::view2rawxliff($this->source);
         $config['translation'] = CatUtils::view2rawxliff($this->target);
         $config['source'] = $this->source_lang;
         $config['target'] = $this->target_lang;
         $config['email'] = "*****@*****.**";
         //Props
         $config['prop'] = json_encode(CatUtils::getTMProps($job_data));
         //instantiate TMS object
         $result = array();
         $this->checkLogin();
         try {
             if (self::isRevision()) {
                 $this->userRole = TmKeyManagement_Filter::ROLE_REVISOR;
             }
             //find all the job's TMs with write grants and make a contribution to them
             $tm_keys = TmKeyManagement_TmKeyManagement::getJobTmKeys($tm_keys, 'w', 'tm', $this->uid, $this->userRole);
             if (!empty($tm_keys)) {
                 unset($config['id_user']);
                 foreach ($tm_keys as $i => $tm_info) {
                     $config['id_user'] = $tm_info->key;
                     $res = $tms->set($config);
                     if (!$res) {
                         $result[] = $res;
                         $this->result['errors'][] = array("code" => -5, "message" => "Set contribution error for key " . $tm_info->name);
                     }
                 }
             } else {
                 $res = $tms->set($config);
                 if (!$res) {
                     $result[] = $res;
                     $this->result['errors'][] = array("code" => -5, "message" => "Set contribution error");
                 }
             }
         } catch (Exception $e) {
             $this->result['errors'][] = array("code" => -6, "message" => "Error while retrieving job's TM.");
             Log::doLog(__METHOD__ . " -> " . $e->getMessage());
         }
         //if translator_username is empty no key is added to MyMemory API SET query string, so, anonymous by default
         if (count($result)) {
             $this->result['code'] = -1;
             $this->result['data'] = "KO";
             Log::doLog("Set Contribution Failed.");
             Log::doLog(var_export($_POST, true));
             return -1;
         }
         $this->result['code'] = 1;
         $this->result['data'] = "OK";
     } else {
         $this->result['code'] = 1;
         $this->result['data'] = "NOCONTRIB_OK";
     }
 }
 protected function _update($config)
 {
     $tm_keys = $this->job_info['tm_keys'];
     if (self::isRevision()) {
         $this->userRole = TmKeyManagement_Filter::ROLE_REVISOR;
     }
     //get TM keys with read grants
     $tm_keys = TmKeyManagement_TmKeyManagement::getJobTmKeys($tm_keys, 'w', 'glos', $this->uid, $this->userRole);
     $config['segment'] = CatUtils::view2rawxliff($config['segment']);
     $config['translation'] = CatUtils::view2rawxliff($config['translation']);
     $config['prop'] = json_encode(CatUtils::getTMProps($this->job_info));
     //prepare the error report
     $set_code = array();
     //set the glossary entry for each key with write grants
     if (count($tm_keys)) {
         /**
          * @var $tm_key TmKeyManagement_TmKeyStruct
          */
         foreach ($tm_keys as $tm_key) {
             $config['id_user'] = $tm_key->key;
             $TMS_RESULT = $this->_TMS->update($config);
             $set_code[] = $TMS_RESULT;
         }
     }
     $set_successful = true;
     if (array_search(false, $set_code, true)) {
         $set_successful = false;
     }
     //reset key list
     $config['id_user'] = array();
     foreach ($tm_keys as $tm_key) {
         $config['id_user'][] = $tm_key->key;
     }
     if ($set_successful) {
         $TMS_GET_RESULT = $this->_TMS->get($config)->get_glossary_matches_as_array();
         $this->result['data']['matches'] = $TMS_GET_RESULT;
     }
 }