예제 #1
0
 protected function _set($config)
 {
     $this->result['errors'] = array();
     $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);
     if (empty($tm_keys)) {
         $APIKeySrv = new TMSService();
         $newUser = (object) $APIKeySrv->createMyMemoryKey();
         //throws exception
         //TODO take only for hystorical reason
         updateTranslatorJob($this->id_job, $newUser);
         //fallback
         $config['id_user'] = $newUser->id;
         $new_key = TmKeyManagement_TmKeyManagement::getTmKeyStructure();
         $new_key->tm = 1;
         $new_key->glos = 1;
         $new_key->key = $newUser->key;
         $new_key->owner = $this->userMail == $this->job_info['owner'];
         if (!$new_key->owner) {
             $new_key->{TmKeyManagement_Filter::$GRANTS_MAP[$this->userRole]['r']} = 1;
             $new_key->{TmKeyManagement_Filter::$GRANTS_MAP[$this->userRole]['w']} = 1;
         } else {
             $new_key->r = 1;
             $new_key->w = 1;
         }
         if ($new_key->owner) {
             //do nothing, this is a greedy if
         } elseif ($this->userRole == TmKeyManagement_Filter::ROLE_TRANSLATOR) {
             $new_key->uid_transl = $this->uid;
         } elseif ($this->userRole == TmKeyManagement_Filter::ROLE_REVISOR) {
             $new_key->uid_rev = $this->uid;
         }
         //create an empty array
         $tm_keys = array();
         //append new key
         $tm_keys[] = $new_key;
         //put the key in the job
         TmKeyManagement_TmKeyManagement::setJobTmKeys($this->id_job, $this->password, $tm_keys);
         //put the key in the user keiring
         if ($this->userIsLogged) {
             $newMemoryKey = new TmKeyManagement_MemoryKeyStruct();
             $newMemoryKey->tm_key = $new_key;
             $newMemoryKey->uid = $this->uid;
             $mkDao = new TmKeyManagement_MemoryKeyDao(Database::obtain());
             $mkDao->create($newMemoryKey);
         }
     }
     $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_keys TmKeyManagement_TmKeyStruct[]
          */
         foreach ($tm_keys as $tm_key) {
             $config['id_user'] = $tm_key->key;
             $TMS_RESULT = $this->_TMS->set($config);
             $set_code[] = $TMS_RESULT;
         }
     }
     $set_successful = true;
     if (array_search(false, $set_code, true)) {
         //There's an error, for now skip, let's assume that are not errors
         $set_successful = false;
     }
     if ($set_successful) {
         //          Often the get method after a set is not in real time, so return the same values ( FAKE )
         //          $TMS_GET_RESULT = $this->_TMS->get($config)->get_glossary_matches_as_array();
         //          $this->result['data']['matches'] = $TMS_GET_RESULT;
         $this->result['data']['matches'] = array($config['segment'] => array(array('segment' => $config['segment'], 'translation' => $config['translation'], 'last_update_date' => date_create()->format('Y-m-d H:i:m'), 'last_updated_by' => "Matecat user", 'created_by' => "Matecat user", 'target_note' => $config['tnote'])));
         if (isset($new_key)) {
             $this->result['data']['created_tm_key'] = true;
         }
     } else {
         $this->result['errors'][] = array("code" => -1, "message" => "We got an error, please try again.");
     }
 }