<?php
namespace Times\EigyoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\NotifyPropertyChanged,
Doctrine\Common\PropertyChangedListener;
use Doctrine\Persistence\NotifyPropertyChanged as PersistenceNotifyPropertyChanged;
use Doctrine\Persistence\PropertyChangedListener as PersistencePropertyChangedListener;
use Kzl\BaseBundle\Common\KzlStatic;
use Symfony\Component\Validator\Constraints as Assert;
use Times\EigyoBundle\Repository\SystemSettingRepository;
#use Kzl\JinjiKoukaBundle\Common\JinjiKoukaStatic;
use Kzl\LogBundle\Entity\LogInterface;
/**
* SystemSetting
*/
class SystemSetting implements PersistenceNotifyPropertyChanged , LogInterface
{
/**
* @var integer
*/
private $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set id
*
* @param integer $id
*
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* @var string
*/
private $key_str;
/**
* @var integer
*/
private $value_kbn;
/**
* @var integer
*/
private $int_value;
/**
* @var string
*/
private $string_value;
/**
* @var \DateTime
*/
private $date_value;
/**
* @var string
*/
private $name;
/**
* @var \DateTime
*/
private $created_at;
/**
* @var \DateTime
*/
private $updated_at;
/**
* Set key_str
*
* @param string $keyStr
* @return SystemSetting
*/
public function setKeyStr($keyStr)
{
$this->key_str = $keyStr;
return $this;
}
/**
* Get key_str
*
* @return string
*/
public function getKeyStr()
{
return $this->key_str;
}
/**
* Set value_kbn
*
* @param integer $valueKbn
* @return SystemSetting
*/
public function setValueKbn($valueKbn)
{
$this->value_kbn = $valueKbn;
return $this;
}
/**
* Get value_kbn
*
* @return integer
*/
public function getValueKbn()
{
return $this->value_kbn;
}
/**
* Set int_value
*
* @param integer $intValue
* @return SystemSetting
*/
public function setIntValue($intValue)
{
$this->int_value = $intValue;
return $this;
}
/**
* Get int_value
*
* @return integer
*/
public function getIntValue()
{
return $this->int_value;
}
/**
* Set string_value
*
* @param string $stringValue
* @return SystemSetting
*/
public function setStringValue($stringValue)
{
$this->string_value = $stringValue;
return $this;
}
/**
* Get string_value
*
* @return string
*/
public function getStringValue()
{
return $this->string_value;
}
/**
* Set date_value
*
* @param \DateTime $dateValue
* @return SystemSetting
*/
public function setDateValue($dateValue)
{
$this->date_value = $dateValue;
return $this;
}
/**
* Get date_value
*
* @return \DateTime
*/
public function getDateValue()
{
return $this->date_value;
}
/**
* Set name
*
* @param string $name
* @return SystemSetting
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
* @return SystemSetting
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get created_at
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set updated_at
*
* @param \DateTime $updatedAt
* @return SystemSetting
*/
public function setUpdatedAt($updatedAt)
{
$this->updated_at = $updatedAt;
return $this;
}
/**
* Get updated_at
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
// ------------------ 以下追加処理 ----------------------
/**
* 値区分によって、該当する項目へ値を設定する
* @param integer $valueKbn
* @param mixed $value
*/
public function setValueByValueKbn($value, $valueKbn = null)
{
//値区分が渡されない場合は、自身の設定を利用する
if(!$valueKbn){
$valueKbn = $this->getValueKbn();
}
// 値区分によってそれぞれの項目へ設定
switch ($valueKbn) {
case SystemSettingRepository::VALUE_KBN_NUM:
$this->setIntValue($value);
break;
case SystemSettingRepository::VALUE_KBN_STR:
$this->setStringValue($value);
break;
case SystemSettingRepository::VALUE_KBN_DATE:
$this->setDateValue($value);
break;
default:
break;
}
}
/**
* 値区分によって、該当する項目から値を取得する
* @param integer $valueKbn
* @param mixed $value
*/
public function getValueByValueKbn($valueKbn = null)
{
//値区分が渡されない場合は、自身の設定を利用する
if(!$valueKbn){
$valueKbn = $this->getValueKbn();
}
// 値区分によってそれぞれの項目を返す
switch ($valueKbn) {
case SystemSettingRepository::VALUE_KBN_NUM:
return $this->getIntValue();
case SystemSettingRepository::VALUE_KBN_STR:
return $this->getStringValue();
case SystemSettingRepository::VALUE_KBN_DATE:
return $this->getDateValue();
default:
throw new \LogicException(sprintf("不明な値区分「%s」です。", $setting->getValueKbn()));
}
}
/**
* 2014/9/13 mitsuanga バリデーションをわたす(コード設定用)
* @param type $key
*/
/*
public function getValidationForCdSetting($key){
$validation = array();
// 種類は必須
if($key == SystemSettingRepository::CD_SETTING_KEY_KIND){
$validation[] = new Assert\NotBlank();
}
// すべて数値
// 14/11/07 sakamoto 社員設定に合わせてコードを10桁まで変更可能に
if($key == SystemSettingRepository::CD_SETTING_KEY_MAX){
// $maxMin = array('max' => 20, 'min' => 1);
$maxMin = array('max' => 10, 'min' => 1);
} else {
$maxMin = array('max' => 1, 'min' => 0);
}
$validation[] = new Assert\Range($maxMin);
return $validation;
}
*/
/**
* 種別ごとに必要な制約を返す。
* @param integer $valueKbn
*/
public function getValidation($valueKbn = null, $keyStr = null)
{
//値区分が渡されない場合は、自身の設定を利用する
if(!$valueKbn){
$valueKbn = $this->getValueKbn();
}
//キー文字列も同様
if(!$keyStr){
$keyStr = $this->getKeyStr();
}
$validation = array();
// 値区分での判定
switch ($valueKbn) {
case SystemSettingRepository::VALUE_KBN_NUM:
// 最大11文字・integer
// 第一期年度の場合は、最小・最大値を4桁へ
if($keyStr == SystemSettingRepository::KEY_TERM_START_YEAR) {
$validation[] = new Assert\Range(array('min'=>2010, 'max' => date_create('now')->format('Y')));
} else if ($keyStr == SystemSettingRepository::KEY_NEW_INFO_DAY_AGO) {
// 新着表示日数
$validation[] = new Assert\Range(array('min'=>1, 'max' => 100));
$validation[] = new Assert\Type(array('type'=>'digit', 'message' => 'This value should be a valid number.'));
} else {
$validation[] = new Assert\Range(array('max' => PHP_INT_MAX));
}
break;
case SystemSettingRepository::VALUE_KBN_STR:
// 最大255(string初期値)
if(strpos ($keyStr, 'MAIL_SEND_TIME') === 0) {
$validation[] = new Assert\Regex(array('pattern' => '/^(0[0-9]{1}|1{1}[0-9]{1}|2{1}[0-3]{1}):(0[0-9]{1}|[1-5]{1}[0-9]{1})$/'));
} elseif($keyStr == SystemSettingRepository::KEY_ADMIN_MAIL) {
// 15/2/26 mitsunaga 追加
$validation[] = new Assert\Email();
} else {
$validation[] = new Assert\Length(array('max' => 6));
}
break;
case SystemSettingRepository::VALUE_KBN_DATE:
// 日付
$validation[] = new Assert\DateTime();
break;
default:
break;
}
// キー文字列での判定
$requiredItems = SystemSettingRepository::$required_items;
// 必須
if(in_array($keyStr, $requiredItems)){
$validation[] = new Assert\NotBlank();
}
/*
// メールアドレス
if($keyStr == SystemSettingRepository::KEY_ADMIN_MAIL){
$validation[] = new Assert\Email();
}
*/
return $validation;
}
/**
* 選択可能な値のリストを返す。
* 主に数値項目で利用
*/
public function getSelectableValues($keyStr = null)
{
//キー文字列の指定がなければ自分自身を使用
if(!$keyStr){
$keyStr = $this->getKeyStr();
}
//if($keyStr == SystemSettingRepository::KEY_KOUKA_STAGE_COLOR_SELECT_INIT){
/*
switch($keyStr){
case SystemSettingRepository::KEY_SELF_HYOKA_FLG:
// 自己評価設定
case SystemSettingRepository::KEY_AIM_FLG:
// 個人目標設定
case SystemSettingRepository::KEY_MAIL_CHECK_FLG:
// 2014/7/9 mitsunaga メールアドレス正当性チェック
case SystemSettingRepository::KEY_EMPLOYEE_OPTION_USE_FLG:
// 個人目標機能使用設定
case SystemSettingRepository::KEY_EMPLOYEE_INFOMATION_FLG:
// 面談機能使用設定
case SystemSettingRepository::KEY_EMPLOYEE_AIM_FLG:
// 個人情報機能使用設定
return SystemSettingRepository::$use_settings;
case SystemSettingRepository::KEY_LOGIN_SETTING:
// ログイン設定
return SystemSettingRepository::$login_settings;
case SystemSettingRepository::KEY_EMPLOYEE_AIM_EDIT_FLG:
case SystemSettingRepository::KEY_EMPLOYEE_AIM_PERCENTAGE_EDIT_FLG:
// 14/12/1 mitsunaga employee_aim setting
return SystemSettingRepository::$employee_aim_setting;
} */
return array();
}
/**
* @ORM\PostLoad
*/
public function doPostLoad()
{
// Add your code here
}
private $_changeValues = array();
/**
* @ORM\PrePersist
*/
public function doPreUpdate()
{
KzlStatic::setNewDNo($this);
// ** 変更された値をログへ記録
/*
// 1)変更値取得
if ($this->_listeners) {
foreach ($this->_listeners as $listener) {
// 変更項目
$changeSet = $listener->getEntityChangeSet($this);
foreach ($changeSet as $item => $values) {
// 監視対象なら変更値を保存
if(SystemSettingRepository::isLogTargetItem($item)){
foreach($values as $key => $value){
if(is_null($value) || JinjiKoukaStatic::isNotValue($value)){
$values[$key] = JinjiKoukaStatic::LOG_EMPTT_STRING;
}
}
//変更値を保持しておく
$this->_changeValues[SystemSettingRepository::$log_target_list[$item]] = $values;
}
}//end foreach $changeSet
} // end foreach $_listeners
} //end if
*/
// 新規の時は作成日を設定
if(null === $this->created_at){
$this->setCreatedAt(new \DateTime);
}
// 更新日を設定
$this->setUpdatedAt(new \DateTime);
}
public function writeLog()
{
/*
// var_dump( $this->_changeValues);
// 2)変更値があればログへ保存
if(count($this->_changeValues) > 0){
// var_dump( $this->_changeValues);
JinjiKoukaStatic::getLogger()->infoChange(JinjiKoukaStatic::getTrans('システム設定'), $this->getName(), $this->_changeValues);
// 初期化
$this->_changeValues = array();
} */
}
public function getChangeValues()
{
if(count($this->_changeValues) > 0){
return $this->_changeValues;
}
return false;
}
public function resetChangeValues(){
if(count($this->_changeValues = array()));
}
//-------------------------------------------------------------------
// NotifyPropertyChanged 実装
private $_listeners = array();
/**
* 値変更のイベントリスナー
* @param \Doctrine\Common\PropertyChangedListener $listener
*/
public function addPropertyChangedListener(PersistencePropertyChangedListener $listener)
{
$this->_listeners[] = $listener;
}
/**
* 変更前・変更後の値をイベントリスナーに保存する
* @param type $propName
* @param type $oldValue
* @param type $newValue
*/
protected function _onPropertyChanged($propName, $oldValue, $newValue)
{
if ($this->_listeners) {
foreach ($this->_listeners as $listener) {
$listener->propertyChanged($this, $propName, $oldValue, $newValue);
}
}
}
// -----------------------------------------
/**
* 値型が文字列かどうか
* @return bolean
*/
private function isValueString()
{
return $this->value_kbn == SystemSettingRepository::VALUE_KBN_STR;
}
/**
* 2014/9/13 mitsunaga 文字列の値をJSON形式で返す
* @return type
*/
/*
public function getStrValueByArray()
{
if(!$this->isValueString()) throw new \Exception('値処理が不正です');
if($this->string_value){
return JinjiKoukaStatic::getArrayByJson($this->string_value);
}else{
// 初期値で空が入っているので、値が無ければデフォルト値を返す
if(SystemSettingRepository::isCdSetting($this->key_str)){
return SystemSettingRepository::$cd_setting_default_values;
}else{
throw new \Exception('値処理が不正です');
}
}
} */
/**
* 2014/9/13 mitsunaga 文字列の値を配列で設定
* @return type
*/
/*
public function setStrValueByArray($array)
{
if(!$this->isValueString()) throw new \Exception('値処理が不正です');
$this->string_value = JinjiKoukaStatic::getJsonByArray($array);
} */
}