Example #1
0
 public function __construct($clazz, $config = NULL)
 {
     $this->app = \Bono\App::getInstance();
     $this->clazz = $clazz;
     $collection = \Norm\Norm::factory($clazz);
     $this->schema = $collection->schema();
     $globalConfig = $this->app->config('component.table');
     if (!isset($config['actions'])) {
         if (isset($globalConfig['mapping'][$clazz]['actions'])) {
             $config['actions'] = $globalConfig['mapping'][$clazz]['actions'];
         } elseif (isset($globalConfig['default']['actions'])) {
             $config['actions'] = $globalConfig['default']['actions'];
         } else {
             $config['actions'] = array('read' => NULL, 'update' => NULL, 'delete' => NULL);
         }
     }
     if (!isset($config['columns'])) {
         if (isset($globalConfig['mapping'][$clazz]['columns'])) {
             $config['columns'] = $globalConfig['mapping'][$clazz]['columns'];
         } elseif (isset($globalConfig['default']['columns'])) {
             $config['columns'] = $globalConfig['default']['columns'];
         }
     }
     if (empty($this->schema) && empty($config['columns'])) {
         throw new \Exception('Plain table needs collection schema or "component.table" configuration!');
     }
     $this->config = $config;
     $this->view = new \Slim\View();
     $this->view->setTemplatesDirectory(realpath(dirname(__FILE__) . '/../../../templates'));
     $this->view->set('self', $this);
 }
Example #2
0
 public function authenticateRemoteUser($remoteUser)
 {
     $app = \App::getInstance();
     $authorized = f('auth.remoteAuthorize', $remoteUser);
     if ($authorized) {
         $users = \Norm\Norm::factory('User');
         $user = $users->findOne(array('username' => $remoteUser['username']));
         if (is_null($user)) {
             $user = $users->newInstance();
             $user['username'] = $remoteUser['username'];
             $user['first_name'] = $remoteUser['first_name'];
             $user['last_name'] = $remoteUser['last_name'];
             $user['birth_date'] = $remoteUser['birth_date'];
             $user['birth_place'] = $remoteUser['birth_place'];
         }
         if (!empty($remoteUser['normalized_username'])) {
             $user['normalized_username'] = $remoteUser['normalized_username'];
         }
         $user['email'] = $remoteUser['email'];
         $user['sso_account_id'] = $remoteUser['$id'];
         $user->save();
     } else {
         throw new \Exception('You are unauthorized to access this application. Please contact administrator.');
     }
     return $user->toArray();
 }
Example #3
0
 /**
  * Overloading method to convert this implementation to a string.
  *
  * @return string
  */
 public function __toString()
 {
     if ($tz = Norm::options('tz')) {
         $this->setTimezone(new DateTimeZone($tz));
     }
     return $this->format('c');
 }
Example #4
0
 public function removed($model)
 {
     $histCollection = Norm::factory($model->getClass() . 'History');
     $history = $histCollection->newInstance();
     $history['model_id'] = $model['$id'];
     $history['type'] = 'remove';
     $history->save();
 }
Example #5
0
 public function __construct($clazz, $config = NULL)
 {
     $this->app = \Bono\App::getInstance();
     $this->clazz = $clazz;
     $collection = \Norm\Norm::factory($clazz);
     $this->schema = $collection->schema();
     $this->globalConfig = $this->app->config('component.form');
     $this->config = isset($this->globalConfig['mapping'][$this->clazz]) ? $this->globalConfig['mapping'][$this->clazz] : NULL;
 }
 public function read($slug)
 {
     $entry = Norm::factory('Git')->findOne(array('slug' => $slug));
     if (is_null($entry)) {
         return $this->app->notFound();
     }
     $this->data['entry'] = $entry;
     $this->response->template('git/read');
 }
 protected function createSlug($repo)
 {
     $counter = (int) Norm::factory('Git')->find(array('repo', $repo))->count();
     $repoName = preg_replace("/[^\\da-z]/i", '-', $repo);
     $slug = strtolower($repoName);
     if ($counter > 0) {
         $counter++;
         $slug = strtolower($repoName) . '-' . (string) $counter;
     }
     return $slug;
 }
 public function initialize()
 {
     $app = $this->app;
     $app->get('/', function () use($app) {
         $query = $app->request->get('q') ?: '';
         $gits = Norm::factory('Git')->find()->sort(array('$updated_time' => -1))->match($query)->limit(10);
         $count = Norm::factory('Git')->find()->count();
         $app->response->set('gits', $gits);
         $app->response->set('query', $query);
         $app->response->set('count', $count);
         $app->response->template('home');
         $app->view->setLayout('layout');
     });
 }
 public function formatInput($value, $entry = null)
 {
     if ($format = $this['inputFormat'] and is_callable($format)) {
         return $format($value, $entry, $this);
     }
     $foreign = Norm::factory($this['foreign']);
     if ($this['readonly']) {
         $criteria = array('group' => $this->get('foreignGroup'), $this->get('foreignKey') => $value);
         $entry = $foreign->findOne($criteria);
         $label = is_null($entry) ? null : $entry->get($this->get('foreignLabel'));
         return '<span class="field">' . $label . '</span>';
     }
     $entries = $foreign->find(array('group' => $this->get('foreignGroup')));
     return App::getInstance()->theme->partial('_schema/sysparam', array('self' => $this, 'value' => $value, 'entries' => $entries));
 }
Example #10
0
 public function authenticate(array $options = array())
 {
     $app = \App::getInstance();
     if (!isset($options['username']) && !isset($options['password'])) {
         return null;
     }
     $userCollection = \Norm\Norm::factory(@$this->options['userCollection'] ?: 'User');
     $user = $userCollection->findOne(array('!or' => array(array('username' => $options['username']), array('email' => $options['username']), array('normalized_username' => str_replace('.', '', $options['username'])))));
     if (function_exists('salt')) {
         $options['password'] = salt($options['password']);
     }
     if (is_null($user) || $user['password'] . '' !== $options['password']) {
         return null;
     }
     if (empty($options['keep'])) {
         $app->session->reset();
     } else {
         $app->session->reset(array('lifetime' => 365 * 24 * 60 * 60));
     }
     $_SESSION['user'] = $user->toArray();
     return $user->toArray();
 }
Example #11
0
 /**
  * Find reference of a foreign key.
  *
  * @param string $key
  * @param string $foreign
  * @param string $foreignLabel
  * @param string $foreignKey
  * @param int &$i
  *
  * @return string
  */
 public function getQueryReference($key = '', $foreign = '', $foreignLabel = '', $foreignKey = '', &$i)
 {
     $model = Norm::factory($foreign);
     $refSchemes = $model->schema();
     $foreignKey = $foreignKey ?: 'id';
     if ($foreignKey == '$id') {
         $foreignKey = 'id';
     }
     $query = $key . ' IN (SELECT ' . $foreignKey . ' FROM ' . strtolower($foreign) . ' WHERE ' . $foreignLabel . ' LIKE :f' . $i . ') ';
     $i++;
     return $query;
 }
Example #12
0
 /**
  * Initialize the provider
  */
 public function initialize()
 {
     $app = $this->app;
     $include = $app->request->get('!include');
     if (!empty($include)) {
         Norm::options('include', true);
     }
     $tz = $app->request->get('!tz');
     if (!empty($tz)) {
         Norm::options('tz', $tz);
     }
     if (!isset($this->options['datasources'])) {
         $this->options['datasources'] = $this->app->config('norm.datasources');
     }
     // DEPRECATED: norm.databases deprecated
     if (!isset($this->options['datasources'])) {
         $this->options['datasources'] = $this->app->config('norm.databases');
     }
     if (!isset($this->options['datasources'])) {
         throw new \Exception('[Norm] No data source configuration. Append "norm.datasources" bono configuration!');
     }
     if (!isset($this->options['collections'])) {
         $this->options['collections'] = $this->app->config('norm.collections');
     }
     Norm::init($this->options['datasources'], $this->options['collections']);
     $controllerConfig = $this->app->config('bono.controllers');
     if (!isset($controllerConfig['default'])) {
         $controllerConfig['default'] = 'Norm\\Controller\\NormController';
     }
     $this->app->config('bono.controllers', $controllerConfig);
     if (!class_exists('Norm')) {
         class_alias('Norm\\Norm', 'Norm');
     }
     $d = explode(DIRECTORY_SEPARATOR . 'src', __DIR__);
     $this->app->theme->addBaseDirectory($d[0], 10);
 }
Example #13
0
 public function formatPlain($value, $entry = null)
 {
     $value = $this->prepare($value);
     $label = '';
     if (is_null($value)) {
         return null;
     } elseif (is_array($this['foreign'])) {
         if (isset($this['foreign'][$value])) {
             $label = $this['foreign'][$value];
         }
     } elseif (is_callable($this['foreign'])) {
         $label = $this['foreign']($value);
     } else {
         $foreignEntry = Norm::factory($this['foreign'])->findOne(array($this['foreignKey'] => $value));
         if (is_string($this['foreignLabel'])) {
             $label = $foreignEntry[$this['foreignLabel']];
         } elseif (is_callable($this['foreignLabel'])) {
             $getLabel = $this['foreignLabel'];
             $label = $getLabel($foreignEntry);
         }
     }
     return $label;
 }
Example #14
0
 /**
  * Implement the json serializer normalizing the data structures.
  *
  * @return array
  */
 public function jsonSerialize()
 {
     if (!Norm::options('include')) {
         return $this->toArray();
     }
     $destination = array();
     $source = $this->toArray();
     $schema = $this->collection->schema();
     foreach ($source as $key => $value) {
         if (isset($schema[$key]) and isset($value)) {
             $destination[$key] = $schema[$key]->toJSON($value);
         } else {
             $destination[$key] = $value;
         }
         $destination[$key] = JsonKit::replaceObject($destination[$key]);
     }
     return $destination;
 }
Example #15
0
 /**
  * Register a route model.
  *
  * @param string $key
  *
  * @return mixed
  */
 public function routeModel($key)
 {
     if (!isset($this->routeModels[$key])) {
         $Clazz = Inflector::classify($key);
         $collection = Norm::factory($this->schema($key)->get('foreign'));
         $this->routeModels[$key] = $collection->findOne($this->routeData($key));
     }
     return $this->routeModels[$key];
 }
Example #16
0
 public function toJSON($value)
 {
     if (!is_string($this['foreign'])) {
         // FIXME should return translated value if non scalar foreign item
         return $value;
     }
     $foreignCollection = Norm::factory($this['foreign']);
     if (Norm::options('include')) {
         $foreignKey = $this['foreignKey'];
         $newValue = array();
         foreach ($value as $k => $v) {
             if (is_null($foreignKey)) {
                 $newValue[] = $foreignCollection->findOne($v);
             } else {
                 $newValue[] = $foreignCollection->findOne(array($this['foreignKey'] => $v));
             }
         }
         $value = $newValue;
     }
     return $value;
 }
Example #17
0
 public function filterUnique($key, $value, $data, $args = array())
 {
     if (is_null($value) || $value === '') {
         return '';
     }
     $clazz = $args[0];
     $field = isset($args[1]) ? $args[1] : $key;
     $model = \Norm\Norm::factory($clazz)->findOne(array($field => $value));
     if (isset($model) && $model['$id'] != $data['$id']) {
         throw FilterException::factory($key, 'Field %s must be unique')->args($this->rules[$key]['label']);
     }
     return $value;
 }