/**
  * @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();
 }
Exemplo n.º 2
0
 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($action)
 {
     $action = 'execute' . Inflection::hungaryNotationToCamel($action);
     if (method_exists($this, $action)) {
         $this->getEventDispatcher()->dispatch('onBeginControllerExecute', new Event($this, array('action' => $action)));
         $this->beforeExecute();
         $this->{$action}();
         $this->afterExecute();
         $this->getEventDispatcher()->dispatch('onAfterControllerExecute', new Event($this, array('action' => $action)));
     } else {
         Base::end('ERROR: task ' . Inflection::hungaryNotationToCamel($this->_name) . ':' . $action . ' not existed!' . PHP_EOL);
     }
 }
Exemplo n.º 4
0
 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;
 }
Exemplo n.º 5
0
 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();
     }
 }
 public function __call($method, $params)
 {
     foreach ($this->_behaviors as $behavior) {
         if ($behavior->getEnable() && method_exists($behavior, $method)) {
             return call_user_func_array(array($behavior, $method), $params);
         }
     }
     if (strrpos($method, 'set') === 0 && isset($params[0]) && null !== $params[0]) {
         $name = Inflection::camelCaseToHungary(substr($method, 3, strlen($method)));
         if (isset(static::$_cols[$name])) {
             $this->_data[$name] = $this->fixData($params[0], static::$_schema[$name]);
             $this->_modifiedCols[$name] = true;
         } else {
             $this->{$name} = $params[0];
         }
         return true;
     }
     if (strpos($method, 'get') === 0) {
         $name = Inflection::camelCaseToHungary(substr($method, 3, strlen($method)));
         if (in_array($name, static::$_cols)) {
             return isset($this->_data[$name]) ? $this->_data[$name] : null;
         }
         return $this->{$name};
     }
     $lcMethod = strtolower($method);
     if (substr($lcMethod, 0, 6) == 'findby') {
         $by = substr($method, 6, strlen($method));
         $method = 'findBy';
         $one = false;
     } else {
         if (substr($lcMethod, 0, 9) == 'findoneby') {
             $by = substr($method, 9, strlen($method));
             $method = 'findOneBy';
             $one = true;
         }
     }
     if ($method == 'findBy' || $method == 'findOneBy') {
         if (isset($by)) {
             if (!isset($params[0])) {
                 throw new Exception('You must specify the value to ' . $method);
             }
             /*if ($one) {
                   $fieldName = static::_resolveFindByFieldsName($by);
                   if(false == $fieldName) {
                       throw new Exception('Column ' .$fieldName .' not found!');
                   }
               }*/
             return static::findBy($by, $params, $one);
         }
     }
     if (substr($lcMethod, 0, 10) == 'retrieveby') {
         $by = substr($method, 10, strlen($method));
         $method = 'retrieveBy';
         if (isset($by)) {
             if (!isset($params[0])) {
                 return false;
                 //@FIXED not need throw exception
                 //throw new Exception('You must specify the value to ' . $method);
             }
             return static::retrieveBy($by, $params);
         }
     }
     //        return parent::$method($params);
 }
Exemplo n.º 7
0
 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;
 }
Exemplo n.º 8
0
 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;
 }
 public function run()
 {
     $this->beforeRun();
     $this->getEventDispatcher()->dispatch('onBeginRequest', new Event($this));
     if (null == $this->_task) {
         throw new Exception("Missing 'task' parameter!");
     }
     $camelName = Inflection::hungaryNotationToCamel($this->_task);
     $class = $this->getAppNamespace() . '\\Task\\' . $camelName;
     $taskPath = TASK_DIR . 'Task/' . str_replace('\\', DIRECTORY_SEPARATOR, $camelName) . '.php';
     if (file_exists($taskPath)) {
         //            require_once $file;
         $this->_controller = new $class($this->_task, $taskPath);
         $this->_controller->execute($this->_act);
         $this->_finished = true;
     } else {
         Base::end("ERROR: task {$this->_task} ({$taskPath}/{$class}Task.php) not existed" . PHP_EOL);
     }
     $this->getEventDispatcher()->dispatch('onEndRequest', new Event($this));
     $this->afterRun();
 }
Exemplo n.º 10
0
 public function run()
 {
     $tables = $this->_getTablesList();
     $this->package = Inflection::hungaryNotationToCamel($this->package);
     echo "\n\n";
     for ($i = 0, $size = sizeof($tables); $i < $size; ++$i) {
         $table = $tables[$i];
         $this->_generateBaseModel($table);
         $this->_generateModel($table);
     }
 }
Exemplo n.º 11
0
 public function run()
 {
     if (null == $this->_conn) {
         die('Connection fail');
     }
     $tables = $this->_getTablesList();
     $this->package = Inflection::hungaryNotationToCamel($this->package);
     echo "\n\n";
     $tableDestination = array();
     for ($i = 0, $size = sizeof($tables); $i < $size; ++$i) {
         /*$ignore = $this->_isInIgnoreList($tables[$i]);
         
                     $allow = $this->_isInAllowList($tables[$i]);
         
                     $build = (!$ignore) && $allow;
         
                     if (!$build) continue;*/
         $schemaName = str_replace(' ', '', ucwords(str_replace('_', ' ', trim(str_replace($this->tbPrefix, '', $tables[$i]), '_'))));
         array_push($tableDestination, $tables[$i]);
         $schema = $this->_generateSchemas($tables[$i], $schemaName);
         echo " -- Generate schema " . $schema . " success \n";
     }
     $this->getSpecialTbl() != false ? $note = '[SOME]' : ($note = '[ALL]');
     echo "\nAre you sure you want to generate " . $note . " OMs ?\nType 'yes' to continue or 'no' to abort': ";
     $handle = fopen("php://stdin", "r");
     if (trim(fgets($handle)) != 'yes') {
         echo 'Gen OMs aborted!';
         exit;
     }
     $nextRunning = "php command gen:models --config=" . $this->_configKey;
     if ($this->getSpecialTbl() != false) {
         $tableToModels = @implode(',', $tableDestination);
         $nextRunning .= " --table=" . $tableToModels;
     }
     echo shell_exec($nextRunning);
     exit;
 }
Exemplo n.º 12
0
 /**
  * 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)));
 }
Exemplo n.º 13
0
 public function _generateBaseController()
 {
     $temp = "<?php" . PHP_EOL;
     $appName = Inflection::hungaryNotationToCamel($this->appName);
     $class = $this->appName . 'Base';
     $webController = 'ApiController';
     $temp .= 'namespace ' . $appName . '\\Controller;' . PHP_EOL;
     $temp .= 'use Flywheel\\Controller\\ApiController;' . PHP_EOL;
     $temp .= 'abstract class ' . $class . ' extends ' . $webController . '{' . PHP_EOL . '' . PHP_EOL . '}' . PHP_EOL;
     $fs = new Filesystem\Filesystem();
     $destinationDir = $this->appDir . 'Controller' . DIRECTORY_SEPARATOR . $class . '.php';
     if ($fs->exists($destinationDir) === false) {
         $fs->dumpFile($destinationDir, $temp);
         echo "-- " . $class . " is generated success !\n";
     } else {
         echo "-- " . $class . " is exists, aborted !\n";
     }
 }