public function testGetTmKeyStructure_withArgs()
 {
     $args = array('key' => self::$dummyTmKey_key, 'uid_transl' => self::$dummyTmKey_uid_transl, 'r_transl' => self::$dummyTmKey_r_transl, 'w_transl' => self::$dummyTmKey_w_transl, 'owner' => self::$dummyTmKey_owner, 'r' => self::$dummyTmKey_r, 'w' => self::$dummyTmKey_w);
     $structure = TmKeyManagement_TmKeyManagement::getTmKeyStructure($args);
     $this->assertInstanceOf("TmKeyManagement_TmKeyStruct", $structure);
     $this->assertObjectHasAttribute("tm", $structure);
     $this->assertObjectHasAttribute("glos", $structure);
     $this->assertObjectHasAttribute("owner", $structure);
     $this->assertObjectHasAttribute("uid_transl", $structure);
     $this->assertObjectHasAttribute("uid_rev", $structure);
     $this->assertObjectHasAttribute("name", $structure);
     $this->assertObjectHasAttribute("key", $structure);
     $this->assertObjectHasAttribute("r", $structure);
     $this->assertObjectHasAttribute("w", $structure);
     $this->assertObjectHasAttribute("r_transl", $structure);
     $this->assertObjectHasAttribute("w_transl", $structure);
     $this->assertObjectHasAttribute("r_rev", $structure);
     $this->assertObjectHasAttribute("w_rev", $structure);
     $this->assertObjectHasAttribute("source", $structure);
     $this->assertObjectHasAttribute("target", $structure);
     $this->assertNull($structure->tm);
     $this->assertNull($structure->glos);
     $this->assertEquals(self::$dummyTmKey_owner, $structure->owner);
     $this->assertEquals(self::$dummyTmKey_uid_transl, $structure->uid_transl);
     $this->assertNull($structure->uid_rev);
     $this->assertNull($structure->name);
     $this->assertEquals(self::$dummyTmKey_key, $structure->key);
     $this->assertEquals(self::$dummyTmKey_r, $structure->r);
     $this->assertEquals(self::$dummyTmKey_w, $structure->w);
     $this->assertEquals(self::$dummyTmKey_r_transl, $structure->r_transl);
     $this->assertEquals(self::$dummyTmKey_w_transl, $structure->w_transl);
     $this->assertNull($structure->r_rev);
     $this->assertNull($structure->w_rev);
     $this->assertNull($structure->source);
     $this->assertNull($structure->target);
 }
 protected function _createJobs(ArrayObject $projectStructure, $owner)
 {
     foreach ($projectStructure['target_language'] as $target) {
         //shorten languages and get payable rates
         $shortSourceLang = substr($projectStructure['source_language'], 0, 2);
         $shortTargetLang = substr($target, 0, 2);
         //get payable rates
         $projectStructure['payable_rates'] = Analysis_PayableRates::getPayableRates($shortSourceLang, $shortTargetLang);
         $query_min_max = "SELECT MIN( id ) AS job_first_segment , MAX( id ) AS job_last_segment\n                FROM segments WHERE id_file IN ( %s )";
         $string_file_list = implode(",", $projectStructure['file_id_list']->getArrayCopy());
         $last_segments_query = sprintf($query_min_max, $string_file_list);
         $rows = $this->dbHandler->fetch_array($last_segments_query);
         if (!$rows || count($rows) == 0) {
             Log::doLog("Segment Search: Failed Retrieve min_segment/max_segment for files ( {$string_file_list} ) - DB Error: " . var_export($this->dbHandler->get_error(), true) . " - \n");
             throw new Exception("Segment import - DB Error: " . var_export($this->dbHandler->get_error(), true), -5);
         }
         //IT IS EVERY TIME ONLY A LINE!! don't worry about a cycle
         $job_segments = $rows[0];
         $password = $this->_generatePassword();
         $tm_key = array();
         if (!empty($projectStructure['private_tm_key'])) {
             foreach ($projectStructure['private_tm_key'] as $tmKeyObj) {
                 $newTmKey = TmKeyManagement_TmKeyManagement::getTmKeyStructure();
                 $newTmKey->tm = true;
                 $newTmKey->glos = true;
                 $newTmKey->owner = true;
                 $newTmKey->name = $tmKeyObj['name'];
                 $newTmKey->key = $tmKeyObj['key'];
                 $newTmKey->r = $tmKeyObj['r'];
                 $newTmKey->w = $tmKeyObj['w'];
                 $tm_key[] = $newTmKey;
             }
             //TODO: change this: private tm key field should not be used
             //set private tm key string to the first tm_key for retro-compatibility
             Log::doLog($projectStructure['private_tm_key']);
             //                $projectStructure[ 'private_tm_key' ] = $projectStructure[ 'private_tm_key' ][ 0 ][ 'key' ];
         }
         $projectStructure['tm_keys'] = json_encode($tm_key);
         $jid = insertJob($projectStructure, $password, $target, $job_segments, $owner);
         $projectStructure['array_jobs']['job_list']->append($jid);
         $projectStructure['array_jobs']['job_pass']->append($password);
         $projectStructure['array_jobs']['job_segments']->offsetSet($jid . "-" . $password, $job_segments);
         foreach ($projectStructure['file_id_list'] as $fid) {
             try {
                 //prepare pre-translated segments queries
                 if (!empty($projectStructure['translations'])) {
                     $this->_insertPreTranslations($jid);
                 }
             } catch (Exception $e) {
                 $msg = "\n\n Error, pre-translations lost, project should be re-created. \n\n " . var_export($e->getMessage(), true);
                 Utils::sendErrMailReport($msg);
             }
             insertFilesJob($jid, $fid);
         }
     }
 }
 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.");
     }
 }