protected function _beforeInsert(KDatabaseContextInterface $context) { $identifier = $context->getSubject()->getIdentifier()->toArray(); $identifier['path'] = array('model'); $count = (int) $this->getObject($identifier)->default(1)->count(); if ($count === 0) { $context->data->default = 1; } }
protected function _beforeSave(KDatabaseContextInterface $context) { $entity = $context->getSubject(); if (!$entity->isNew() && !$entity->overwrite) { $translator = $this->getObject('translator'); $entity->setStatusMessage($translator->translate('Resource already exists')); return false; } return true; }
/** * Sets the folder name as created by the OS (encoding) in the filesystem * * @param KDatabaseContextInterface $context */ protected function _afterSave(KDatabaseContextInterface $context) { if ($context->siblings && count($context->siblings)) { $siblings = KObjectConfig::unbox($context->siblings); $name = array_diff(scandir(dirname($context->getSubject()->fullpath)), array_pop($siblings)); if (count($name) == 1) { $this->name = current($name); } $context->siblings = $siblings; } }
protected function _beforeSelect(KDatabaseContextInterface $context) { $query = $context->query; if ($query) { $params = $context->query->params; $id_column = $context->getSubject()->getIdentityColumn(); // To display the custom ordering in backend if (!$query->isCountQuery()) { $query->columns(array('ordering' => 'ordering2.custom'))->join(array('ordering2' => 'docman_category_orderings'), 'tbl.' . $id_column . ' = ordering2.' . $id_column, 'left'); } // Force the sort if we are not fetching immediate children of a category if ($params && !($params->level == 1 && $params->parent_id && $params->sort !== 'custom')) { if (in_array($params->sort, array('title', 'created_on', 'custom'))) { $query->order = array(); $column = sprintf('GROUP_CONCAT(LPAD(`ordering`.`%s`, 5, \'0\') ORDER BY crumbs.level DESC SEPARATOR \'/\')', $params->sort); $query->join(array('ordering' => 'docman_category_orderings'), 'crumbs.ancestor_id = ordering.' . $id_column, 'inner')->columns(array('order_path' => $column))->order('order_path', 'ASC'); } } } }
protected function _beforeSelect(KDatabaseContextInterface $context) { $query = $context->query; $params = $context->query->params; if (!$query) { return true; } $is_count = false; if ($query->isCountQuery() && $context->mode === KDatabase::FETCH_FIELD) { $is_count = true; $query->columns = array(); } $id_column = $context->getSubject()->getIdentityColumn(); $closure_table = $this->getRelationTable(); // We are going to force ordering ourselves here $query->order = array(); $sort = 'path'; $direction = 'ASC'; $query->columns(array('level' => 'COUNT(crumbs.ancestor_id)'))->columns(array('path' => 'GROUP_CONCAT(crumbs.ancestor_id ORDER BY crumbs.level DESC SEPARATOR \'/\')'))->join(array('crumbs' => $closure_table), 'crumbs.descendant_id = tbl.' . $id_column, 'INNER')->group('tbl.' . $id_column); if ($max_level = (int) $params->get('max_level')) { $params->set('level', range(1, $max_level)); } if ($params->has('parent_id')) { $query->join(array('closures' => $closure_table), 'closures.descendant_id = tbl.' . $id_column, 'inner')->where('closures.ancestor_id IN :parent_id')->bind(array('parent_id' => (array) $params->get('parent_id'))); if (!$params->has('include_self')) { $query->where('tbl.' . $id_column . ' NOT IN :parent_id'); } if ($params->has('level')) { $query->where('closures.level IN :level')->bind(array('level' => (array) $params->get('level'))); } // If we are fetching the immediate children of a category we can sort however we want if ($params->level == 1 && $params->sort !== 'custom') { $sort = 'tbl.' . $params->sort; $direction = $params->direction; } } elseif ($params->has('level')) { $query->having('level IN :level')->bind(array('level' => (array) $params->get('level'))); } $query->order($sort, $direction); if ($is_count) { $data = $context->getSubject()->getAdapter()->select($context->query, KDatabase::FETCH_FIELD_LIST); $context->data = count($data); return false; } return true; }