/** * Execute validation for all of request values * * @param array $data Request data * @return boolean */ public function validate($data) { foreach ($this->_rules as $item => $rules) { foreach ($rules as $ruleInfo) { $rule = $ruleInfo['rule']; $method = NameManager::convertToCamel($rule); if (!method_exists($this, $method)) { continue; } $message = ''; if (isset($ruleInfo['error_message'])) { $message = $ruleInfo['error_message']; } else { if (isset(self::$_messages[$rule])) { $message = self::$_messages[$rule]; } } if ($message == '') { throw new Exception('Error message of rule "' . $rule . '" is not specified.'); } $res = $this->{$method}($data[$item], $ruleInfo); if ($res == false) { foreach ($ruleInfo as $key => $val) { $message = str_replace('%' . $key . '%', $val, $message); } $this->_errors[$item] = $message; break; } } } if ($this->_errors) { return false; } return true; }
/** * Constructor * * @param HtmlForm $form * @return void */ public function __construct(HtmlForm $form) { $this->_parentForm = $form; $tagName = $this->_tagName; if ($this->_tagName == '') { // decide tag name from tail of class name require_once 'core/name_manager.php'; $parts = NameManager::split(get_class($this)); $tagName = array_pop($parts); } parent::__construct($tagName); }
/** * Execute output html using a layout template * * @return string */ protected function renderTemplateWithLayout() { ob_start(); extract($this->_vars); $dir = PathManager::getViewTemplateDirectory(); $ext = NameManager::getTemplateExtension(); $templatePath = sprintf('%s/%s.%s', $dir, $this->_template, $ext); if (!file_exists($templatePath)) { throw new FileNotExistException($templatePath); } require_once $templatePath; $inner_contents = ob_get_contents(); ob_clean(); $dir = PathManager::getViewLayoutDirectory(); $layoutPath = sprintf('%s/%s.%s', $dir, $this->_layout, $ext); if (!file_exists($layoutPath)) { throw new FileNotExistException($layoutPath); } require_once $layoutPath; return ob_get_clean(); }
/** * apply curry.ini settings * * @return boolean */ public function applyConfig() { Loader::load('Ini', 'core'); Loader::load('Db', 'db'); $ini = false; if ($this->_appEnv == null) { // Default section is "product" $ini = Ini::load('database.ini', 'product'); } else { $ini = Ini::load('database.ini', $this->_appEnv); } if ($ini === false) { // For the compatibility of a old version. $ini = Ini::load('database.ini', 'connection'); } if ($ini !== false) { Db::setConfig($ini); } $ini = Ini::load('curry.ini'); if ($ini === false) { return false; } if (array_key_exists('dispatch', $ini)) { $values = $ini['dispatch']; $key = 'plugin_enabled'; if (array_key_exists($key, $values)) { if (is_numeric($values[$key]) && $values[$key] == 1) { $this->dispatcher->enablePlugin(true); } } $key = 'is_send_404'; if (array_key_exists($key, $values)) { if (is_numeric($values[$key]) && $values[$key] != 1) { $this->dispatcher->isSend404(false); } } $key = 'sub_controller_enabled'; if (array_key_exists($key, $values)) { if (is_numeric($values[$key]) && $values[$key] == 1) { $this->router->enableSubController(true); } } $key = 'is_rewrite'; if (array_key_exists($key, $values)) { if (is_numeric($values[$key]) && $values[$key] != 1) { $this->router->isRewrite(false); } } $key = 'default_controller'; if (array_key_exists($key, $values)) { $this->router->setDefaultController($values[$key]); } $key = 'default_action'; if (array_key_exists($key, $values)) { $this->router->setDefaultAction($values[$key]); } $key = 'controller_query_key'; if (array_key_exists($key, $values)) { $this->router->setControllerQueryKey($values[$key]); } $key = 'action_query_key'; if (array_key_exists($key, $values)) { $this->router->setActionQueryKey($values[$key]); } $key = 'controller_suffix'; if (array_key_exists($key, $values)) { NameManager::setControllerSuffix($values[$key]); } $key = 'action_suffix'; if (array_key_exists($key, $values)) { NameManager::setActionSuffix($values[$key]); } } if (array_key_exists('view', $ini)) { $values = $ini['view']; $key = 'class_name'; if (array_key_exists($key, $values)) { $this->dispatcher->setViewClass($values[$key]); } $key = 'layout_enabled'; if (array_key_exists($key, $values)) { if (is_numeric($values[$key]) && $values[$key] != 1) { ViewAbstract::setDefaultLayoutEnabled(false); } } $key = 'template_extension'; if (array_key_exists($key, $values)) { NameManager::setTemplateExtension($values[$key]); } } if (array_key_exists('request', $ini)) { $values = $ini['request']; $key = 'auto_trim'; if (array_key_exists($key, $values)) { if (is_numeric($values[$key]) && $values[$key] == 1) { Request::setAutoTrim(true); } } } if (array_key_exists('database', $ini)) { $values = $ini['database']; $key = 'is_singleton'; if (array_key_exists($key, $values)) { if (is_numeric($values[$key]) && $values[$key] == 0) { Db::setIsSingleton(false); } } } if (array_key_exists('logger', $ini)) { Loader::load('Logger', 'utility'); $values = $ini['logger']; $key = 'system_log'; if (array_key_exists($key, $values)) { Logger::setLogName($values[$key]); } $key = 'query_log'; if (array_key_exists($key, $values)) { Logger::setLogName($values[$key], 'query'); Db::enableLogging('query'); } $key = 'output_level'; if (array_key_exists($key, $values)) { $val = strtolower(trim($values[$key])); if (is_numeric($val) == false) { $levels = array('debug' => LogLevel::DEBUG, 'info' => LogLevel::INFO, 'warn' => LogLevel::WARN, 'error' => LogLevel::ERROR, 'except' => LogLevel::EXCEPT); if (array_key_exists($val, $levels)) { $val = $levels[$val]; } else { $val = LogLevel::NO_OUTPUT; } } Logger::setOutputLevel($val, 'query'); } $key = 'max_line'; if (array_key_exists($key, $values)) { Logger::setMaxLine($values[$key]); } $key = 'max_generation'; if (array_key_exists($key, $values)) { Logger::setGeneration($values[$key]); } } if (array_key_exists('loader', $ini)) { $values = $ini['loader']; $key = 'autoload_dirs'; if (array_key_exists($key, $values)) { $dirs = explode(',', $values[$key]); Loader::addAutoloadDirectory($dirs); } } return true; }
/** * Include class file as autoload * * @param string $className * @return boolean */ public static function autoload($className) { $snake = NameManager::convertToSnake($className); $parts = explode('_', $snake); if (count($parts) == 0) { return false; } $fileName = NameManager::toPhpFile($className); $included = false; foreach (self::$_autoloadDirs as $dir) { $res = false; if (defined('CURRY_PATH')) { $res = @(include_once rtrim(CURRY_PATH, '/') . '/' . $fileName); } if ($res == false) { $res = @(include_once $dir . '/' . $fileName); } if ($res == true) { $included = true; break; } } if ($included == false) { $res = @(include_once $fileName); if ($res == false) { return false; } } return true; }
/** * Execute a SELECT statement using WHERE condition * created by the field names and values specified in the parameter * and return rows information as sql result. * * @param string $name Field names that combined by "And" * @param array $args Values for fields * @return array */ private function _selectByMethodName($name, $args) { $columns = explode('_and_', $name); $i = 0; $sel = $this->select(); foreach ($columns as $column) { $columnName = NameManager::toTable($column); $sel->where($columnName, $args[$i]); $i++; } if (count($args) > count($columns)) { $sel->order($args[$i]); } $rows = $sel->fetchAll(); return $rows; }
function dump_pub($filetype, $filename, $formatfile, $prefix) { $format = pub_format_parse_file($formatfile); $reader = new PubReader($format, $filename); $header = $reader->readHeader(); $names = new NameManager(); if ($header['type'] != $filetype) { throw new Exception("{$filetype} file is not an {$filetype} file!"); } for ($i = 0; $i < $header['num']; ++$i) { $entry = $reader->readNextEntry(); if ($entry['name'] == 'eof') { break; } $gender = null; if ($filetype == 'EIF') { if ($entry['type'] == 'Armor') { $gender = $entry['spec2']; } } $idname = $names->generateName($entry['name'], $gender); // Re-order unknowns to the bottom foreach (array_keys($entry) as $k) { if (substr($k, 0, 7) == 'unknown') { $v = $entry[$k]; unset($entry[$k]); $entry[$k] = $v; } if (substr($k, 0, 5) == 'Xdrop') { unset($entry[$k]); } } // Re-order ID and name to the top... $name = $entry['name']; unset($entry['name']); if ($filetype == 'ESF') { $shout = $entry['shout']; } if ($filetype == 'EIF') { $size = $entry['size']; } if ($filetype == 'EIF') { $weight = $entry['weight']; } if ($filetype == 'ESF') { unset($entry['shout']); } if ($filetype == 'EIF') { unset($entry['size']); } if ($filetype == 'EIF') { unset($entry['weight']); } $entry = array_reverse($entry); $entry['name'] = $name; if ($filetype == 'ESF') { $entry['shout'] = $shout; } if ($filetype == 'EIF') { $entry['size'] = $size; } if ($filetype == 'EIF') { $entry['weight'] = $weight; } $entry['id'] = $i + 1; $entry = array_reverse($entry); $cleanup = array(); foreach ($entry as $k => $v) { if ($v === 0) { $cleanup[$k] = $k; } } if ($filetype == 'EIF') { $cleanup['unknown3'] = 'unknown3'; } foreach ($cleanup as $k) { unset($entry[$k]); } $filename = "{$prefix}{$idname}.json"; echo "Dumping: {$filename}\n"; file_put_contents($filename, json_encode($entry, JSON_PRETTY_PRINT)); } }
/** * Create and add elements to form from array * * @param array $elementSettings */ public function buildFromArray($elementSettings) { $this->_checkArgumentIsArray(__METHOD__, 1, $elementSettings); // search layout element from child nodes $searchedLayout = null; foreach ($this->getNodes() as $elem) { if ($elem instanceof FormLayoutAbstract) { $searchedLayout = $elem; break; } } // create form elements foreach ($elementSettings as $setting) { $type = strtolower($setting['type']); unset($setting['type']); $method = 'create' . ucfirst($type); $formElem = null; if ($type == 'select' || $type == 'radio' || $type == 'checkboxes') { $formElem = $this->{$method}(null, $setting['list']); unset($setting['list']); } else { $formElem = $this->{$method}(); } foreach ($setting as $key => $val) { if ($key == 'layout' || $key == 'container') { continue; } if ($key == 'caption') { // set caption $formElem->setCaption($val); } else { // call setter method $method = NameManager::toMethod('set_' . strtolower($key)); $formElem->{$method}($val); } } // create layout element and add form element to there $layout = null; if (isset($setting['layout'])) { if ($setting['layout'] instanceof FormLayoutAbstract) { $layout = $setting['layout']; } else { if ($setting['layout'] === true && $searchedLayout instanceof FormLayoutAbstract) { $layout = $searchedLayout; } } } // add child node of form if ($layout instanceof FormLayoutAbstract) { $layout->addFormElement($formElem); } else { if (isset($setting['container']) && $setting['container'] instanceof HtmlElement) { $container = $setting['container']; $container->addElement($formElem); $this->addElement($container); } else { $this->addFormElement($formElem); } } } }
/** * Transfer the process to other action or controller * * @param string $action Action key you want to transfer * @param string $controller Controller key you want to transfer * @return void */ public function transfer($action, $controller = null) { if ($controller != null) { $this->view->setRenderingController($controller); } $this->view->setRenderingAction($action); $actionMethod = NameManager::convertActionToMethod($action); if ($controller == null) { $this->{$actionMethod}(); } else { $subdir = ''; $parts = explode('/', trim($controller, '/')); $partsCnt = count($parts); if ($partsCnt > 1) { $controller = $parts[$partsCnt - 1]; unset($parts[$partsCnt - 1]); $subdir = implode('/', $parts); } $className = NameManager::convertControllerToClass($controller); $ins = Loader::getControllerInstance($className, $subdir); $ins->setRequest($this->request); $ins->setView($this->view); $ins->{$actionMethod}(); } $this->dispatched = true; }
/** * Get an instance of the view script class for the controller name given in the parameter * * @param string $controllerName * @param string $subdir * @return ViewScript */ protected function _getViewScriptInstance($controllerName, $subdir) { $className = NameManager::convertControllerToViewClass($controllerName); $ins = Loader::getViewScriptInstance($className, $subdir); return $ins; }
/** * Execute output html using a layout template * * @return string */ protected function renderTemplateWithLayout() { foreach ($this->_vars as $key => $val) { $this->_smarty->assign($key, $val); } $ext = NameManager::getTemplateExtension(); $templateDir = PathManager::getViewTemplateDirectory(); $template = $this->_template . '.' . $ext; $templatePath = $templateDir . '/' . $template; if (!file_exists($templatePath)) { throw new FileNotExistException($templatePath); } $layoutDir = PathManager::getViewLayoutDirectory(); $layout = $this->_layout . '.' . $ext; $layoutPath = $layoutDir . '/' . $layout; if (!file_exists($layoutPath)) { throw new FileNotExistException($layoutPath); } $this->_smarty->template_dir = $templateDir; $contents = $this->_smarty->fetch($template); $this->_smarty->assign('inner_contents', $contents); $this->_smarty->template_dir = $layoutDir; $rendered = $this->_smarty->fetch($layout); return $rendered; }
/** * Set extension of view template file * * @param string $extension * @return void */ public static function setTemplateExtension($extension) { self::$_tplExtension = $extension; }
/** * Create connection instance that extends PDO * * @return DbAdapterAbstract * @throws Exception */ protected static function _connect() { $snake = 'db_adapter_' . strtolower(self::$_config['type']); $adapterName = NameManager::toClass($snake); Loader::load($adapterName, 'db'); if (!Loader::classExists($adapterName)) { throw new ClassUndefinedException($adapterName); } $instance = new $adapterName(self::$_config); return $instance; }
/** * Process when not found exception occurs * * @return void */ public function notFound() { $dir = PathManager::getViewTemplateDirectory(); $ext = NameManager::getTemplateExtension(); $exists = file_exists(sprintf('%s/error/not_found.%s', $dir, $ext)); if ($this->view instanceof ViewAbstract && $exists) { $this->view->setTemplate('not_found', 'error'); $this->view->enableLayout(false); $this->view->render(); } else { echo '404 Not Found'; } }
/** * Set layout template * * @param string $layout Layout template name * @return void */ public function setLayout($layout) { $ext = NameManager::getTemplateExtension(); $layout = preg_replace('/\\.' . $ext . '$/', '', $layout); $this->_layout = $layout; }