/** * @see BaseValidator::isValid() * * @param mixed $map * @param string $str * * @throws \Flywheel\Db\Exception * @return boolean */ public function isValid($map, $str) { if (!$map instanceof ActiveRecord) { throw new Exception('UniqueValidator require "$map" parameter much be ActiveRecord object'); } $where = array(); $params = array(); foreach ($str as $name => $rule) { $where[] = $map::getTableName() . ".{$name} = ?"; $getter = 'get' . Inflection::camelize($name); $params[] = $map->{$getter}(); } if (!$map->isNew()) { $where[] = $map::getTableName() . '.' . $map::getPrimaryKeyField() . ' != ?'; $params[] = $map->getPkValue(); } $where = implode(' AND ', $where); $fields = array_keys($str); foreach ($fields as &$field) { $field = $map->quote($field); } $data = $map::read()->select(implode(',', $fields))->where($where)->setMaxResults(1)->setParameters($params)->execute()->fetch(\PDO::FETCH_ASSOC); if ($data) { foreach ($data as $field => $value) { if ($map->{$field} == $value) { $map->setValidationFailure($map::getTableName() . $field, $str[$field]['message'], $this); } } } return !$map->hasValidationFailures(); }
protected function _rows($item) { $s = ''; $s .= '<tr class="post-row" id="post-' . $item->getId() . '">'; foreach ($this->columns as $name => $column) { if (is_int($name)) { $name = $column; } $class = []; if (isset($this->columns[$name]['htmlOption']['class'])) { $class[] = $this->columns[$name]['htmlOption']['class']; } $class[] = 'post-item'; $class[] = 'column-' . $name; $class = implode(' ', $class); $s .= '<td class="' . $class . '">'; if ('cb' == $name) { $s .= '<label> <input type="checkbox" name="bulk_actions[]" value="' . $item->id . '" class="check-list"> </label>'; } else { $method = '_column' . Inflection::camelize($name); if (method_exists($this, $method)) { $s .= $this->{$method}($item); } else { $s .= $this->_columnCustom($name, $item); } } $s .= '</td>'; } $s .= '</tr>'; return $s; }
public final function execute($regMethod, $method = null) { if (!$method) { throw new \Flywheel\Exception\Api('Api not found', 404); } $apiMethod = strtolower($regMethod) . Inflection::camelize($method); $this->getEventDispatcher()->dispatch('onBeginControllerExecute', new Event($this, array('action' => $apiMethod))); if (!method_exists($this, $apiMethod)) { throw new \Flywheel\Exception\Api('Api ' . Factory::getRouter()->getApi() . "/{$method} not found", 404); } $this->beforeExecute(); $buffer = $this->{$apiMethod}(); $this->afterExecute(); $this->getEventDispatcher()->dispatch('onAfterControllerExecute', new Event($this, array('action' => $apiMethod))); return $buffer; }
public function parseUrl($url) { $url = trim($url, '/'); if (null == $url) { throw new ApiException('Invalid request !', 404); } $segment = explode('/', $url); $_cf = explode('.', end($segment)); //check define format if (isset($_cf[1])) { $this->_format = $_cf[1]; $segment[count($segment) - 1] = $_cf[0]; } $size = sizeof($segment); $router = array(); for ($i = 0; $i < $size; ++$i) { $router[$i] = Inflection::camelize($segment[$i]); } for ($i = $size - 1; $i >= 0; --$i) { $router = array_slice($router, 0, $i + 1); $_camelName = implode("\\", $router); $_path = implode(DIRECTORY_SEPARATOR, $router); if (false !== file_exists($file = Base::getAppPath() . '/Controller/' . $_path . '.php')) { $this->_api = trim($_camelName, "\\"); break; } } if (null == $this->_api) { throw new ApiException('API not found', 404); } $segment = array_slice($segment, $i + 1); if (!empty($segment)) { $this->_method = array_shift($segment); $this->_params = !empty($segment) ? $segment : array(); } }
protected function _rows($item) { $s = ''; $s .= '<tr class="term-row" id="term-' . $item->getId() . '">'; foreach ($this->columns as $name => $column) { if (is_int($name)) { $name = $column; } $htmlOption = @$this->columns[$name]['htmlOption']; $htmlOption['class'] = $htmlOption['class'] ? $htmlOption['class'] . ' ' : ''; $htmlOption['class'] .= "term-item column-{$name}"; $s .= '<td ' . Html::serializeHtmlOption($htmlOption) . '>'; if ('cb' == $name) { $s .= '<label> <input type="checkbox" name="bulk_actions[]" value="' . $item->id . '" class="check-list"> </label>'; } else { $method = '_column' . Inflection::camelize($name); if (method_exists($this, $method)) { $s .= $this->{$method}($item); } else { $s .= $this->_columnCustom($name, $item); } } $s .= '</td>'; } $s .= '</tr>'; return $s; }
private function _parseControllers($route) { if (false === is_array($route)) { $route = explode('/', $route); } $_path = ''; $_camelName = ''; $size = sizeof($route); for ($i = 0; $i < $size; ++$i) { $route[$i] = Inflection::camelize($route[$i]); } for ($i = $size - 1; $i >= 0; --$i) { $_camelName = implode("\\", array_slice($route, 0, $i + 1)); $_path = implode(DIRECTORY_SEPARATOR, array_slice($route, 0, $i + 1)); if (false !== file_exists($file = Base::getAppPath() . '/Controller/' . $_path . '.php')) { $this->_camelControllerName = trim($_camelName, "\\"); break; } } return $i; }
private function _writeClassMagicMethod($class, $column, &$infos) { $s = ''; $name = Inflection::camelize($column); if ($infos['type'] == 'date' || $infos['type'] == 'datetime' || $infos['type'] == 'time' || $infos['type'] == 'timestamp') { $s .= ' * @method void set' . $name . '(\\Flywheel\\Db\\Type\\DateTime $' . $column . ') set' . $name . '(string $' . $column . ') set ' . $column . ' value' . PHP_EOL . ' * @method \\Flywheel\\Db\\Type\\DateTime get' . $name . '() get ' . $column . ' value' . PHP_EOL . ' * @method static \\' . $class . '[] findBy' . $name . '(\\Flywheel\\Db\\Type\\DateTime $' . $column . ') findBy' . $name . '(string $' . $column . ') find objects in database by ' . $column . PHP_EOL . ' * @method static \\' . $class . ' findOneBy' . $name . '(\\Flywheel\\Db\\Type\\DateTime $' . $column . ') findOneBy' . $name . '(string $' . $column . ') find object in database by ' . $column . PHP_EOL . ' * @method static \\' . $class . ' retrieveBy' . $name . '(\\Flywheel\\Db\\Type\\DateTime $' . $column . ') retrieveBy' . $name . '(string $' . $column . ') retrieve object from poll by ' . $column . ', get it from db if not exist in poll' . PHP_EOL . PHP_EOL; } else { $s .= ' * @method void set' . $name . '(' . $infos['type'] . ' $' . $column . ') set ' . $column . ' value' . PHP_EOL . ' * @method ' . $infos['type'] . ' get' . $name . '() get ' . $column . ' value' . PHP_EOL . ' * @method static \\' . $class . '[] findBy' . $name . '(' . $infos['type'] . ' $' . $column . ') find objects in database by ' . $column . PHP_EOL . ' * @method static \\' . $class . ' findOneBy' . $name . '(' . $infos['type'] . ' $' . $column . ') find object in database by ' . $column . PHP_EOL . ' * @method static \\' . $class . ' retrieveBy' . $name . '(' . $infos['type'] . ' $' . $column . ') retrieve object from poll by ' . $column . ', get it from db if not exist in poll' . PHP_EOL . PHP_EOL; } return $s; }
/** * Execute * * @param $action * @throws \Flywheel\Exception\NotFound404 * @return string component process result */ public final function execute($action) { $this->getEventDispatcher()->dispatch('onBeginControllerExecute', new Event($this, array('action' => $action))); $csrf_auto_protect = ConfigHandler::get('csrf_protection'); if (null === $csrf_auto_protect || $csrf_auto_protect) { if (!$this->request()->validateCsrfToken()) { Base::end('Invalid token'); } } /* @var \Flywheel\Router\WebRouter $router */ $router = Factory::getRouter(); $this->_action = $action; /* removed from version 1.1 * //set view file with action name * $this->_view = $this->_path .$action; */ $action = 'execute' . Inflection::camelize($action); if (!method_exists($this, $action)) { throw new NotFound404("Controller: Action \"" . $router->getController() . '/' . $action . "\" doesn't exist"); } $this->beforeExecute(); $this->filter(); $this->_beforeRender(); $this->view()->assign('controller', $this); $this->_buffer = $this->{$action}(); $this->_afterRender(); $this->afterExecute(); // assign current controller $this->view()->assign('controller', $this); $this->getEventDispatcher()->dispatch('onAfterControllerExecute', new Event($this, array('action' => $action))); }