protected function _beforeTableSelect(Library\CommandContext $context) { if ($context->query instanceof Library\DatabaseQuerySelect && $context->mode == Library\Database::FETCH_ROWSET) { $this->_table = $context->getSubject(); $this->_table->getAdapter()->getCommandChain()->enqueue($this, $this->getPriority()); } }
protected function _databaseBeforeSave(Library\CommandContext $context) { $row = $context->getSubject(); if (is_string($row->file) && !is_uploaded_file($row->file)) { // remote file try { $file = $this->getObject('com:files.database.row.url'); $file->setData(array('file' => $row->file)); $file->load(); $row->contents = $file->contents; } catch (DatabaseRowUrlException $e) { throw new \RuntimeException($e->getMessage(), $e->getCode()); } if (empty($row->name)) { $uri = $this->getObject('lib:http.url', array('url' => $row->file)); $path = $uri->toString(Library\HttpUrl::PATH | Library\HttpUrl::FORMAT); if (strpos($path, '/') !== false) { $path = basename($path); } $row->name = $path; } } $result = parent::_databaseBeforeSave($context); if ($result) { $filter = $this->getObject('com:files.filter.file.uploadable'); $result = $filter->validate($context->getSubject()); if ($result === false) { $errors = $filter->getErrors(); if (count($errors)) { $context->getSubject()->setStatusMessage(array_shift($errors)); } } } return $result; }
protected function _beforeControllerBrowse(Library\CommandContext $context) { $state = $context->getSubject()->getModel()->getState(); //If we are filtering for all the trashed entities, decorate the actionbar with the revisable toolbar if ($state->trashed == true && $this->hasToolbar('actionbar')) { $this->getToolbar('actionbar')->decorate('com:revisions.controller.toolbar.revisable'); } }
protected function _reorder(Library\CommandContext $context) { $table = $context->getSubject(); $table->getAdapter()->execute('SET @index = 0'); $query = $this->getObject('lib:database.query.update')->table($table->getBase())->values('ordering = (@index := @index + 1)')->order('ordering', 'ASC'); $this->_buildQuery($query); $table->getAdapter()->update($query); }
protected function _databaseAfterSave(Library\CommandContext $context) { $row = $context->getSubject(); $max = $row->getContainer()->parameters->maximum_image_size; if ($row->isImage() && $max) { try { $imagine = new \Imagine\Gd\Imagine(); $image = $imagine->open($row->fullpath); $size = $image->getSize(); $larger = max($size->getWidth(), $size->getHeight()); if ($larger > $max) { $image->resize($size->scale($max / $larger)); $image->save($row->fullpath); } } catch (\Exception $e) { } } }
protected function _beforeTableSelect(Library\CommandContext $context) { if ($query = $context->query) { // Calculate ordering_path only if querying a list and it's sorted by an ordering column. $state = $context->options->state; if (!$query->isCountQuery() && $state && !$state->isUnique()) { if (in_array($state->sort, $this->_columns)) { $table = $context->getSubject(); $query->columns(array('ordering_path' => 'GROUP_CONCAT(ordering_crumbs.' . $state->sort . ' ORDER BY crumbs.level DESC SEPARATOR \'/\')'))->join(array('ordering_crumbs' => $table->getOrderingTable()->getName()), 'crumbs.ancestor_id = ordering_crumbs.' . $table->getIdentityColumn(), 'INNER'); // Replace sort column with ordering path. foreach ($query->order as &$order) { if ($order['column'] == $state->sort) { $order['column'] = 'ordering_path'; break; } } } $query->columns(array('ordering' => 'CAST(SUBSTRING_INDEX(GROUP_CONCAT(ordering_crumbs.custom ORDER BY crumbs.level DESC SEPARATOR \'/\'), \'/\', -1) AS UNSIGNED)')); } } }
public function execute($name, Library\CommandContext $context) { if (in_array($name, $this->_actions)) { $entity = $context->result; if ($entity instanceof Library\DatabaseRowInterface || $entity instanceof Library\DatabaseRowsetInterface) { $rowset = array(); if ($entity instanceof Library\DatabaseRowInterface) { $rowset[] = $entity; } else { $rowset = $entity; } foreach ($rowset as $row) { //Only log if the row status is valid. $status = $row->getStatus(); if (!empty($status)) { $identifier = $context->getSubject()->getIdentifier(); $log = array('action' => $context->action, 'package' => $identifier->package, 'name' => $identifier->name, 'status' => $status, 'created_by' => $context->user->getId()); if (is_array($this->_title_column)) { foreach ($this->_title_column as $title) { if ($row->{$title}) { $log['title'] = $row->{$title}; break; } } } elseif ($row->{$this->_title_column}) { $log['title'] = $row->{$this->_title_column}; } if (!isset($log['title'])) { $log['title'] = '#' . $row->id; } $log['row'] = $row->id; $log['ip'] = $context->request->getAddress(); $this->getObject('com:activities.database.row.activity', array('data' => $log))->save(); } } } } }
protected function _databaseBeforeCopy(Library\CommandContext $context) { $row = $context->getSubject(); if (!array_intersect(array('destination_folder', 'destination_name'), $row->getModified())) { $row->setStatusMessage(\JText::_('Please supply a destination.')); return false; } if ($row->fullpath === $row->destination_fullpath) { $row->setStatusMessage(JText::_('Source and destination are the same.')); return false; } $dest_adapter = $row->getContainer()->getAdapter($row->getIdentifier()->name, array('path' => $row->destination_fullpath)); $exists = $dest_adapter->exists(); if ($exists) { if (!$row->overwrite) { $row->setStatusMessage(\JText::_('Destination resource already exists.')); return false; } else { $row->overwritten = true; } } return true; }
protected function _afterAdapterDelete(Library\CommandContext $context) { if ($context->affected) { $languages = $this->getObject('application.languages'); $primary = $languages->getPrimary(); $active = $languages->getActive(); // Remove item from other tables too. if ($active->iso_code == $primary->iso_code) { $table = $context->query->table; } else { $table = substr($context->query->table['0'], 6); } $table = $this->_tables->find(array('name' => $table))->top(); if ($table instanceof Library\DatabaseRowInterface && $table->enabled) { foreach ($languages as $language) { if ($language->iso_code != $active->iso_code) { $query = clone $context->query; $prefix = $language->iso_code != $primary->iso_code ? strtolower($language->iso_code . '_') : ''; $query->table($prefix . $table->name); $context->getSubject()->execute($query); } } // Mark item as deleted in translations table. $this->getObject('com:languages.database.table.translations')->select(array('table' => $table->name, 'row' => $query->data[$table->unique_column]))->setData(array('deleted' => 1))->save(); } } }
/** * Before table delete * * Add a new revision if the row exists and it hasn't been revised yet. Delete the revisions for the row, if the * row was previously deleted. * * @param Library\CommandContext $context * @return void */ protected function _beforeTableDelete(Library\CommandContext $context) { if (!$context->getSubject()->count($context->data->id)) { $this->setMixer($context->data); if ($this->_countRevisions(Library\Database::STATUS_DELETED) == 1) { //Delete the revision $context->affected = $this->_deleteRevisions(); //Set the status $context->data->setStatus(Library\Database::STATUS_DELETED); //Prevent the item from being deleted return false; } } else { if ($this->_countRevisions() == 0) { $this->_insertRevision(); } } }
/** * Delete the row and its children * * @param Library\CommandContext $context A command context object. * @return boolean True on success, false on failure. */ protected function _beforeTableDelete(Library\CommandContext $context) { $table = $context->getSubject(); $id_column = $table->getIdentityColumn(); $select = $this->getObject('lib:database.query.select')->columns('descendant_id')->table($table->getClosureTable()->getName())->where('ancestor_id = :id')->where('descendant_id <> :id')->bind(array('id' => $context->data->id)); $query = $this->getObject('lib:database.query.delete')->table($table->getBase())->where($id_column . ' IN :id')->bind(array('id' => $select)); $table->getAdapter()->delete($query); return true; }
/** * Modify the select query * * If the $this->_parent_column is set, this will modify the query to add the column needed by the behavior */ protected function _beforeTableSelect(Library\CommandContext $context) { $this->_table = $context->getSubject(); if ($parent_column = $this->_parent_column) { $query = $context->query; if (!is_null($query) && !$query->isCountQuery()) { $table = $context->getSubject(); $parent_column = $table->mapColumns($parent_column); $subquery = $this->getObject('lib:database.query.select')->columns(array($parent_column, 'order_total' => 'COUNT(ordering)'))->table($table->getBase())->group($parent_column); $query->columns('orderable.order_total')->join(array('orderable' => $subquery), 'orderable.' . $parent_column . ' = tbl.' . $parent_column); } } }