Skip to content

Commit

Permalink
Merge pull request #39 from s-nakajima/master
Browse files Browse the repository at this point in the history
多言語対応
  • Loading branch information
s-nakajima committed Jan 5, 2017
2 parents 9c56876 + 3057090 commit 5d34579
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions Model/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,71 @@ public function getLanguages() {
return self::$languages;
}

/**
* 利用可能な言語登録
*
* 呼び出しもとでbeginを実行する
*
* @param array $data リクエストデータ
* @return bool
* @throws InternalErrorException
*/
public function saveActive($data) {
if (! $this->validateActive($data)) {
return false;
}

$update = array(
'is_active' => true,
);
$conditions = array(
'code' => $data['Language']['code']
);
if (! $this->updateAll($update, $conditions)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

$update = array(
'is_active' => false,
);
$conditions = array(
'code NOT IN' => $data['Language']['code']
);
if (! $this->updateAll($update, $conditions)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

return true;
}

/**
* 利用可能な言語登録の入力チェック
*
* @param array $data リクエストデータ
* @return bool
*/
public function validateActive($data) {
$fieldName = 'Language.code';

$languages = $this->find('list', array(
'fields' => array('code', 'is_active'),
'recursive' => -1,
));
$defaultLangs = array_keys($languages);

if (! isset($data['Language']['code']) || count($data['Language']['code']) === 0) {
$this->invalidate(
$fieldName, __d('site_manager', 'Please select the language to use.')
);
}

if (array_diff($data['Language']['code'], $defaultLangs)) {
$this->invalidate(
$fieldName, __d('net_commons', 'Invalid request.')
);
}

return count($this->validationErrors) === 0;
}

}

0 comments on commit 5d34579

Please sign in to comment.