ActiveRow is based on the great library NotORM http://www.notorm.com written by Jakub Vrana.
Автор: Jakub Vrana
Наследование: extends Nette\Object, implements IteratorAggregate, implements ArrayAccess
Пример #1
0
 public function submitFormEdit(Form $form)
 {
     $values = $form->getValues();
     $this->privilege->update(array('name' => $values->name, 'system_name' => $values->system_name));
     $this->flashMessage($this->translator->translate('admin.form.editSuccess'));
     $this->redirect('edit', $this->privilege->id);
 }
Пример #2
0
 public static function fromActiveRow(ActiveRow $row)
 {
     $version = new static();
     $version->id = $row->id;
     $version->version = $row->version;
     $version->license = $row->license;
     $version->distType = $row->distType;
     $version->distUrl = $row->distUrl;
     $version->sourceType = $row->sourceType;
     $version->sourceUrl = $row->sourceUrl;
     $version->sourceReference = $row->sourceReference;
     $version->composerJson = Json::decode($row->composerJson);
     // this may fail
     $version->updatedAt = $row->updatedAt;
     $linkTypes = self::getLinkTypes();
     $linkTypes = array_flip($linkTypes);
     foreach ($row->related('dependencies') as $dependencyRow) {
         $type = $dependencyRow->type;
         $type = $linkTypes[$type];
         $version->{$type}[$dependencyRow->packageName] = $dependencyRow->version;
         if ($dependencyRow->dependencyId) {
             $version->relatedAddons[$dependencyRow->packageName] = $dependencyRow->dependencyId;
         }
     }
     return $version;
 }
 private function updateAddon(ActiveRow $addon)
 {
     $this->writeln('Updating: ' . $addon->name);
     $github = $this->normalizeGithubUrl($addon->repository);
     if ($github) {
         $guzzle = new \Guzzle\Http\Client();
         try {
             $guzzle->get($github)->send();
             $this->db->table('addons_resources')->insert(array('addonId' => $addon->id, 'type' => 'github', 'resource' => $github));
         } catch (\Guzzle\Http\Exception\RequestException $e) {
             $this->writeln((string) $e);
         }
     }
     if ($addon->type === 'composer') {
         $version = $addon->related('versions')->order('id', 'DESC')->fetch();
         $composerData = Json::decode($version->composerJson);
         $packagist = $this->generatePackagistUrl($composerData->name);
         $guzzle = new \Guzzle\Http\Client();
         try {
             $guzzle->get($packagist . '.json')->send();
             $this->db->table('addons_resources')->insert(array('addonId' => $addon->id, 'type' => 'packagist', 'resource' => $packagist));
         } catch (\Guzzle\Http\Exception\RequestException $e) {
             $this->writeln((string) $e);
         }
     }
     if ($addon->demo) {
         $guzzle = new \Guzzle\Http\Client();
         try {
             $guzzle->get($addon->demo)->send();
             $this->db->table('addons_resources')->insert(array('addonId' => $addon->id, 'type' => 'demo', 'resource' => $addon->demo));
         } catch (\Guzzle\Http\Exception\RequestException $e) {
             $this->writeln((string) $e);
         }
     }
 }
Пример #4
0
 public function actionDelete($id)
 {
     $this->exist($id);
     $this->row->delete();
     $this->flashMessage($this->translator->translate('admin.text.itemDeleted'));
     $this->redirect('default');
 }
Пример #5
0
 /**
  * @param \Nette\Database\Table\ActiveRow
  * @return \Nette\Security\Identity
  */
 public function createIdentity(ActiveRow $user)
 {
     $data = $user->toArray();
     unset($data['password']);
     $role = strtolower($user->ref('users_groups', 'group_id')->g_title);
     return new Identity($user->id, $role, $data);
 }
Пример #6
0
 public function submitFormGeneratePassword(Form $form)
 {
     $values = $form->getValues();
     $this->userRow->update(array('password' => md5($values['password']), 'hash' => NULL));
     $this->flashMessage($this->translator->translate('admin.sign.passwordChanged'));
     $this->redirect('default');
 }
Пример #7
0
 /**
  * Adds all annexes for this directive.
  * @param object $dirRow annex data Nette\Database\Table\ActiveRow
  * @return void
  */
 public function addAnnex(ActiveRow $dirRow)
 {
     $annexData = $dirRow->related('annex.id')->order('order');
     foreach ($annexData as $annexRow) {
         $annex = new Annex($annexRow);
         $this->addComponent($annex, "annex" . $annexRow->order);
     }
 }
Пример #8
0
 public function submitFormEdit(Form $form)
 {
     $values = $form->getValues();
     $data = array('name' => $values['name'], 'email' => $values['email'], 'google_analytics' => $values['google_analytics'] == '' ? NULL : $values['google_analytics'], 'facebook_link' => $values['facebook_link'] == '' ? NULL : $values['facebook_link'], 'twitter_link' => $values['twitter_link'] == '' ? NULL : $values['twitter_link']);
     $this->row->update($data);
     $this->flashMessage($this->translator->translate('admin.form.editSuccess'));
     $this->redirect('this');
 }
Пример #9
0
 private static function loadTagsFromDb(DbEntry $photo)
 {
     $tags = new TagCollection();
     foreach ($photo->related("photo_tag") as $tag) {
         $tags->addItem(TagService::loadFromDb($tag->tag));
     }
     return $tags;
 }
Пример #10
0
 public function output(\Nette\Database\Table\ActiveRow $query)
 {
     $output = array();
     foreach ($query->related('permission')->where('role_id', $this->roleId) as $permission) {
         $output[] = $permission->privilege['name'];
     }
     return implode(', ', $output);
 }
Пример #11
0
 public function output(\Nette\Database\Table\ActiveRow $query)
 {
     $translateLocale = $query->related('translate_locale')->where('language_id', $this->languageId)->fetch();
     if ($translateLocale) {
         parent::output($translateLocale);
     } else {
         return '';
     }
 }
 /**
  * 
  * @param \Nette\Database\Table\ActiveRow $row
  * @return \dbEntities\AbstractDBEntity
  */
 public function loadFromActiveRow(ActiveRow $row)
 {
     $referenceVar = $this->getReferenceArray();
     foreach ($referenceVar as $column => &$varRef) {
         if ($row->offsetExists($column)) {
             $varRef = $row[$column];
         }
     }
     return $this;
 }
Пример #13
0
 public function process(Form $form)
 {
     $values = $form->values;
     $this->onPreSave($values);
     if ($this->row) {
         $this->row->update($values);
     } else {
         $this->row = $this->database->table($this->table)->insert($values);
     }
     $this->onPostSave($this->row);
 }
Пример #14
0
 protected function createComponentFormTranslate($name)
 {
     $form = new Form($this, $name);
     $form->addTextArea('translate', $this->translator->trans('translate.grid.translate'));
     $form->addSubmit('send', $this->translator->trans('translate.menu.translate'));
     $form->onSuccess[] = [$this, 'submitFormTranslate'];
     $translatesLocale = $this->row->related('translate_locale')->where('language_id', $this->webLanguage)->fetch();
     if ($translatesLocale) {
         $form->setDefaults(array('translate' => $translatesLocale['translate']));
     }
     return $form;
 }
Пример #15
0
 public function __construct(array $data, NSelection $table)
 {
     if (!($table instanceof Selection || $table instanceof GroupedSelection)) {
         throw new \Nette\InvalidArgumentException();
     }
     parent::__construct($data, $table);
 }
Пример #16
0
 /**
  * @param string $entityClass
  * @param string $key
  * @param null $column
  * @return StoredEntity|null
  * @throws NoReferenceException
  */
 public function reference($entityClass, $key = null, $column = null)
 {
     if ($this->cachedReferences == null) {
         $this->cachedReferences = new ArrayHash();
     }
     if ($this->isNewEntity()) {
         return null;
     }
     if ($key == null) {
         $columnToDiscoverReference = $entityClass;
         $reflectProperty = $this->getPropertyByColumnName($columnToDiscoverReference);
         if ($reflectProperty) {
             $entityClass = $this->getReferenceClassForProperty($reflectProperty);
             if (!$entityClass) {
                 throw new NoReferenceException($columnToDiscoverReference);
             }
             $column = $columnToDiscoverReference;
             $key = $entityClass::getTableName($this->annotationReader);
         }
     }
     self::mustBeChildOf($entityClass, StoredEntity::class);
     if (!$this->cachedReferences->offsetExists($key)) {
         $reference = $entityClass::create($this, $this->row->getTable()->getReferencedTable($this->row, $key, $column));
         $this->cachedReferences->offsetSet($key, $reference);
     }
     return $this->cachedReferences->offsetGet($key);
 }
Пример #17
0
 public function getField(ActiveRow $row, $name)
 {
     if (isset($this->fields[$name])) {
         $column = $this->fields[$name];
         return $row->{$column};
     }
     if (isset($this->refs[$name])) {
         list($table, $column, $type) = $this->refs[$name];
         $ref = $row->ref($table, $column);
         return $ref ? $this->manager->createEntity($type, $row) : NULL;
     }
     if (isset($this->related[$name])) {
         list($table, $column, $type) = $this->related[$name];
         return $this->manager->createCollection($type, $row->related($table, $column));
     }
     throw new Nette\InvalidArgumentException();
 }
Пример #18
0
 /** @return bool */
 public function update()
 {
     $this->checkRow();
     $status = TRUE;
     if (!$this->isPersisted()) {
         $status = $this->row->update($this->modified);
         $this->reload($this->row);
     }
     return $status;
 }
Пример #19
0
 public function startup()
 {
     parent::startup();
     $this->cronRow = $this->logCron->getCronRow();
     if (!is_object($this->cronRow) || $this->cronRow->id < 1) {
         $e = new \Nette\InvalidArgumentException("Uknown cron detected!");
         $this->logCron->logError($e);
         $this->logCron->finishTask();
     } else {
         $this->logCronRow = $this->logCron->createLogFromCronId($this->cronRow->id);
     }
     $cronRow = $this->cronRow;
     if ($this->cronRow && isset($this->cronRow->running_flag) && $this->cronRow->running_flag == 1 && $this->context->parameters['productionMode']) {
         $now = new \DateTime();
         $diff = $now->diff($this->cronRow->upd_dt);
         // if cron was started more than 20 minutes ago, run again. There is big change, there was some error in cron which prevent finishing
         if ($diff->d > 0 || $diff->h > 0 || $diff->i > 20) {
             $this->cronRow->update(array('running_flag' => 0, 'upd_process_id' => 'BasePresenter::startup()'));
         } else {
             // if cron is requested when previous request is still running, do not allow to run again
             $this->logCron->setSkippedFlag(true);
             $this->logCron->finishTask();
         }
     }
     $this->cronRow->update(array('running_flag' => 1));
     // set NewRelic background Job
     // If no argument or true as an argument is given, mark the current transaction as a background job.
     // If false is passed as an argument, mark the transaction as a web transaction.
     if (extension_loaded('newrelic')) {
         newrelic_background_job(1);
     }
     // Check for same tasks in db in interval
     $dateCheck = new \DateTime();
     $dateCheck->add(\DateInterval::createFromDateString('-30 seconds'));
     $multipleCheck = $this->logCronEntity->getTable()->where("del_flag", 0)->where("cron_id", $this->cronRow->id)->where("ins_dt >= ?", $dateCheck)->count('*');
     // Same task run already in period, terminate()!
     $multipleCheck = 0;
     if ($multipleCheck > 0) {
         $this->logCron->setSkippedFlag(true);
         $this->logCron->finishTask();
     }
 }
Пример #20
0
 protected function createComponentFormEdit($name)
 {
     $form = new Form($this, $name);
     $form->addGroup();
     $form->addText('name', $this->translator->translate('admin.form.name'))->addRule(Form::FILLED, $this->translator->translate('admin.form.isRequired'));
     $form->addText('system_name', $this->translator->translate('admin.form.systemName'))->addRule(Form::FILLED, $this->translator->translate('admin.form.name'))->addRule([$this, 'valideFormEditSystemName'], $this->translator->translate('admin.form.systemNameExist'));
     $form->addGroup($this->translator->translate('admin.resource.setPrivileges'));
     foreach ($this->privilege->order('name') as $privilege) {
         $form->addCheckbox('privilege_' . $privilege['id'], $privilege['name']);
     }
     $form->addGroup();
     $form->addSubmit('send', $this->translator->translate('admin.form.edit'));
     $form->onSuccess[] = [$this, 'submitFormEdit'];
     $defaults = array('name' => $this->resource->name, 'system_name' => $this->resource->system_name);
     foreach ($this->resource->related('resource_privilege') as $resourcePrivilege) {
         $defaults['privilege_' . $resourcePrivilege['privilege_id']] = true;
     }
     $form->setDefaults($defaults);
     return $form;
 }
Пример #21
0
 /**
  * Creates Addon entity from Nette\Database row.
  *
  * @todo   Consider lazy loading for versions and tags.
  *
  * @param \Nette\Database\Table\ActiveRow
  * @param AddonVotes
  * @return Addon
  */
 public static function fromActiveRow(ActiveRow $row, AddonVotes $addonVotes = NULL)
 {
     $addon = new static();
     $addon->id = (int) $row->id;
     $addon->name = $row->name;
     $addon->composerVendor = $row->composerVendor;
     $addon->composerName = $row->composerName;
     $addon->userId = (int) $row->user->id;
     $addon->shortDescription = $row->shortDescription;
     $addon->description = $row->description;
     $addon->descriptionFormat = $row->descriptionFormat;
     $addon->defaultLicense = $row->defaultLicense;
     $addon->repository = $row->repository;
     $addon->repositoryHosting = $row->repositoryHosting;
     $addon->demo = $row->demo;
     $addon->updatedAt = $row->updatedAt ? DateTime::from($row->updatedAt) : NULL;
     $addon->deletedAt = $row->deletedAt;
     $addon->deletedBy = $row->ref('deletedBy');
     $addon->type = $row->type;
     $addon->stars = $row->stars;
     foreach ($row->related('versions') as $versionRow) {
         $version = AddonVersion::fromActiveRow($versionRow);
         $version->addon = $addon;
         $addon->versions[$version->version] = $version;
     }
     foreach ($row->related('tags') as $tagRow) {
         $addon->tags[$tagRow->tag->id] = Tag::fromActiveRow($tagRow->tag);
     }
     foreach ($row->related('addons_resources') as $resourceRow) {
         $addon->resources[$resourceRow->type] = $resourceRow->resource;
     }
     if ($addonVotes) {
         $addon->votes = $addonVotes->calculatePopularity($row);
     }
     return $addon;
 }
Пример #22
0
 /**
  * Returns referenced row.
  * @param  ActiveRow
  * @param  string
  * @param  string|NULL
  * @return ActiveRow|NULL|FALSE NULL if the row does not exist, FALSE if the relationship does not exist
  */
 public function getReferencedTable(ActiveRow $row, $table, $column = NULL)
 {
     if (!$column) {
         $belongsTo = $this->conventions->getBelongsToReference($this->name, $table);
         if (!$belongsTo) {
             return FALSE;
         }
         list($table, $column) = $belongsTo;
     }
     if (!$row->accessColumn($column)) {
         return FALSE;
     }
     $checkPrimaryKey = $row[$column];
     $referenced =& $this->refCache['referenced'][$this->getSpecificCacheKey()]["{$table}.{$column}"];
     $selection =& $referenced['selection'];
     $cacheKeys =& $referenced['cacheKeys'];
     if ($selection === NULL || $checkPrimaryKey !== NULL && !isset($cacheKeys[$checkPrimaryKey])) {
         $this->execute();
         $cacheKeys = array();
         foreach ($this->rows as $row) {
             if ($row[$column] === NULL) {
                 continue;
             }
             $key = $row[$column];
             $cacheKeys[$key] = TRUE;
         }
         if ($cacheKeys) {
             $selection = $this->createSelectionInstance($table);
             $selection->where($selection->getPrimary(), array_keys($cacheKeys));
         } else {
             $selection = array();
         }
     }
     return isset($selection[$checkPrimaryKey]) ? $selection[$checkPrimaryKey] : NULL;
 }
Пример #23
0
 /**
  * Vrací úkoly spadající pod danný list.
  * @return Nette\Database\Table\Selection
  */
 public function tasksOf(Nette\Database\Table\ActiveRow $list)
 {
     return $list->related('task')->order('created');
 }
Пример #24
0
 private static function loadCommentsFromDb(DbEntry $article)
 {
     $comments = new CommentCollection();
     foreach ($article->related("comment", "article_id") as $comment) {
         $comments->addItem(CommentService::loadFromDb($comment));
     }
     return $comments;
 }
Пример #25
0
 /**
  * Returns referenced row.
  * @param  ActiveRow
  * @param  string
  * @param  string|NULL
  * @return ActiveRow|NULL|FALSE NULL if the row does not exist, FALSE if the relationship does not exist
  */
 public function getReferencedTable(ActiveRow $row, $table, $column = NULL)
 {
     if (!$column) {
         $belongsTo = $this->conventions->getBelongsToReference($this->name, $table);
         if (!$belongsTo) {
             return FALSE;
         }
         list($table, $column) = $belongsTo;
     }
     if (!$row->accessColumn($column)) {
         return FALSE;
     }
     $checkPrimaryKey = $row[$column];
     $referenced =& $this->refCache['referenced'][$this->getSpecificCacheKey()]["{$table}.{$column}"];
     $selection =& $referenced['selection'];
     $cacheKeys =& $referenced['cacheKeys'];
     if ($selection === NULL || $checkPrimaryKey !== NULL && !isset($cacheKeys[$checkPrimaryKey])) {
         $this->execute();
         $cacheKeys = [];
         foreach ($this->rows as $row) {
             if ($row[$column] === NULL) {
                 continue;
             }
             $key = $row[$column];
             $cacheKeys[$key] = TRUE;
         }
         if ($cacheKeys) {
             $selection = $this->createSelectionInstance($table);
             //search for foreign key column name which is referenced from activeRow parent table to table from which is selection made
             $foreign = $this->conventions->getForeign($row->getTable()->getName(), $column);
             $primary = $foreign == NULL ? $selection->getPrimary() : $foreign;
             $selection->where($primary, array_keys($cacheKeys));
         } else {
             $selection = NULL;
         }
     }
     return $selection;
     //return isset($selection[$checkPrimaryKey]) ? $selection[$checkPrimaryKey] : NULL;
 }
Пример #26
0
 /**
  * Sets column value.
  * @param  string
  * @param  string
  * @return void
  */
 public function setColumnValue($name, $value)
 {
     parent::__set($name, $value);
 }
Пример #27
0
 public function createIdentity(ActiveRow $user)
 {
     $data = $user->toArray();
     unset($user['password']);
     return new \Nette\Security\Identity($user->id, NULL, $data);
 }
Пример #28
0
 public function update(ActiveRow $entity, array $data)
 {
     $entity->update($data);
 }
Пример #29
0
 /**
  * Appends referencing database table data to result set according to $this->deepListing
  * @param array $dest reference to data destination
  * @param \Nette\Database\Table\ActiveRow $row
  * @param array $map
  */
 protected function getDeepData(array &$dest, ActiveRow $row, array $map)
 {
     foreach ($map as $key => $val) {
         if (is_array($val)) {
             if ($this->isValidId($dest[$key]) === FALSE) {
                 continue;
             }
             $dest[$key] = $row->ref($key)->toArray();
             $this->getDeepData($dest[$key], $row->ref($key), $val);
         } else {
             $oneToMany = strpos($val, '.');
             if ($oneToMany) {
                 $dest[substr($val, 0, $oneToMany)] = iterator_to_array($row->related($val), false);
             } else {
                 $dest[$val] = $row->ref($val)->toArray();
             }
         }
     }
 }
Пример #30
0
 /**
  * Retrieve an external iterator
  *
  * @return \Traversable
  */
 public function getIterator()
 {
     return $this->activeRow->getIterator();
 }