示例#1
0
 /**
  * (non-PHPdoc)
  * 
  * @see \Simplify\Form\Element::onPostData()
  */
 public function onPostData(Action $action, &$data, $post)
 {
     $value = sy_get_param($post, $this->getName(), $this->getDefaultValue());
     $value = str_replace($this->thousandsSeparator, '', $value);
     $value = str_replace($this->decimalSeparator, '.', $value);
     $value = floatval($value);
     $data[$this->getName()] = $value;
 }
示例#2
0
 /**
  * Return a new instance of \Simplify\Thumb with optional params
  *
  * @param mixed $params
  * @return \Simplify\Thumb
  */
 public static function factory($params = null)
 {
     $thumb = new self();
     if ($params !== false) {
         $thumb->baseDir = sy_get_param($params, 'baseDir', \Simplify::config()->get('www_dir'));
         $thumb->filesPath = sy_get_param($params, 'filesPath', \Simplify::config()->get('files_path'));
         $thumb->cachePath = sy_get_param($params, 'cachePath', $thumb->filesPath . 'cache/');
     }
     return $thumb;
 }
示例#3
0
 /**
  * 
  * @param \Simplify\MatchedRoute $match
  * @throws \Exception
  * @return unknown
  */
 protected function forward(\Simplify\MatchedRoute $match)
 {
     $options = $match->route->getOptions();
     if (!isset($options['controller'])) {
         throw new \Exception('No controller defined in route');
     }
     $controller = sy_get_param($match->params, 'controller', $options['controller']);
     $action = sy_get_param($match->params, 'action', sy_get_param($options, 'action', 'index'));
     $Controller = new $controller();
     $output = $Controller->callAction($action, $match->params);
     return $output;
 }
示例#4
0
 protected function prepareRoute()
 {
     $defaults = (array) sy_get_param($this->options, 'defaults');
     $patterns = (array) sy_get_param($this->options, 'patterns');
     $words = preg_split('#(/[^/]+)#', $this->uri, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
     $regex = array();
     $names = array();
     $required = array();
     $index = 0;
     foreach ($words as $i => $word) {
         if ($word == '/*') {
             $regex[] = '(?:(/.*))?';
             $patterns['extra'] = '#^(.*)?#';
             $names[$index++] = 'extra';
             $words[$i] = '/:extra';
             break;
         } elseif (strpos($word, '/:') === 0) {
             $wildcard = substr($word, 2);
             $names[$index++] = $wildcard;
             if (isset($patterns[$wildcard])) {
                 if (isset($defaults[$wildcard]) || preg_match('#' . $patterns[$wildcard] . '#', '')) {
                     $regex[] = '(?:/(' . $patterns[$wildcard] . '))?';
                 } else {
                     $regex[] = '/(' . $patterns[$wildcard] . ')';
                     $required[] = $wildcard;
                 }
                 $patterns[$wildcard] = '#^' . $patterns[$wildcard] . '$#';
             } elseif (array_key_exists($wildcard, $defaults)) {
                 $regex[] = '(?:/([^/]+))?';
                 $patterns[$wildcard] = '#^([^/]+)?$#';
             } else {
                 $required[] = $wildcard;
                 $regex[] = '/([^/]+)';
                 $patterns[$wildcard] = '#^[^/]+$#';
             }
         } else {
             $regex[] = $word;
         }
     }
     $regex = '#^' . implode('', $regex) . '$#';
     $this->uri = implode('', $words);
     $this->regex = $regex;
     $this->defaults = $defaults;
     $this->patterns = $patterns;
     $this->names = $names;
     $this->required = $required;
 }
示例#5
0
 public function getError($id, $default = null)
 {
     return sy_get_param($this->errors, $id, $default);
 }
示例#6
0
 /**
  * (non-PHPdoc)
  *
  * @see Component::onCollectTableData()
  */
 public function onCollectTableData(\Simplify\Form\Action $action, &$row, $data)
 {
     $row[$this->getBeginField()] = sy_get_param($data, $this->getBeginField());
     $row[$this->getEndField()] = sy_get_param($data, $this->getEndField());
 }
示例#7
0
/**
 * Find the key of an element in an array by one of it's properties
 *
 * @param mixed[] $array
 * @param string $key
 * @param mixed $value
 * @return mixed
 */
function sy_find_key($array, $key, $value)
{
    $found = false;
    foreach ($array as $k => $item) {
        if (sy_get_param($item, $key) === $value) {
            $found = $k;
            break;
        }
    }
    return $found;
}
示例#8
0
 /**
  * (non-PHPdoc)
  * @see \Simplify\Form\Element::onPostData()
  */
 public function onPostData(\Simplify\Form\Action $action, &$data, $post)
 {
     $id = $data[$this->getReferenceColumn()];
     $data[$this->getName()] = array();
     if (!empty($post[$this->getName()])) {
         $position = 0;
         $data[$this->getName()][\Simplify\Form::ID] = sy_get_param($post[$this->getName()], \Simplify\Form::ID);
         $data[$this->getName()][$this->getForeignKeyColumn()] = $id;
         $elements = $this->getElements($action);
         while ($elements->valid()) {
             $elements->current()->onPostData($action, $data[$this->getName()], $post[$this->getName()]);
             $elements->next();
         }
     }
 }
示例#9
0
 /**
  *
  * @param array $data
  * @return mixed
  */
 public function save(&$data)
 {
     $id = sy_get_param($data, $this->pk);
     if (empty($id)) {
         return $this->insert($data);
     } else {
         return $this->update($data);
     }
 }
示例#10
0
 /**
  * (non-PHPdoc)
  * @see Component::onCollectTableData()
  */
 public function onCollectTableData(Action $action, &$row, $data)
 {
     $row[$this->getFieldName()] = sy_get_param($data, $this->getName(), $this->getDefaultValue());
 }
 /**
  *
  * @param mixed[] $rules
  */
 protected function parse($rules)
 {
     foreach ($rules as $name => $rule) {
         if (empty($rule)) {
             continue;
         }
         if (is_array($rule[0])) {
             foreach ($rule as $_rule) {
                 $this->setRule($name, $this->factory($_rule[0], $_rule[1], sy_get_param($_rule, 2)));
             }
         } elseif (!$rule instanceof \Simplify\ValidationInterface) {
             $this->setRule($name, $this->factory($rule[0], $rule[1], sy_get_param($rule, 2)));
         } else {
             $this->setRule($name, $rule);
         }
     }
 }
示例#12
0
 protected static function output($asset, $group = null)
 {
     $url = self::$assets[$asset]['url'];
     $output = sy_get_param(self::$assets[$asset], 'output');
     switch (substr($url, strrpos($url, '.'))) {
         case '.css':
             if (empty($output)) {
                 $output = "<link rel=\"stylesheet\" href=\"{$url}\" />";
             }
             break;
         case '.js':
             if (empty($output)) {
                 $output = "<script src=\"{$url}\"></script>";
             }
             break;
         default:
             $output = $url;
     }
     self::$assets[$asset]['output'] = true;
     return $output;
 }
示例#13
0
 /**
  * Get/set a temporary session variable.
  * Call Session::flash($name, $value) to set it's value.
  * Call Session::flash($name) to get it's value and delete the variable.
  *
  * @param string $name
  * @param mixed $value Optional
  * @return mixed
  */
 public function flash($name, $value = null)
 {
     if (func_num_args() === 1) {
         return isset($_SESSION['__flash__']) ? sy_get_param($_SESSION['__flash__'], $name) : null;
     } else {
         $_SESSION['__flash__'][$name] = $value;
     }
 }
示例#14
0
 public function loadSchema()
 {
     if ($this->exists()) {
         $_columns = \Simplify::db()->query("SHOW COLUMNS FROM {$this->name}")->execute()->fetchAll();
         $this->columns = array();
         $this->indexes = array();
         $this->constraints = array();
         foreach ($_columns as &$_column) {
             $column = new \Simplify\Db\Schema\Column($_column['Field']);
             if (preg_match('/([a-z]+)(?:\\((.+)\\))?( unsigned)*( zerofill)*/', $_column['Type'], $type)) {
                 $column->type = $type[1];
                 $column->size = $type[2];
                 $column->unsigned = !empty($type[3]);
                 $column->zerofill = !empty($type[4]);
             }
             if (preg_match('/auto_increment/', $_column['Extra'])) {
                 $column->autoIncrement = true;
             }
             $column->field = $_column['Field'];
             $column->null = $_column['Null'] == 'YES';
             $column->default = $_column['Default'];
             $this->columns[$_column['Field']] = $column;
             $this->_schema['columns'][$_column['Field']] = $column;
         }
         $_indexes = \Simplify::db()->query("SHOW INDEX FROM {$this->name}")->execute()->fetchAll();
         foreach ($_indexes as $_index) {
             if (empty($this->indexes[$_index['Key_name']])) {
                 $index = new \Simplify\Db\Schema\Index();
                 $index->unique = empty($_index['Non_unique']);
                 $this->indexes[$_index['Key_name']] = $index;
             }
             $this->indexes[$_index['Key_name']]->fields[$_index['Seq_in_index']] = $_index['Column_name'];
             $this->_schema['indexes'][$_index['Key_name']] = $this->indexes[$_index['Key_name']];
         }
         $db = sy_get_param(\Simplify::db()->getParams(), 'name');
         $sql = "\n        SELECT CONSTRAINT_NAME, TABLE_NAME, COLUMN_NAME,\n          REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME\n        FROM information_schema.KEY_COLUMN_USAGE\n\t\t\t\tWHERE TABLE_SCHEMA = '{$db}' AND REFERENCED_TABLE_NAME IS NOT NULL\n          AND (REFERENCED_TABLE_NAME = '{$this->name}' OR TABLE_NAME = '{$this->name}')\n      ";
         $_constraints = \Simplify::db()->query($sql)->execute()->fetchAll();
         foreach ($_constraints as $_constraint) {
             $constraint = new \Simplify\Db\Schema\Constraint();
             $constraint->table = $_constraint['TABLE_NAME'];
             $constraint->column = $_constraint['COLUMN_NAME'];
             $constraint->referencedTable = $_constraint['REFERENCED_TABLE_NAME'];
             $constraint->referencedColumn = $_constraint['REFERENCED_COLUMN_NAME'];
             $this->constraints[$_constraint['CONSTRAINT_NAME']] = $constraint;
             $this->_schema['constraints'][$_constraint['CONSTRAINT_NAME']] = $constraint;
         }
     }
 }
示例#15
0
 /**
  * (non-PHPdoc)
  * @see \Simplify\Form\Element::getDisplayValue()
  */
 public function getDisplayValue(\Simplify\Form\Action $action, $data, $index)
 {
     return sy_get_param($this->getOptions(), $this->getValue($data));
 }
示例#16
0
 /**
  * (non-PHPdoc)
  * @see \Simplify\Form\Action::onLoadData()
  */
 protected function onLoadData()
 {
     $elements = $this->getElements();
     $name = $this->getNameField();
     $value = $this->getValueField();
     $params = array();
     $params[\Simplify\Db\QueryParameters::SELECT][] = $value;
     $this->formData = array();
     $elements->rewind();
     while ($elements->valid()) {
         $element = $elements->current();
         $elements->next();
         $params[\Simplify\Db\QueryParameters::WHERE][] = "{$name} = :{$name}";
         $params[\Simplify\Db\QueryParameters::DATA] = array($name => $element->getFieldName());
         $row = array();
         $row['found'] = \Simplify::db()->query()->from($this->getTable())->setParams($params)->execute()->fetchRow();
         $row['data'][$element->getFieldName()] = sy_get_param($row['found'], $value);
         $this->formData[$element->getName()] = $row;
         $element->onLoadData($this, $this->formData[$element->getName()]['data'], $row['data']);
     }
 }
示例#17
0
 protected function __removeBranch($id)
 {
     $dao = Simplify::db();
     $data = $dao->query()->from($this->table)->select("{$this->left}, {$this->right}, {$this->right} - {$this->left} + 1 AS width, {$this->parent}")->where("{$this->pk} = ?")->execute($id)->fetchRow();
     if (empty($data)) {
         throw new Simplify\Db\MpttException("Row not found in table <b>{$this->table}</b> where <b>{$this->pk}</b> = <b>{$id}</b>");
     }
     $left_id = (int) sy_get_param($data, $this->left);
     $right_id = (int) sy_get_param($data, $this->right);
     $width = (int) sy_get_param($data, 'width');
     $parent_id = (int) sy_get_param($data, $this->parent, '0');
     $ids = $dao->query()->from($this->table)->select($this->pk)->where("{$this->left} BETWEEN ? AND ?")->execute($left_id, $right_id)->fetchCol();
     $_ids = implode(', ', $ids);
     $dao->query("UPDATE {$this->table} SET {$this->right} = {$this->right} - ? WHERE {$this->right} > ? AND {$this->pk} NOT IN ({$_ids})")->execute($width, $right_id);
     $dao->query("UPDATE {$this->table} SET {$this->left} = {$this->left} - ? WHERE {$this->left} > ? AND {$this->pk} NOT IN ({$_ids})")->execute($width, $right_id);
     return array('ids' => $ids, 'width' => $width, 'left' => $left_id, 'right' => $right_id);
 }
示例#18
0
 /**
  * Get the base url
  *
  * @return string
  */
 public function base()
 {
     return ($this->secure() ? 'https' : 'http') . '://' . sy_get_param($_SERVER, 'HTTP_HOST');
 }
示例#19
0
 /**
  * Fill form data with data sent via POST
  */
 public function onPostData()
 {
     $post = $this->form->getPostData();
     $id = $this->form->getId();
     $elements = $this->getElements();
     $filters = $this->form->getFilters();
     foreach ($this->formData as $index => &$row) {
         $row[Form::ID] = sy_get_param($id, $index);
         $elements->rewind();
         while ($elements->valid()) {
             $element = $elements->current();
             $element->onPostData($this, $row, $post[$index]);
             $elements->next();
         }
         foreach ($filters as $filter) {
             $filter->onPostData($this, $row, $post[$index]);
         }
     }
 }
示例#20
0
 /**
  * Tries to delete the image file and all related cached thumbnails
  *
  * @param array $data form row
  */
 protected function onDelete(&$data)
 {
     if ($this->deleteFile) {
         $file = sy_get_param($data, $this->getFieldName());
         if (!empty($file)) {
             if (!sy_path_is_absolute($file)) {
                 $file = \Simplify::config()->get('www:dir') . $file;
             }
             if (file_exists($file)) {
                 @unlink($file);
             }
         }
     }
 }
 /**
  * (non-PHPdoc)
  * @see \Simplify\Form\Element::onPostData()
  */
 public function onPostData(\Simplify\Form\Action $action, &$data, $post)
 {
     $data[$this->getName()] = (array) sy_get_param($post, $this->getName());
 }
示例#22
0
 public function onRender(\Simplify\Form\Action $action)
 {
     $data = $action->get('data');
     foreach ($data as &$row) {
         if (sy_get_param($row, \Simplify\Form::ID) == 1) {
             $row['elements']['groups']['label'] = __('Grupos');
             $row['elements']['groups']['controls'] = '<p class="form-control-static">' . __('Este usuário pertence a todos os grupos') . '</p>';
             $row['elements']['permissions']['label'] = __('Permissões');
             $row['elements']['permissions']['controls'] = '<p class="form-control-static">' . __('Este usuário tem todas as permissões') . '</p>';
         }
     }
     $action->set('data', $data);
 }
示例#23
0
 /**
  * Find the cache filename
  *
  * @param string $id cache name
  * @return string cache filename
  */
 protected function findFile($id)
 {
     $found = glob($this->path . '/' . $id . '*.php');
     return sy_get_param((array) $found, 0);
 }
示例#24
0
 /**
  * (non-PHPdoc)
  * @see Simplify\Db\DatabaseInterface::connect()
  */
 public function connect()
 {
     if (empty($this->db)) {
         $dsn = $this->dsn['driver'] . ':host=' . $this->dsn['host'] . ';dbname=' . $this->dsn['database'] . ';charset=' . $this->dsn['charset'];
         try {
             $this->db = new \PDO($dsn, sy_get_param($this->dsn, 'username'), sy_get_param($this->dsn, 'password'), $this->options);
         } catch (\PDOException $e) {
             throw new DatabaseException('Database connection failed with message: ' . $e->getMessage());
         }
     }
     return $this;
 }
示例#25
0
 /**
  * (non-PHPdoc)
  * @see \Simplify\Form\Element::onPostData()
  */
 public function onPostData(\Simplify\Form\Action $action, &$data, $post)
 {
     $a = sy_get_param(sy_get_param($post, $this->getName(), array()), 'a');
     $b = sy_get_param(sy_get_param($post, $this->getName(), array()), 'b');
     $c = sy_get_param(sy_get_param($post, $this->getName(), array()), 'c');
     $empty = '';
     $exists = !empty($data[\Simplify\Form::ID]) || $this->exists === true;
     $required = $this->required && !$exists;
     if ($this->askForConfirmation && $a != $b) {
         $this->errors['_'][] = $this->getError('match', __('As senhas não conferem'));
     } elseif ($required && $a == $empty) {
         $this->errors['_'][] = $this->getError('empty', __('Informe a senha'));
     } elseif ($this->matchOriginal) {
         if ($a != $data[$this->getName()]) {
             $this->errors['_'][] = $this->getError('original', __('Senha incorreta'));
         }
     } elseif ($a != $empty) {
         $data[$this->getName()] = $this->hash($a);
     } else {
         unset($data[$this->getName()]);
     }
 }
示例#26
0
 /**
  * (non-PHPdoc)
  *
  * @see \Simplify\Form\Action::onExtractData()
  */
 protected function onExtractData(&$data, $row)
 {
     $calendarAction = \Simplify::request()->get('calendarAction');
     if ($calendarAction == 'data') {
         $data['title'] = $row['title'];
         $data['start'] = $row['start'];
         $data['end'] = $row['end'];
         $data['allDay'] = sy_get_param($row, 'allDay');
     }
     $data['editUrl'] = $this->url()->format('html')->set('calendarAction', 'edit')->set(Form::ID, $data[Form::ID])->build();
     $data['deleteUrl'] = $this->url()->format('html')->set('calendarAction', 'delete')->set(Form::ID, $data[Form::ID])->build();
 }
示例#27
0
 /**
  * Read an option and return it's value
  *
  * @param string $name            
  * @param mixed $default
  *            default return value if option does not exist
  * @return mixed
  */
 public static function value($name, $default = null)
 {
     try {
         $option = self::read($name);
         if (empty($option)) {
             return $default;
         }
         $str = sy_get_param($option, 'option_value', $default);
         $uns = @unserialize($str);
         $value = $str == serialize(false) || $uns !== false ? $uns : $str;
         return $value;
     } catch (\Simplify\Db\TableNotFoundException $e) {
         return $default;
     }
 }
示例#28
0
 /**
  * (non-PHPdoc)
  * @see \Simplify\Form\Repository::insert()
  */
 public function insert(&$data)
 {
     return $this->mptt()->append($data, sy_get_param($data, $this->parent, 0));
 }
示例#29
0
 /**
  * Match an uri.
  * 
  * Uri example:
  * /posts/:id/*
  *    :id - matches named parameter id
  *    * - extra parameter, matches everything at the end of the uri 
  * 
  * Options:
  * array(
  *    'controller' => '\Namespace\TheController', // controller class
  *    'action' => 'index', // controller action
  *    'patterns' => array( // array of patterns to match against uri named parameters
  *        'id' => '^\d+' // regular expression to match against id
  *    ),
  *    'defaults' => array( // default values for parameters
  *        'id' => 123
  *    ),
  *    'as' => 'post' // name this route
  * )
  * 
  * @param string $uri the url to match
  * @param array $options
  * @return \Simplify\Route
  */
 public function match($uri, $options = null)
 {
     $route = new Route($uri, $options);
     $priority = sy_get_param($options, 'priority', 0);
     if (!isset($this->routes[$priority])) {
         $this->routes[$priority] = array();
     }
     $this->routes[$priority][] = $route;
     if (isset($options['as'])) {
         $this->named[$options['as']] = $route;
     }
     return $route;
 }