Gc\Core\Translator::setValue PHP Method

setValue() public method

Set config value
public setValue ( string $source, array $destinations ) : boolean
$source string Source
$destinations array Destinations
return boolean
    public function setValue($source, array $destinations)
    {
        if (is_numeric($source)) {
            $row = $this->fetchRow($this->select(array('id' => $source)));
            if (empty($row)) {
                return false;
            }
            $sourceId = $row['id'];
        } else {
            $row = $this->fetchRow($this->select(array('source' => $source)));
            if (!empty($row)) {
                $sourceId = $row['id'];
            } else {
                $this->insert(array('source' => $source));
                $sourceId = $this->getLastInsertId();
            }
        }
        foreach ($destinations as $destination) {
            if (empty($destination['locale']) or empty($destination['value'])) {
                continue;
            }
            $select = new Select();
            $select->from('core_translate_locale');
            $select->where->equalTo('locale', $destination['locale']);
            $select->where->equalTo('core_translate_id', $sourceId);
            $row = $this->fetchRow($select);
            if (!empty($row)) {
                $destination['dst_id'] = $row['id'];
            }
            if (!empty($destination['dst_id'])) {
                $update = new Update('core_translate_locale');
                $update->set(array('destination' => $destination['value'], 'locale' => $destination['locale']));
                $update->where->equalTo('id', $destination['dst_id']);
                $this->execute($update);
            } else {
                $insert = new Insert();
                $insert->into('core_translate_locale')->values(array('destination' => $destination['value'], 'locale' => $destination['locale'], 'core_translate_id' => $sourceId));
                $this->execute($insert);
            }
        }
        return true;
    }

Usage Example

Example #1
0
 /**
  * Insert translations into database
  *
  * @param array $session Session array
  *
  * @return void
  */
 protected function insertTranslations($session)
 {
     if (empty($session['copy_translations'])) {
         return;
     }
     //Save all languages in database
     $languagesFilename = glob(GC_APPLICATION_PATH . '/data/install/translation/*.php');
     $translator = new Core\Translator();
     foreach ($languagesFilename as $language) {
         $langConfig = (include $language);
         $locale = basename($language, '.php');
         foreach ($langConfig as $source => $destination) {
             $translator->setValue($source, array(array('locale' => $locale, 'value' => $destination)));
         }
         copy($language, GC_APPLICATION_PATH . '/data/translation/' . basename($language));
     }
 }
All Usage Examples Of Gc\Core\Translator::setValue