slug = $_brandSlug; $s = $_SERVER["HTTP_HOST"]; if($s == 'dev.celebrities-jewelry.com'){ $this->serverPath = '/home/kir820556/public_html/dev'; $this->baseUrl = '/brand/'.$this->slug; $this->topUrl = ''; } else { $this->serverPath = '/home/kir820556/public_html/www'; $this->baseUrl = '/brand/'.$this->slug; $this->topUrl = ''; } $this->dataDir = $this->serverPath.'/data'; $this->regularDataDir = '/home/kir820556/public_html/www/data'; $this->mode = $mode; //ブランドページがあるかどうか $this->hasPage = false; //公開しているかどうか $this->public = false; //各ページ名 $this->pageTitle = array( 'wedding' => 'ウエディング', 'history' => 'ブランドの歴史', 'topics' => 'トピックス' ); if(file_exists($this->serverPath.'/data/'.$_brandSlug.'/inc/config.inc')){ include($this->serverPath.'/data/'.$_brandSlug.'/inc/config.inc'); $this->hasPage = true; $this->config = $caj_brand_config[$_brandSlug]; if($this->config['public']){ $this->public = true; } } } /* * 情報取得 * -------------------------------- */ public function getData($page){ $res = array(); $p = $page; if($page == 'weddingTop'){ $p = 'wedding'; } $file = $this->serverPath.'/data/'.$this->slug.'/inc/'.$p.'.inc'; if(file_exists($file)){ $res['result'] = 'success'; include($file); $res['data'] = $cajBrandContents; //設定がまだない場合 } else { $res['result'] = 'success'; $res['state'] = 'empty'; $res['data'] = array(); switch($p){ case 'history': $res['data'] = array( 'lead' => '', 'body' => '', 'imgCaption' => array( '01' => '', '02' => '', '03' => '', '04' => '' ) ); break; case 'topics': $jt = array( 'name' => '', 'subName' => '', 'dec' => '', 'price' => '', 'priceKara' => '', 'material' => '', 'url' => array( 'ring' => '', 'store' => '' ) ); $res['data'] = array( 'lead' => '', 'body' => '', 'imgCaption' => array( 'main' => array( 'title' => '', 'body' => '' ) ), 'jewelry' => array( 1 => $jt, 2 => $jt, 3 => $jt ) ); break; case 'wedding': $res['data'] = array( 'lead' => '', 'body' => '', 'imgCaption' => array( 'main' => '', 'sub01' => '', 'sub02' => '' ) ); break; case 'weddingEngagement': case 'weddingMarriage': $m = $p == 'weddingEngagement' ? 'engagement' : 'marriage'; $pt = array( 'price' => null, 'kara' => false, 'material' => '' ); $rt = array( 'name' => '', 'subName' => '', 'dec' => '', 'price' => array( 1 => $pt, 2 => $pt, 3 => $pt, 4 => $pt, ), 'url' => array( 'ring' => '', 'store' => '' ) ); $res['data'][$m] = array(); for($i = 1; $i <= 10; $i++){ $d = $rt; $d['order'] = $i; $res['data'][$m][$i] = $d; } break; } } return $res; } /* * 保存処理の振り分け * -------------------------------- */ public function saveData(){ $res = array(); switch($this->mode){ // 共通設定ファイル case 'config': $check = $this->parmCheck( array( $_POST['slug'], $_POST['name'], $_POST['kana'], $_POST['dec'] )); if($check['res']){ $res = $this->saveBrandConfig(); } else { $res = array('result' => 'fail', 'error' => $check['field']); } break; // History case 'history': $check = $this->parmCheck( array( $_POST['data']['lead'], $_POST['data']['body'] )); if($check['res']){ $res = $this->saveBrandHistory(); } else { $res = array('result' => 'fail', 'error' => $check['field']); } break; // Topics case 'jewelry': case 'topics': $check = $this->parmCheck( array( $_POST['data']['lead'], $_POST['data']['body'] )); if($check['res']){ $res = $this->saveBrandTopics(); } else { $res = array('result' => 'fail', 'error' => $check['field']); } break; // Wedding case 'weddingTop': $check = $this->parmCheck( array( $_POST['data']['lead'], $_POST['data']['body'] )); if($check['res']){ $res = $this->saveBrandWedding(); } else { $res = array('result' => 'fail', 'error' => $check['field']); } break; // Wedding - Rings case 'weddingEngagement': $res = $this->saveRings('engagement'); break; case 'weddingMarriage': $res = $this->saveRings('marriage'); break; // 本番反映 case 'deploy': $res = $this->deployBrandData($_POST['slug']); break; } return $res; } /* * 必須データーチェック * 必須項目のキーを渡して値をチェック * -------------------------------- */ private function parmCheck($required_data){ $res = true; $emptyField = array(); if(!empty($required_data)){ foreach($required_data as $k => $v){ if(empty($v)){ $res = false; $emptyField[] = $v; } } } return array( 'res' => $res, 'field' => $emptyField ); } /* * 共通設定ファイルの保存 * -------------------------------- */ public function saveBrandConfig(){ //新規保存 $addFlag = (!empty($_POST['new'])) ? true : false; //データー整形 $public = (!empty($_POST['public'])) ? $_POST['public'] : 'false'; $featureUrl = ''; if(!empty($_POST['feature'])){ $featureUrl = str_replace(array('http://www.celebrities-jewelry.com', $this->baseUrl), '', $_POST['feature']); } $DATA = <<< EOD '{$_POST['slug']}', 'name' => '{$_POST['name']}', 'kana' => '{$_POST['kana']}', 'dec' => '{$_POST['dec']}', 'feature' => '{$featureUrl}', 'officialUrl' => '{$_POST['officialUrl']}', 'storeUrl' => '{$_POST['storeUrl']}', 'public' => {$public}, 'password' => '{$_POST['password']}' ); EOD; try { //新規に設定の場合はディレクトリも作成 if($addFlag){ if(!file_exists($this->dataDir.'/'.$_POST['slug'])){ mkdir($this->dataDir.'/'.$_POST['slug']); chmod($this->dataDir.'/'.$_POST['slug'], 0755); } if(!file_exists($this->dataDir.'/'.$_POST['slug'].'/inc')){ mkdir($this->dataDir.'/'.$_POST['slug'].'/inc'); chmod($this->dataDir.'/'.$_POST['slug'].'/inc', 0755); } if(!file_exists($this->dataDir.'/'.$_POST['slug'].'/img')){ mkdir($this->dataDir.'/'.$_POST['slug'].'/img'); chmod($this->dataDir.'/'.$_POST['slug'].'/img', 0755); } foreach($this->pageTitle as $k => $v){ if(!file_exists($this->dataDir.'/'.$_POST['slug'].'/img/'.$k)){ mkdir($this->dataDir.'/'.$_POST['slug'].'/img/'.$k); chmod($this->dataDir.'/'.$_POST['slug'].'/img/'.$k, 0755); if($k == 'wedding'){ mkdir($this->dataDir.'/'.$_POST['slug'].'/img/wedding/engagement'); chmod($this->dataDir.'/'.$_POST['slug'].'/img/wedding/engagement', 0755); mkdir($this->dataDir.'/'.$_POST['slug'].'/img/wedding/marriage'); chmod($this->dataDir.'/'.$_POST['slug'].'/img/wedding/marriage', 0755); } } } } //設定ファイルに書き出し $filename = $this->dataDir.'/'.$_POST['slug'].'/inc/config.inc'; $fp = @fopen($filename, "w"); flock($fp, LOCK_EX); fwrite($fp, $DATA); flock($fp, LOCK_UN); fclose($fp); $res = array('result' => 'success'); } catch (Exception $e) { //throw $e; $res = array('result' => 'fail', 'state' => 'write file error', 'error' => $e); } return $res; } /* * History * -------------------------------- */ public function saveBrandHistory(){ $DATA = <<< EOD '{$_POST['data']['imgCaption']['01']}', '02' => '{$_POST['data']['imgCaption']['02']}', '03' => '{$_POST['data']['imgCaption']['03']}', '04' => '{$_POST['data']['imgCaption']['04']}' ); EOD; try { //設定ファイルに書き出し $filename = $this->dataDir.'/'.$_POST['slug'].'/inc/history.inc'; $fp = @fopen($filename, "w"); flock($fp, LOCK_EX); fwrite($fp, $DATA); flock($fp, LOCK_UN); fclose($fp); $res = array('result' => 'success'); } catch (Exception $e) { //throw $e; $res = array('result' => 'fail', 'error' => $e); } return $res; } /* * Topics * -------------------------------- */ public function saveBrandTopics(){ //ジュエリーデーター $jData = '' ; for($i = 1; $i <= self::MAX_JEWELRY_RINGS; $i++){ if(!empty($_POST['data']['jewelry'][$i])){ $j = $_POST['data']['jewelry'][$i]; $price = ''; if(!empty($j['price'])){ foreach($j['price'] as $k => $v){ if(!empty($v['price']) && !empty($v['material'])){ $kara = $v['kara'] == 1 ? 'true' : 'false'; $price .= <<< EOD {$k} => array( 'price' => {$v['price']}, 'kara' => {$kara}, 'material' => '{$v['material']}'), EOD; } } } $listPrice = !empty($j['listPrice']) ? $j['listPrice'] : 0; if(!empty($j['name']) && !empty($price)){ $jData .= <<< EOD \$cajBrandContents['jewelry'][{$i}] = array( 'name' => '{$j['name']}', 'subName' => '{$j['subName']}', 'dec' => '{$j['dec']}', 'price' => array( {$price} ), 'listPrice' => {$listPrice}, 'priceText' => '{$j['priceText']}', 'url' => array( 'ring' => '{$j['url']['ring']}', 'store' => '{$j['url']['store']}' ) ); EOD; } } } $DATA = <<< EOD '{$_POST['data']['imgCaption']['main']['title']}', 'body' => '{$_POST['data']['imgCaption']['main']['body']}' ); //Jewelry Data {$jData} EOD; try { //設定ファイルに書き出し $filename = $this->dataDir.'/'.$_POST['slug'].'/inc/topics.inc'; $fp = @fopen($filename, "w"); flock($fp, LOCK_EX); fwrite($fp, $DATA); flock($fp, LOCK_UN); fclose($fp); $res = array('result' => 'success'); } catch (Exception $e) { //throw $e; $res = array('result' => 'fail', 'error' => $e); } return $res; } /* * Wedding * -------------------------------- */ public function saveBrandWedding(){ $DATA = <<< EOD '{$_POST['data']['imgCaption']['main']}', 'sub01' => '{$_POST['data']['imgCaption']['sub01']}', 'sub02' => '{$_POST['data']['imgCaption']['sub02']}' ); EOD; try { //設定ファイルに書き出し $filename = $this->dataDir.'/'.$_POST['slug'].'/inc/wedding.inc'; $fp = @fopen($filename, "w"); flock($fp, LOCK_EX); fwrite($fp, $DATA); flock($fp, LOCK_UN); fclose($fp); $res = array('result' => 'success'); } catch (Exception $e) { //throw $e; $res = array('result' => 'fail', 'error' => $e); } return $res; } /* * Weding - Engagement&Marriage * -------------------------------- */ public function saveRings($type){ $imgDir = $this->dataDir.'/'.$_POST['slug'].'/img/wedding/'.$type.'/'; if ($handle = opendir($imgDir)) { while(false !== ($f = readdir($handle))){ if ($f != "." && $f != "..") { rename($imgDir.$f, $imgDir.'_'.$f); } } closedir($handle); } //ジュエリーデーター $jData = ''; for($i = 1; $i <= self::MAX_WEDDING_RINGS; $i++){ if(!empty($_POST['data'][$type][$i])){ $j = $_POST['data'][$type][$i]; if(!empty($j['name'])){ $f = sprintf('%03d', $i); if($i != $j['order']){ if(file_exists($imgDir.'_'.sprintf('%03d', $j['order']))) rename($imgDir.'_'.sprintf('%03d', $j['order']), $imgDir.$f); } else { if(file_exists($imgDir.'_'.$f)) rename($imgDir.'_'.$f, $imgDir.$f); } $price = ''; if(!empty($j['price'])){ foreach($j['price'] as $k => $v){ if(!empty($v['price']) && !empty($v['material'])){ $kara = $v['kara'] == 1 ? 'true' : 'false'; $price .= <<< EOD {$k} => array( 'price' => {$v['price']}, 'kara' => {$kara}, 'material' => '{$v['material']}'), EOD; } } } $listPrice = !empty($j['listPrice']) ? $j['listPrice'] : 0; $jData .= <<< EOD \$cajBrandContents['{$type}'][{$i}] = array( 'order' => {$i}, 'name' => '{$j['name']}', 'subName' => '{$j['subName']}', 'dec' => '{$j['dec']}', 'price' => array( {$price} ), 'listPrice' => {$listPrice}, 'priceText' => '{$j['priceText']}', 'url' => array( 'ring' => '{$j['url']['ring']}', 'store' => '{$j['url']['store']}' ) ); EOD; } } } $DATA = <<< EOD dataDir.'/'.$_POST['slug'].'/inc/'.$_POST['mode'].'.inc'; $fp = @fopen($filename, "w"); flock($fp, LOCK_EX); fwrite($fp, $DATA); flock($fp, LOCK_UN); fclose($fp); $res = array('result' => 'success'); } catch (Exception $e) { //throw $e; $res = array('result' => 'fail', 'error' => $e); } return $res; } /* * 本番に反映する * -------------------------------- */ private function deployBrandData($_slug){ if($this->copyDirectory($this->dataDir.'/'.$_slug, $this->regularDataDir.'/'.$_slug)){ $res = array('result' => 'success'); } else { $res = array('result' => 'fail', 'error' => $e); } return $res; } private function copyDirectory($src, $dst){ try { $this->copyFiles($src, $dst); return true; } catch (Exception $e) { return false; } } private function copyFiles($src, $dst){ if(is_dir($src)){ mkdir($dst); $files = scandir($src); foreach($files as $file){ if(($file != '.') && ($file != '..')) $this->copyFiles($src.'/'.$file, $dst.'/'.$file); } } else if(file_exists($src)) { copy($src, $dst); } } } ?>