/** * Get the proxy * * @return Centurion_Db_Table_Row_Abstract */ protected function _getProxy($model, $pk) { if (null === $this->_proxy && null !== $pk) { $proxyTable = Centurion_Db::getSingletonByClassName($model); $this->_proxy = $proxyTable->find($pk)->current(); } return $this->_proxy; }
/** * Get the row referenced by the value and set it to the element before rendering it. * @param Zend_View_Interface $view * @return string */ public function render(Zend_View_Interface $view = null) { if ($this->getValue() !== null) { $relatedModel = Centurion_Db::getSingletonByClassName($this->_reference); $this->setValue($relatedModel->findOneById($this->getValue())); } return parent::render($view); }
/** * Retrieve navigation object. * * @return Zend_Navigation */ public function getNavigation() { if (null === $this->_navigation) { $options = $this->getOptions(); $this->_navigation = new Zend_Navigation(Centurion_Db::getSingletonByClassName($options['adapter'])->getCache()->toNavigation()); $key = isset($options['registry_key']) && !is_numeric($options['registry_key']) ? $options['registry_key'] : self::DEFAULT_REGISTRY_KEY; Zend_Registry::set($key, $this->_navigation); } return $this->_navigation; }
protected function _getProxy($model, $pk) { if (null !== $model) { if (null === $this->_proxy) { $proxyTable = Centurion_Db::getSingletonByClassName($model); if (null !== $pk) { $this->_proxy = $proxyTable->findOneById($pk); } } return $this->_proxy; } return false; }
public function translateAction() { $form = $this->_getForm(); $fromPk = $this->_controller->getRequest()->getParam('from'); $targetLang = $this->_controller->getRequest()->getParam('lang'); $this->_controller->getRequest()->setParam('from', null); $this->_controller->getRequest()->setParam('lang', null); $model = Centurion_Db::getSingletonByClassName(get_class($form->getModel())); Centurion_Db_Table_Abstract::setFiltersStatus(false); try { $row = $model->get(array('language_id' => $targetLang, 'original_id' => $fromPk)); Centurion_Db_Table_Abstract::restoreFiltersStatus(); $this->_form = null; $this->_controller->getRequest()->setParam('id', $row->id); self::$_filters = false; Centurion_Db_Table_Abstract::setFiltersStatus(self::$_filters); $this->getAction(); } catch (Centurion_Db_Table_Row_Exception_DoesNotExist $e) { $this->_controller->getRequest()->setParam(Centurion_Controller_CRUD::PARAM_FORM_VALUES, array('original_id' => $fromPk, 'language_id' => $targetLang)); $this->newAction(); } }
protected function getTestableModel() { global $application; $bootstrap = $application->getBootstrap(); $bootstrap->bootstrap('FrontController'); $bootstrap->bootstrap('modules'); $bootstrap->bootstrap('db'); $moduleRessource = $bootstrap->getResource('modules'); if (null == $this->_testableModel) { $this->_testableModel = array(); $front = Centurion_Controller_Front::getInstance(); $modules = $front->getControllerDirectory(); $moduleEnabled = Centurion_Config_Manager::get('resources.modules'); foreach ($modules as $moduleName => $module) { if (!in_array($moduleName, $moduleEnabled)) { continue; } $dbTableDir = realpath($module . '/../models/DbTable'); if (!file_exists($dbTableDir)) { continue; } $dir = new Centurion_Iterator_DbTableFilter($dbTableDir); foreach ($dir as $fileInfo) { $filename = $fileInfo->getFilenameWithoutExtension(); $className = sprintf('%s_Model_DbTable_%s', ucfirst($moduleName), $filename); $model = Centurion_Db::getSingletonByClassName($className); if (method_exists($model, 'getTestCondition')) { $testCondition = $model->getTestCondition(); if (null !== $testCondition) { $this->_testableModel[] = array($model, $model->getTestCondition()); } } } } } return $this->_testableModel; }
public function uploadAction() { $this->getHelper('layout')->disableLayout(); $this->getHelper('viewRenderer')->setNoRender(true); $ticket = $this->_getParam('uploadTicket'); $ticket = Centurion_Db::getSingleton('media/multiupload_ticket')->findOneByTicket($ticket); if (null === $ticket) { echo Zend_Json::encode(array('code' => false, 'message' => 'Invalid Ticket')); return; } $originalForm = new $ticket->form_class_model->name(); if (null !== $ticket->proxy_pk) { $instance = Centurion_Db::getSingletonByClassName($ticket->proxy_model->name)->find($ticket->proxy_pk); $instance = $instance->current(); $originalForm->setInstance($instance); } if ($ticket->values !== null) { $originalForm->populate(unserialize($ticket->values)); } $result = array(); $form = $originalForm->getElement($ticket->element_name)->getFile(); if ($form->isValid(array())) { $file = $form->save(); if (method_exists($originalForm, 'postMultiuploadSuccess')) { $originalForm->postMultiuploadSuccess($file); } $result['code'] = true; if ($file->isImage()) { $result['thumb'] = $file->getStaticUrl(array('resize' => array('maxWidth' => 100, 'maxHeight' => 100))); } $result['fileId'] = $file->id; } else { $result['code'] = false; $result['message'] = $form->getErrorMessages(); } echo Zend_Json::encode($result); }
/** * Adds a JOIN table and columns to the query with the referenceMap. * * @param string $key The referenceMap key * @param array|string $cols The columns to select from the joined table. * @param string $schema The database name to specify, if any. * @return Centurion_Db_Table_Select This Centurion_Db_Table_Select object. */ protected function _joinUsingVia($key, $type, $cols = '*', $schema = null) { if (empty($this->_parts[self::FROM])) { require_once 'Zend/Db/Select/Exception.php'; throw new Zend_Db_Select_Exception("You can only perform a joinUsing after specifying a FROM table"); } $referenceMap = $this->getTable()->getReferenceMap(Centurion_Inflector::tableize($key)); $refTable = Centurion_Db::getSingletonByClassName($referenceMap['refTableClass']); $name = $refTable->info('name'); $join = $this->_adapter->quoteIdentifier(key($this->_parts[self::FROM]), true); $from = $this->_adapter->quoteIdentifier($name); $primary = $refTable->info('primary'); $cond1 = $join . '.' . $referenceMap['columns']; $cond2 = $from . '.' . $primary[1]; $cond = $cond1 . ' = ' . $cond2; return $this->_join($type, $name, $cond, $cols, $schema); }
/** * Initialize DbTable for each loaded module. * * @return void */ protected function _initDbTable() { $cache = $this->_getCache('core'); if (!($references = $cache->load('references_apps'))) { $this->bootstrap('FrontController'); $this->bootstrap('db'); $this->bootstrap('modules'); $front = $this->getResource('FrontController'); $modules = $front->getControllerDirectory(); $references = array(); $moduleEnabled = Centurion_Config_Manager::get('resources.modules'); foreach ($modules as $moduleName => $module) { if (!in_array($moduleName, $moduleEnabled)) { continue; } $dbTableDir = realpath($module . '/../models/DbTable'); if (!file_exists($dbTableDir)) { continue; } $dir = new Centurion_Iterator_DbTableFilter($dbTableDir); foreach ($dir as $fileInfo) { $filename = $fileInfo->getFilenameWithoutExtension(); $className = sprintf('%s_Model_DbTable_%s', ucfirst($moduleName), $filename); $model = Centurion_Db::getSingletonByClassName($className); $meta = $model->getMeta(); $metaData = $model->info('metadata'); foreach ($model->getReferenceMap() as $key => $referenceMap) { $refTableClass = $referenceMap['refTableClass']; $referencedModel = Centurion_Db::getSingletonByClassName($refTableClass); $plural = true; if (isset($metaData[$referenceMap['columns']]['UNIQUE']) && $metaData[$referenceMap['columns']]['UNIQUE'] == true) { $plural = false; } if ($plural) { $dependentTableKey = $meta['verbosePlural']; } else { $dependentTableKey = $meta['verboseName']; } if (array_key_exists($dependentTableKey, $referencedModel->getDependentTables())) { continue; } if (!array_key_exists($refTableClass, $references)) { $references[$refTableClass] = array(); } $references[$refTableClass][$dependentTableKey] = $className; } } } $cache->save($references, 'references_apps'); } Centurion_Db::setReferences($references); }
public function setFilters($filters) { $this->cleanForm(); foreach ($filters as $key => &$filterData) { if (!is_array($filterData)) { $filterData = array('label' => $filterData); if ($this->_table !== null) { $reference = $this->_table->info(Centurion_Db_Table_Abstract::REFERENCE_MAP); if (isset($reference[$key])) { $refTable = Centurion_Db::getSingletonByClassName($reference[$key]['refTableClass']); $rowset = $refTable->all(); $data = array(); foreach ($rowset as $row) { $data[] = (string) $row; } $filterData['type'] = Centurion_Controller_CRUD::FILTER_TYPE_CHECKBOX; $filterData['data'] = $data; } } } if (isset($filterData['label'])) { $label = $filterData['label']; } else { $label = ''; } $checkboxType = 'multiCheckbox'; $element = null; if (!isset($filterData['type'])) { $filterData['type'] = Centurion_Controller_CRUD::FILTER_TYPE_TEXT; } if (!isset($filterData['column'])) { $filterData['column'] = $key; } if (!isset($filterData['behavior'])) { $filterData['behavior'] = Centurion_Controller_CRUD::FILTER_BEHAVIOR_CONTAINS; } switch ($filterData['type']) { case Centurion_Controller_CRUD::FILTER_TYPE_RADIO: $checkboxType = 'radio'; case Centurion_Controller_CRUD::FILTER_TYPE_SELECT: if ($checkboxType === 'multiCheckbox') { $checkboxType = 'select'; } case Centurion_Controller_CRUD::FILTER_TYPE_CHECKBOX: $element = $this->createElement($checkboxType, $key, array('label' => $label)); if (!isset($filterData['data'])) { $manyDependentTables = $this->_table->info('manyDependentTables'); if (isset($manyDependentTables[$key])) { $refRowSet = Centurion_Db::getSingletonByClassName($manyDependentTables[$key]['refTableClass'])->fetchAll(); $filterData['data'] = array(); foreach ($refRowSet as $refRow) { $filterData['data'][$refRow->id] = $refRow->__toString(); } asort($filterData['data']); } } $element->addMultiOptions($filterData['data']); $element->setSeparator(''); if ($checkboxType === 'multiCheckbox') { $element->setIsArray(true); } break; case Centurion_Controller_CRUD::FILTER_TYPE_TEXT: case Centurion_Controller_CRUD::FILTER_TYPE_NUMERIC: $element = $this->createElement('text', $key, array('label' => $label)); break; case Centurion_Controller_CRUD::FILTER_TYPE_DATE: $element = $this->createElement('text', $key, array('label' => $label, 'class' => 'datepicker')); break; case Centurion_Controller_CRUD::FILTER_TYPE_BETWEEN_DATE: case Centurion_Controller_CRUD::FILTER_TYPE_BETWEEN_DATETIME: $form = new self(); if ($filterData['type'] == Centurion_Controller_CRUD::FILTER_TYPE_BETWEEN_DATETIME) { $class = 'field-datetimepicker'; } else { $class = 'datepicker'; } $element = $form->createElement('text', 'gt', array('class' => $class, 'belongsTo' => $key, 'label' => $this->_translate('From'), 'value' => '26/08/11 03:00')); $form->addElement($element, 'gt'); $element = $form->createElement('text', 'lt', array('class' => $class, 'belongsTo' => $key, 'label' => $this->_translate('To'), 'value' => '26/08/11 03:00')); $form->addElement($element, 'lt'); $element = null; $form->setDescription($label); $this->addSubForm($form, $key); $this->getSubForm($key)->setDecorators($this->defaultDisplayGroupDecorators); $form->getElement('lt')->setDecorators($this->defaultElementDecoratorsInDisplayGroup); $form->getElement('gt')->setDecorators($this->defaultElementDecoratorsInDisplayGroup); // $this->addDisplayGroup(array('lt', 'gt'), $key, array('description' => $label)); break; } if (null !== $element) { $this->addElement($element, $key); } } $this->_filters = $filters; }
public function getProxy() { if (null === $this->_proxy && null !== $this->proxy_pk) { $proxyTable = Centurion_Db::getSingletonByClassName($this->model->name); $tableName = $proxyTable->info(Centurion_Db_Table_Abstract::NAME); $this->_proxy = $proxyTable->select(true)->where($tableName . '.id=?', $this->proxy_pk)->fetchRow(); //Reverse binding if (null !== $this->_proxy && method_exists($this->_proxy, 'setNavigation')) { $this->_proxy->setNavigation($this); } } return $this->_proxy; }
/** * @return void * @todo documentation */ public function __wakeup() { $this->_table = Centurion_Db::getSingletonByClassName($this->_tableClass); $this->_connected = true; }
/** * Retrieve the profile attached to the user instance. * * @return Centurion_Db_Table_Row_Abstract */ public function getProfile() { if (null === $this->_profile) { $options = Centurion_Config_Manager::get('centurion'); if (!isset($options['auth_profile'])) { throw new Centurion_Exception('No site profile module available: you have to set a centurion.auth_profile in your application.ini'); } if (!class_exists($options['auth_profile'])) { throw new Centurion_Exception('The class in centurion.auth_profile option does not exists.'); } $this->_profile = Centurion_Db::getSingletonByClassName($options['auth_profile'])->findOneByUserId($this->pk); } return $this->_profile; }
/** * Override the getter, if we get and related object of a related object. * * @param string $tableName * @return void */ protected function _getTableFromString($tableName) { return Centurion_Db::getSingletonByClassName($tableName); }
/** * Parse columns to fields. * * @param array $options * @todo Add CSRF key, for security. * @return array Elements. */ protected function _columnToElements() { $info = $this->getModel()->info(); $metadata = $info['metadata']; $elements = array(); foreach ($metadata as $columnName => $columnDetails) { if ($this->isExcluded($columnName)) { continue; } if (!isset($this->_elementLabels[$columnName])) { continue; } if ($columnDetails['IDENTITY']) { $config = array('hidden', array()); } elseif (substr($columnName, 0, 2) == 'is' || substr($columnName, 0, 6) == 'can_be' || substr($columnName, 0, 3) == 'has') { $config = array('onOff', array()); } elseif (substr($columnName, 0, -3) == 'pwd' || $columnName == 'password') { $config = array('password', array()); } elseif (preg_match('/^enum/i', $columnDetails['DATA_TYPE'])) { preg_match_all('/\'(.*?)\'/', $columnDetails['DATA_TYPE'], $matches); $options = true === $columnDetails['NULLABLE'] ? array(null => '') : array(); foreach ($matches[1] as $match) { $options[$match] = $match; } $config = array('select', array('multioptions' => $options)); } elseif (false !== ($reference = $this->getModel()->getReferenceByColumnName($columnName))) { if ($this->isDisabled($columnName)) { $config = array('Reference', array('reference' => $reference)); } else { $relatedTable = Centurion_Db::getSingletonByClassName($reference); $options = $this->_buildOptions($relatedTable, $columnName, true === $columnDetails['NULLABLE']); $config = array('select', array('multioptions' => $options)); } } else { $datatype = $columnDetails['DATA_TYPE']; $config = array($this->_columnTypes[$datatype], array()); if (array_key_exists($datatype, $this->_columnValidators)) { $config[1]['validators'] = array(array('validator' => $this->_columnValidators[$datatype])); if ($this->_columnValidators[$datatype] == 'StringLength') { $config[1]['validators'][0]['options'] = array(0, $columnDetails['LENGTH']); } } if (array_key_exists($datatype, $this->_columnFilters)) { $config[1]['filters'] = $this->_columnFilters[$datatype]; } } $config[1] = array_merge($config[1], array('label' => $this->_getElementLabel($columnName), 'required' => isset($columnDetails['NULLABLE']) ? (bool) $columnDetails['NULLABLE'] != true : false)); if ($this->isDisabled($columnName)) { $config[1] = array_merge($config[1], array('disabled' => 'disabled')); } $elements[$columnName] = $config; } $manyDependentTables = $this->getModel()->info(Centurion_Db_Table_Abstract::MANY_DEPENDENT_TABLES); foreach ($manyDependentTables as $key => $manyDependentTable) { if ($this->isExcluded($key)) { continue; } $options = array('label' => $this->_getElementLabel($key)); //TODO: remove this Media_Model_DbTable_File reference from core if ($manyDependentTable['refTableClass'] !== 'Media_Model_DbTable_File') { $table = Centurion_Db::getSingletonByClassName($manyDependentTable['refTableClass']); $intersectionTable = Centurion_Db::getSingletonByClassName($manyDependentTable['intersectionTable']); $options['multioptions'] = $this->_buildOptions($table, $key, false); if ($intersectionTable->hasColumn('order')) { $options['class'] = 'sortable'; } $elementName = 'multiselect'; } else { $elementName = new Media_Form_Element_MultiFile($key, array('parentForm' => $this)); } $elements[$key] = array($elementName, $options); } return $elements; }
public function update(array $data, $where) { $currentFileRow = $this->fetchRow($where); if (null !== $currentFileRow->proxy_model) { $oldProxyTableClass = $currentFileRow->proxy_model; } foreach ($currentFileRow->duplicates as $duplicate) { $duplicate->delete(); } foreach ($this->_dependentProxies as $key => $dependentProxy) { $proxyTable = Centurion_Db::getSingletonByClassName($dependentProxy); $mimes = array_keys($proxyTable->getMimeTypes()); if (!in_array($data['mime'], $mimes)) { continue; } if (in_array($data['mime'], $mimes)) { $newProxyTableClass = $dependentProxy; break; } } if (!isset($data['sha1'])) { $data['sha1'] = sha1_file(Centurion_Config_Manager::get('media.uploads_dir') . DIRECTORY_SEPARATOR . $data['local_filename']); } if (isset($oldProxyTableClass) && $oldProxyTableClass != $newProxyTableClass) { $currentProxyRow = Centurion_Db::getRow($oldProxyTableClass, $currentFileRow->proxy_pk); if (null !== $currentProxyRow) { $data = array_merge($data, array('proxy_model' => null, 'proxy_pk' => null)); $currentProxyRow->delete(); } } if (null !== $newProxyTableClass) { $newProxyTable = Centurion_Db::getSingletonByClassName($newProxyTableClass); if (isset($oldProxyTableClass) && $oldProxyTableClass == $newProxyTableClass) { $pk = $newProxyTable->update($data, $newProxyTable->getAdapter()->quoteInto('id = ?', $currentFileRow->proxy_pk)); } else { $pk = $newProxyTable->insert($data); $data['proxy_model'] = $newProxyTableClass; } $data['proxy_pk'] = $pk; } if (array_key_exists(self::BELONG_TO, $data)) { list($model, $pk) = $this->_setupProxyBelong($data[self::BELONG_TO]); $data = array_merge($data, array('belong_model' => $model, 'belong_pk' => $pk)); unset($data[self::BELONG_TO]); } return parent::update($data, $where); }
/** * Get a row with the class name and the primary key value. * * @param string $class Class name * @param int $pk Primary key value * @return false|Centurion_Db_Table_Row_Abstract */ public static function getRow($class, $pk) { if (class_exists($class)) { $modelTable = Centurion_Db::getSingletonByClassName($class); if (null !== $modelTable) { return $modelTable->find($pk)->current(); } } return false; }
public function preDispatch() { parent::preDispatch(); if ($this->_hasParam('model')) { $this->_model = Centurion_Db::getSingletonByClassName($this->_getParam('model')); } $this->view->controller = $this->_request->getControllerName(); $this->view->module = $this->_request->getModuleName(); $id = $this->_getParam('id', 0); $form = $this->_getForm(); if ($id > 0) { $action = $this->_helper->url->url(array_merge($this->_extraParam, array('controller' => $this->_request->getControllerName(), 'id' => $id, 'action' => 'put', 'module' => $this->_request->getModuleName())), null, true); $object = $this->_getModel()->find($id)->current(); if (null === $object) { throw new Zend_Controller_Dispatcher_Exception(sprintf('Invalid action specified (%s)', $id)); } if (!$form->hasInstance()) { $form->setInstance($object); } $form->addElement('hidden', '_method', array('value' => 'put')); } else { $action = $this->_helper->url->url(array_merge($this->_extraParam, array('controller' => $this->_request->getControllerName(), 'module' => $this->_request->getModuleName()))); $form->removeElement('id'); } $form->cleanForm(); $form->setAction($action); }
public function addProxyAction() { //TODO: check Content type and id with config $proxyPk = $this->_getParam('proxy_pk'); $proxyModel = $this->_getParam('proxy_model'); list($contentType, ) = Centurion_db::getSingleton('core/content_type')->getOrCreate(array('name' => $proxyModel)); $instance = Centurion_Db::getSingletonByClassName($this->_getParam('proxy_model'))->findOneById($proxyPk); if (!$instance instanceof Core_Model_DbTable_Row_Navigable_Interface) { throw new Centurion_Controller_Action_Exception('Not an navigable object'); } $navigation = $this->_model->createRow(); list($navigation, $created) = $this->_model->getOrCreate(array('proxy_model' => $contentType->id, 'proxy_pk' => $proxyPk)); if ($proxyPk) { $navigation->is_visible = $instance->isPublished(); $navigation->save(); if (isset($this->_rootNode)) { $navigation->moveTo($this->_rootNode, Core_Traits_Mptt_Model_DbTable::POSITION_LAST_CHILD); } } $this->_redirect($this->view->url(array('controller' => $this->getRequest()->getControllerName(), 'module' => $this->getRequest()->getModuleName(), 'action' => 'index', 'proxy_pk' => null, 'proxy_model' => null), 'default')); die; }