protected function executeImpl($type, $name, $key) { $post = Vizualizer::request(); if ($post["delete"]) { // サイトデータを取得する。 $loader = new Vizualizer_Plugin($type); $model = $loader->loadModel($name); $model->findByPrimaryKey($post[$key]); // データが検索された場合のみ削除処理を実施 if ($model->{$key} > 0) { // トランザクションデータベースの取得 $connection = Vizualizer_Database_Factory::begin(strtolower($type)); try { $model->delete(); // エラーが無かった場合、処理をコミットする。 Vizualizer_Database_Factory::commit($connection); // 画面をリロードする。 if (!$this->continue) { // 登録に使ったキーを無効化 $this->removeInput("delete"); $this->removeInput($key); $this->reload(); } } catch (Exception $e) { Vizualizer_Database_Factory::rollback($connection); throw new Vizualizer_Exception_Database($e); } } } }
protected function executeImpl($params, $type, $name, $key) { if (!$params->check("upload") || isset($_POST[$params->get("upload")])) { $loader = new Vizualizer_Plugin($type); // アップされたファイルのデータを取得する。 if ($_FILES[$key]["error"] == UPLOAD_ERR_OK) { // Excelファイルを読み込む $book = PHPExcel_IOFactory::load($_FILES[$key]["tmp_name"]); // 処理を実行する $data = $this->process($params, $book); // トランザクションの開始 $connection = Vizualizer_Database_Factory::begin(strtolower($type)); try { foreach ($data as $item) { $model = $loader->loadModel($name); foreach ($item as $col => $value) { $model->{$col} = $value; } $model->save(); } // エラーが無かった場合、処理をコミットする。 Vizualizer_Database_Factory::commit($connection); // 画面をリロードする。 if (!$this->continue) { // 登録に使用したキーを無効化 $this->removeInput("upload"); $this->reload(); } } catch (Exception $e) { Vizualizer_Database_Factory::rollback($connection); throw new Vizualizer_Exception_Database($e); } } } }
public function execute() { // クエリのビルド $sql = $this->buildQuery(); // クエリを実行する。 try { $connection = Vizualizer_Database_Factory::begin($this->module); Vizualizer_Logger::writeDebug($sql); $result = $connection->query($sql); } catch (Exception $e) { Vizualizer_Logger::writeError($sql, $e); throw new Vizualizer_Exception_Database($e); } return $result; }
protected function executeImpl($type, $name, $primary_key) { $post = Vizualizer::request(); if ($post["add"] || $post["save"]) { // サイトデータを取得する。 $loader = new Vizualizer_Plugin($type); $model = $loader->loadModel($name); if (!empty($post[$this->key_prefix . $primary_key])) { $model->findByPrimaryKey($post[$this->key_prefix . $primary_key]); if (!($model->{$primary_key} > 0)) { $model = $loader->loadModel($name, array($primary_key => $post[$this->key_prefix . $primary_key])); } } foreach ($post as $key => $value) { if (!empty($this->key_prefix)) { if (substr($key, 0, strlen($this->key_prefix)) == $this->key_prefix) { $key = preg_replace("/^" . $this->key_prefix . "/", "", $key); $model->{$key} = $value; } } else { $model->{$key} = $value; } } // トランザクションの開始 $connection = Vizualizer_Database_Factory::begin(strtolower($type)); try { $model->save(); if (!empty($this->key_prefix)) { $post->set($this->key_prefix . $primary_key, $model->{$primary_key}); } else { $post->set($primary_key, $model->{$primary_key}); } // エラーが無かった場合、処理をコミットする。 Vizualizer_Database_Factory::commit($connection); // 画面をリロードする。 if (!$this->continue) { // 登録に使用したキーを無効化 $this->removeInput("add"); $this->removeInput("save"); $this->reload(); } } catch (Exception $e) { Vizualizer_Database_Factory::rollback($connection); throw new Vizualizer_Exception_Database($e); } } }
protected function executeImpl($params, $type, $name, $key) { if (!$params->check("upload") || isset($_POST[$params->get("upload")])) { $loader = new Vizualizer_Plugin($type); // アップされたファイルのデータを取得する。 if ($_FILES[$key]["error"] == UPLOAD_ERR_OK) { if (($fp = fopen($_FILES[$key]["tmp_name"], "r")) !== FALSE) { // 1行目はタイトル行とする。 $data = $this->getCsvData($fp); if ($this->checkTitle($data)) { $line = 2; // トランザクションの開始 $connection = Vizualizer_Database_Factory::begin(strtolower($type)); try { while (($data = $this->getCsvData($fp)) !== FALSE) { $model = $loader->loadModel($name); $model = $this->check($line, $model, $data); if ($model != null) { $model->save(); } $line++; } if (count($this->errors) > 0) { throw new Vizualizer_Exception_Invalid($key, $this->errors); } // エラーが無かった場合、処理をコミットする。 Vizualizer_Database_Factory::commit($connection); // 画面をリロードする。 if (!$this->continue) { // 登録に使用したキーを無効化 $this->removeInput("upload"); $this->reload(); } } catch (Exception $e) { Vizualizer_Database_Factory::rollback($connection); throw new Vizualizer_Exception_Database($e); } } else { throw new Vizualizer_Exception_Invalid($key, array("アップされたファイルのタイトル行が正しくありません")); } fclose($fp); } } } }
public function fetch($limit = null, $offset = null) { // クエリのビルド $sql = $this->buildQuery(); // クエリの検索件数を制限する。 if ($limit != null) { $sql .= " LIMIT " . $limit; if ($offset != null) { $sql .= " OFFSET " . $offset; } } // クエリを実行する。 try { $sql = $this->showQuery(); Vizualizer_Logger::writeDebug($sql); $connection = Vizualizer_Database_Factory::conn($this->module); $result = $connection->query($sql); } catch (Exception $e) { Vizualizer_Logger::writeError($sql, $e); throw new Vizualizer_Exception_Database($e); } return new Vizualizer_Query_Select_Result($result); }
/** * フィールドを文字列として扱った場合にフィールド名となるようにする。 * * @return string クラスの文字列表現 */ public function __toString() { // DBの接続を取得する。 $connection = Vizualizer_Database_Factory::conn($this->module); // カラム名をエスケープする。 return $this->table . "." . $connection->escapeIdentifier($this->field); }
/** * 注文詳細のデータを元に在庫の引き当てを実施 */ public function provision() { $loader = new Vizualizer_Plugin("stock"); $menu = $loader->loadModel("Menu"); $menu->findBy(array("set_id" => $this->set_id, "choice_id" => $this->choice_id)); if ($menu->menu_id > 0 && $menu->fixed_flg == "1") { // トランザクションの開始 $connection = Vizualizer_Database_Factory::begin("stock"); try { // メニューが確定されている場合は引き当てを実行 $components = $menu->components(); foreach ($components as $component) { if ($component->quantity <= 0) { break; } $quantity = $component->quantity; $purchase = $loader->loadModel("Purchase"); $purchases = $purchase->findAllBy(array("material_id" => $component->material_id, "purchase_status" => "stocked"), "production_date", false); foreach ($purchases as $purchase) { if ($quantity < $purchase->volume - $purchase->consumed) { $purchase->consumed += $quantity; $quantity = 0; } else { $quantity -= $purchase->volume - $purchase->consumed; $purchase->consumed = $purchase->volume; $purchase->purchase_status = "consumed"; } $purchase->save(); } } $this->provision_flg = 1; $this->save(); Vizualizer_Database_Factory::commit($connection); } catch (Exception $e) { Vizualizer_Database_Factory::rollback($connection); throw new Vizualizer_Exception_Database($e); } } }
/** * データベースの書き込み用接続を取得します。 * * @param string $code データベース設定の元となるキー * @return Vizualizer_Database_Connection データベースの接続 */ private static function getConnection($code = "default", $mode = self::MODE_WRITE) { // プロセスIDを取得する。 $pid = getmypid(); // DBのコネクションが設定されていない場合は接続する。 if (!array_key_exists($pid . "_" . $code . "_" . $mode, self::$connections)) { $confs = Vizualizer_Database_Factory::getConfigure($code); $conf = $confs[$mode]; try { // 設定に応じてDBに接続 switch ($conf["dbtype"]) { case "mysql": self::$connections[$pid . "_" . $code . "_" . $mode] = new Vizualizer_Database_Mysql_Connection($conf); break; case "sqlite": self::$connections[$pid . "_" . $code . "_" . $mode] = new Vizualizer_Database_Sqlite_Connection($conf); break; } } catch (PDOException $e) { // 接続に失敗した場合にはデータベース例外を発行 throw new Vizualizer_Exception_Database($e); } } return Vizualizer_Database_Factory::$connections[$pid . "_" . $code . "_" . $mode]; }
/** * 現在の状態で挿入クエリを実行する。 * * @param s array $values 挿入データの連想配列 */ public function execute($values) { try { $connection = Vizualizer_Database_Factory::begin($this->module); $sql = $this->showQuery($values); Vizualizer_Logger::writeDebug($sql); $result = $connection->query($sql); } catch (Exception $e) { Vizualizer_Logger::writeError($sql, $e); throw new Vizualizer_Exception_Database($e); } return $result; }
/** * レジからの注文データを取り込む。 * @param $params バッチ自体のパラメータ * @param $data バッチで引き回すデータ * @return バッチで引き回すデータ */ protected function importOrders($params, $data) { try { // Use the us-west-2 region and latest version of each client. $sharedConfig = array('region' => 'ap-northeast-1', 'version' => 'latest'); // Create an SDK class used to share configuration across clients. $sdk = new Aws\Sdk($sharedConfig); // Create an Amazon S3 client using the shared configuration data. $client = $sdk->createS3(); if (count($params) > 3) { if ($params[3] == "today") { $time = time(); } elseif ($params[3] == "yesterday") { $time = strtotime("-1 day"); } else { $time = strtotime($params[3]); } } else { $time = time(); } // Get the object $result = $client->getObject(array('Bucket' => "oder-report", 'Key' => "150/" . date("Ym", $time) . "/sales-" . date("Y-m-d", $time) . ".csv")); Vizualizer_Logger::writeDebug($result['Body']); $lines = explode("\n", $result['Body']); $columns = array(); $data = array(); foreach ($lines as $index => $line) { if (!empty($line)) { if ($index > 0) { $item = str_getcsv($line); $record = array(); foreach ($columns as $i => $key) { $record[$key] = $item[$i]; } $data[] = $record; } else { $columns = str_getcsv($line); } } } Vizualizer_Logger::writeDebug(print_r($data, true)); $sets = array(); foreach ($data as $item) { if ($item["type"] == "summary") { // トランザクションの開始 $connection = Vizualizer_Database_Factory::begin("stock"); try { $loader = new Vizualizer_Plugin("stock"); $model = $loader->loadModel("Order"); $model->findByPrimaryKey($item["order_id"]); if (!($model->order_id > 0)) { $model = $loader->loadModel("Order", array("order_id" => $item["order_id"])); } $model->user_id = $item["user_id"]; $model->payment_type = $item["payment_type"]; $model->order_date = $item["purchase_date"]; $model->price = $item["price"]; $model->save(); // エラーが無かった場合、処理をコミットする。 Vizualizer_Database_Factory::commit($connection); } catch (Exception $e) { Vizualizer_Database_Factory::rollback($connection); throw new Vizualizer_Exception_Database($e); } } if ($item["type"] == "set") { $sets[$item["order_id"] . "-" . $item["set_id"]] = $item; // トランザクションの開始 $connection = Vizualizer_Database_Factory::begin("stock"); try { $loader = new Vizualizer_Plugin("stock"); $model = $loader->loadModel("OrderDetail"); $model->order_id = $item["order_id"]; $model->set_id = $item["set_id"]; $model->set_menu_name = $item["set_menu_name"]; $model->order_date = $item["purchase_date"]; $model->price = $item["price"]; $model->quantity = $item["count"]; $model->save(); $model = $loader->loadModel("Menu"); $model->findBy(array("set_id" => $item["set_id"], "choice_id" => 0)); $model->set_id = $item["set_id"]; $model->set_menu_name = $sets[$item["order_id"] . "-" . $item["set_id"]]["set_menu_name"]; $model->menu_name = $item["menu_name"]; $model->price = $item["price"]; $model->fixed_flg = "1"; $model->save(); // エラーが無かった場合、処理をコミットする。 Vizualizer_Database_Factory::commit($connection); } catch (Exception $e) { Vizualizer_Database_Factory::rollback($connection); throw new Vizualizer_Exception_Database($e); } // 在庫の引き当てを実施 $model = $loader->loadModel("OrderDetail"); $models = $model->findAllBy(array("set_id" => $item["set_id"], "ne:provision_flg" => "1")); foreach ($models as $model) { $model->provision(); } } if ($item["type"] == "choice") { // トランザクションの開始 $connection = Vizualizer_Database_Factory::begin("stock"); try { $loader = new Vizualizer_Plugin("stock"); $model = $loader->loadModel("OrderDetail"); $model->order_id = $item["order_id"]; $model->set_id = $item["set_id"]; $model->choice_id = $item["choice_id"]; $model->set_menu_name = $sets[$item["order_id"] . "-" . $item["set_id"]]["set_menu_name"]; $model->menu_name = $item["menu_name"]; $model->price = $item["price"]; $model->quantity = $sets[$item["order_id"] . "-" . $item["set_id"]]["count"]; $model->save(); $model = $loader->loadModel("Menu"); $model->findBy(array("set_id" => $item["set_id"], "choice_id" => $item["choice_id"])); $model->set_id = $item["set_id"]; $model->choice_id = $item["choice_id"]; $model->set_menu_name = $sets[$item["order_id"] . "-" . $item["set_id"]]["set_menu_name"]; $model->menu_name = $item["menu_name"]; $model->price = $item["price"]; $model->fixed_flg = "1"; $model->save(); // エラーが無かった場合、処理をコミットする。 Vizualizer_Database_Factory::commit($connection); } catch (Exception $e) { Vizualizer_Database_Factory::rollback($connection); throw new Vizualizer_Exception_Database($e); } // 在庫の引き当てを実施 $model = $loader->loadModel("OrderDetail"); $models = $model->findAllBy(array("set_id" => $item["set_id"], "choice_id" => $item["choice_id"], "ne:provision_flg" => "1")); foreach ($models as $model) { $model->provision(); } } } } catch (S3Exception $e) { echo $e->getMessage() . "\n"; } return $data; }
/** * テーブルエイリアス名を設定する。 * * @param string $tableName エイリアス名 * @return Vizualizer_Plugin_Table テーブルオブジェクト */ public function setAlias($tableName) { // DBの接続を取得する。 $connection = Vizualizer_Database_Factory::getConnection($this->module, true); // エイリアスの設定に応じて、テーブル内の各変数を調整 $this->_C = $connection->escapeIdentifier($tableName); $this->_T = $this->_B . " AS " . $connection->escapeIdentifier($tableName); $this->_W = $this->_C . ".*"; return $this; }
/** * 起動処理です。 * 設定ファイルを読み込み、システムの設定を行います。 */ public static function start() { // SERVER_NAMEが未設定の場合はlocalhostを割当 if (!isset($_SERVER["SERVER_NAME"])) { $serverName = getenv("PHP_SERVER_NAME"); if (!empty($serverName)) { $_SERVER["SERVER_NAME"] = $serverName; } else { $_SERVER["SERVER_NAME"] = "localhost"; } } // デフォルトの設定 Vizualizer_Configure::set("timezone", "Asia/Tokyo"); Vizualizer_Configure::set("locale", "ja_JP.UTF-8"); Vizualizer_Configure::set("select_month", array("01" => "1月", "02" => "2月", "03" => "3月", "04" => "4月", "05" => "5月", "06" => "6月", "07" => "7月", "08" => "8月", "09" => "9月", "10" => "10月", "11" => "11月", "12" => "12月")); $selectDay = array(); for ($i = 1; $i <= 31; $i++) { $selectDay[sprintf("%02d", $i)] = $i . "日"; } Vizualizer_Configure::set("select_day", array("01" => "1日", "02" => "2日", "03" => "3日", "04" => "4日", "05" => "5日", "06" => "6日", "07" => "7日", "08" => "8日", "09" => "9日", "10" => "10日", "11" => "11日", "12" => "12日", "13" => "13日", "14" => "14日", "15" => "15日", "16" => "16日", "17" => "17日", "18" => "18日", "19" => "19日", "20" => "20日", "21" => "21日", "22" => "22日", "23" => "23日", "24" => "24日", "25" => "25日", "26" => "26日", "27" => "27日", "28" => "28日", "29" => "29日", "30" => "30日", "31" => "31日")); $selectHour = array(); $selectHalfHour = array(); for ($i = 0; $i <= 23; $i++) { $hh = sprintf("%02d", $i); $selectHour[$hh . ":00"] = $i . ":00"; $selectHalfHour[$hh . ":00"] = $i . ":00"; $selectHalfHour[$hh . ":30"] = $i . ":30"; } Vizualizer_Configure::set("select_hour", array("00:00" => "0:00", "01:00" => "1:00", "02:00" => "2:00", "03:00" => "3:00", "04:00" => "4:00", "05:00" => "5:00", "06:00" => "6:00", "07:00" => "7:00", "08:00" => "8:00", "09:00" => "9:00", "10:00" => "10:00", "11:00" => "11:00", "12:00" => "12:00", "13:00" => "13:00", "14:00" => "14:00", "15:00" => "15:00", "16:00" => "16:00", "17:00" => "17:00", "18:00" => "18:00", "19:00" => "19:00", "20:00" => "20:00", "21:00" => "21:00", "22:00" => "22:00", "23:00" => "23:00")); Vizualizer_Configure::set("select_half_hour", array("00:00" => "0:00", "00:30" => "0:30", "01:00" => "1:00", "01:30" => "1:30", "02:00" => "2:00", "02:30" => "2:30", "03:00" => "3:00", "03:30" => "3:30", "04:00" => "4:00", "04:30" => "4:30", "05:00" => "5:00", "05:30" => "5:30", "06:00" => "6:00", "06:30" => "6:30", "07:00" => "7:00", "07:30" => "7:30", "08:00" => "8:00", "08:30" => "8:30", "09:00" => "9:00", "09:30" => "9:30", "10:00" => "10:00", "10:30" => "10:30", "11:00" => "11:00", "11:30" => "11:30", "12:00" => "12:00", "12:30" => "12:30", "13:00" => "13:00", "13:30" => "13:30", "14:00" => "14:00", "14:30" => "14:30", "15:00" => "15:00", "15:30" => "15:30", "16:00" => "16:00", "16:30" => "16:30", "17:00" => "17:00", "17:30" => "17:30", "18:00" => "18:00", "18:30" => "18:30", "19:00" => "19:00", "19:30" => "19:30", "20:00" => "20:00", "20:30" => "20:30", "21:00" => "21:00", "21:30" => "21:30", "22:00" => "22:00", "22:30" => "22:30", "23:00" => "23:00", "23:30" => "23:30")); Vizualizer_Configure::set("debug", true); Vizualizer_Configure::set("debug_level", 99); Vizualizer_Configure::set("display_error", "On"); Vizualizer_Configure::set("session_manager", ""); // プラグインとテンプレートのパス if (!file_exists(VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "templates")) { mkdir(VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "templates"); chmod(VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "templates", 0777); } Vizualizer_Configure::set("site_home", VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "templates"); if (!file_exists(VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "_logs")) { mkdir(VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "_logs"); chmod(VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "_logs", 0777); } Vizualizer_Configure::set("log_root", VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "_logs"); Vizualizer_Configure::set("upload_root", VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "upload"); Vizualizer_Configure::set("max_logs", 100); // データベースの接続設定 Vizualizer_Configure::set("database", array()); // memcacheのホスト設定 Vizualizer_Configure::set("memcache", ""); // コンテンツのmemcache利用設定 Vizualizer_Configure::set("memcache_contents", false); // JSONインターフェイス用キー設定 Vizualizer_Configure::set("json_key", ""); // Facebookのプロトコル Vizualizer_Configure::set("facebook_protocol", "http"); // FacebookのAPP ID Vizualizer_Configure::set("facebook_app_id", ""); // FacebookのAPP Secret Vizualizer_Configure::set("facebook_app_secret", ""); // サイトコード Vizualizer_Configure::set("site_code", "default"); // サイト名 Vizualizer_Configure::set("site_name", "デフォルトサイト"); // サイトドメイン Vizualizer_Configure::set("site_domain", $_SERVER["SERVER_NAME"]); // デフォルトのテンプレートエンジン Vizualizer_Configure::set("template", "Smarty"); // 設定ファイルを読み込み $siteDomain = Vizualizer_Configure::get("site_domain"); if (file_exists(VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "_configure" . DIRECTORY_SEPARATOR . "configure_" . $siteDomain . ".php")) { require VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "_configure" . DIRECTORY_SEPARATOR . "configure_" . $siteDomain . ".php"; } else { // 一つ上の階層の設定がある場合はそちらを見に行く。 list($dummy, $siteDomain) = explode(".", $siteDomain, 2); if (!empty($siteDomain) && file_exists(VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "_configure" . DIRECTORY_SEPARATOR . "configure_" . $siteDomain . ".php")) { require VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "_configure" . DIRECTORY_SEPARATOR . "configure_" . $siteDomain . ".php"; } elseif (file_exists(VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "_configure" . DIRECTORY_SEPARATOR . "configure.php")) { // ホスト別の設定が無い場合はデフォルトの設定ファイルを使用する。 require VIZUALIZER_SITE_ROOT . DIRECTORY_SEPARATOR . "_configure" . DIRECTORY_SEPARATOR . "configure.php"; } } // データベースを初期化する。 Vizualizer_Database_Factory::initialize(Vizualizer_Configure::get("database")); }