/** * Execute the action and returns a result * * @return array */ public function execute($params = null) { if ((!isset($params[$this->queryfield]) || empty($params[$this->queryfield])) && (!isset($params[$this->queryidfield]) || empty($params[$this->queryidfield]))) { return false; } if (isset($params['_offset'])) { $this->setParameter('offset', (int) $params['_offset']); } $extra = isset($params['extra']) ? (array) $params['extra'] : array(); if (Core::getEnvData('cache_datasets') === true) { $hash = serialize($extra); $hash .= isset($params[$this->queryfield]) ? $params[$this->queryfield] : $params[$this->queryidfield]; $md5 = md5($hash); //@todo check unicity, especially with hard-coded conditions having() $ckey = 'ds_ac_' . $this->_cachePrefix . '_' . $md5 . '_' . $this->getParameter('offset') . '_' . $this->getParameter('batch'); if (($data = Core::cacheGet($ckey)) === false) { if (isset($params[$this->queryfield])) { $data = $this->_getSuggestions(trim($params[$this->queryfield]), $extra); } else { $data = $this->_getFromIdentifier(trim($params[$this->queryidfield])); } $data['cache-key'] = $ckey; Core::cacheSet($data, $ckey, true, array('tags' => array('view', 'ac', str_replace('\\', '_', $this->_obj->getClass())))); } } else { if (isset($params[$this->queryfield])) { $data = $this->_getSuggestions(trim($params[$this->queryfield]), $extra); } else { $data = $this->_getFromIdentifier(trim($params[$this->queryidfield])); } } return $data; }
public function viewAction() { if (($cached = Core::cacheGet($this->_getParam('id'))) !== false) { echo '<pre>' . print_r($cached, true) . '<pre>'; } else { throw new \Exception("Unable to find a cached object with this id: " . $this->_getParam('id')); } exit; }
/** * Load a file into a Configuration Array * * @param string $file * @param integer $realm * @param array $params * * @return array|false Configuration Array or false if the file don't exists */ public static function loadConfig($file, $realm = Config::REALM_CONFIGS, array $params = null) { // try to get cached version if configs caching is enabled if (Core::getEnvData('cache_configs') === true) { $ckey = self::getCacheKey($file, $realm); if (($cached = Core::cacheGet($ckey)) !== false) { Core::log(sprintf('[Config] Retrieved %s config file from cache', $file)); return $cached; } } if (($filePath = self::findFile($file, $realm)) == null) { /* no matching file name in paths */ Core::log(sprintf('[Config] Failed loading %s config file', $file), \Zend_Log::ERR); return false; } $type = substr($file, strrpos($file, '.') + 1); /* use existing adapter instance or create it */ if (!isset(self::$_adapters[$type])) { $className = sprintf('\\t41\\Config\\Adapter\\%sAdapter', ucfirst(strtolower($type))); try { self::$_adapters[$type] = new $className($filePath, $params); if (!self::$_adapters[$type] instanceof Adapter\AbstractAdapter) { throw new Exception("{$className} is not implementing AbstractAdapter."); } } catch (\Exception $e) { throw new Exception($e->getMessage()); } } else { self::$_adapters[$type]->setPath($filePath); } $config = self::$_adapters[$type]->load(); Core::log(sprintf('[Config] Successfully loaded %s config file', $file)); /* if ckey is set, cache is activated but empty */ if (isset($ckey)) { Core::cacheSet($config, $ckey, true, array('tags' => array('config'))); } return $config; }
/** * Detect all modules directories and try to get config for them * @param string $path */ public static function init($path) { if (Core::getEnvData('cache_configs') !== false) { $ckey = 'configs_acl'; if (($cached = Core::cacheGet($ckey)) !== false) { self::$_config = $cached; return; } } // load application acl configuration file $config = Config\Loader::loadConfig('acl.xml', Config::REALM_CONFIGS); $resources = array(); // add all fragments coming from modules foreach (Core\Module::getConfig() as $vendorId => $vendorModules) { foreach ($vendorModules as $key => $module) { // module menus if (isset($module['controller']) && isset($module['controller']['items'])) { // walk recursively through all module's items (menu elements) $resources += self::_getAcl($module['controller']['base'], $module['controller']['items']); } // and optional menus extensions if (isset($module['controllers_extends'])) { foreach ($module['controllers_extends'] as $controller => $data) { $resources += self::_getAcl($module['controller']['base'], $data['items']); } } } } if (!isset($config['acl']['resources'])) { $config['acl']['resources'] = array(); } $config['acl']['resources'] += $resources; self::$_config = $config['acl']; if (isset($ckey)) { Core::cacheSet($config['acl'], $ckey, true, array('tags' => array('config', 'acl'))); } }
public function updateAction() { try { // populate status message if provided if (isset($this->_post['_status'])) { $this->_obj->status = new Status($this->_post['_status']); } // test object uri, if empty, object is new or faulty // @todo mix this with ObjectModel::populate() // walk through POST data foreach ($this->_post as $key => $val) { if (($property = $this->_obj->getProperty($key)) !== false) { if ($property instanceof Property\ObjectProperty) { if ($val) { $class = $property->getParameter('instanceof'); if (substr($val, 0, 4) == 'obj_') { // get object from cache $property->setValue(Core::cacheGet($val)); } else { $property->setValue(new $class($val)); } } else { $property->resetValue(); } } else { if ($property instanceof Property\CollectionProperty) { $class = $property->getParameter('instanceof'); $keyprop = $property->getParameter('keyprop'); // val for a collection should come as an array of new/existing members foreach ($val as $memberKey => $memberArray) { if (!is_numeric($memberKey)) { $this->_status = "NOK"; $this->_context['message'] = 'member-id is not a number'; return false; } // action exists to update or remove member if (isset($memberArray['action'])) { // get target member $object = $property->getValue()->getMember($memberKey, ObjectModel::MODEL); switch ($memberArray['action']) { case 'delete': if ($property->setValue($object, Collection::MEMBER_REMOVE) !== true) { $this->_status = "NOK"; $this->_context['message'] = 'error removing member from collection'; return false; } break; case 'update': foreach ($memberArray['props'] as $mApropN => $mApropV) { if (($mAprop = $object->getProperty($mApropN)) !== false) { $mAprop->setValue($mApropV); } } // direct update of the member $object->save(); break; } } else { // no action, default is new member to add $member = new $class(); // set keyprop property value $member->getProperty($member->getProperty($keyprop)->setValue($this->_obj)); // walk through foreach ($memberArray as $memberPropKey => $memberPropVal) { $mprop = $member->getProperty($memberPropKey); if ($mprop instanceof Property\ObjectProperty) { $memberPropVal = Core\Registry::get($memberPropVal); if ($memberPropVal instanceof Property\AbstractProperty) { $mprop->setValue($memberPropVal->getValue()); } else { $mprop->setValue($memberPropVal); } } else { $mprop->setValue($memberPropVal); } } // check if member was added successfully, break otherwise if ($property->setValue($member) === false) { $this->_status = "NOK"; $this->_context['message'] = 'error adding member to collection'; break; } } } } else { $property->setValue($val); } } } } // if record has no uri yet and an identifier value is present, inject it so backend will use it as primary key if (!$this->_obj->getUri() && isset($this->_post[ObjectUri::IDENTIFIER])) { $this->_obj->setUri($this->_post[ObjectUri::IDENTIFIER]); } $result = $this->_obj->save(); if ($result === false) { $this->_context['message'] = Backend::getLastQuery(); $this->_status = 'NOK'; } else { $collections = isset($this->_post['_collections']) ? $this->_post['_collections'] : 1; $extprops = isset($this->_post['_extprops']) ? $this->_post['_extprops'] : []; $this->_data['object'] = $this->_obj->reduce(array('params' => array(), 'extprops' => $extprops, 'collections' => $collections)); $this->_executeActions('ok'); $this->_refreshCache = true; } } catch (\Exception $e) { $this->_context['err'] = $e->getMessage(); if (Core::$env == Core::ENV_DEV) { $this->_context['trace'] = $e->getTraceAsString(); } $this->_status = 'ERR'; } }
public static function loadStore() { if (is_null(self::$_store)) { self::$_store = Core::cacheGet(self::$storeId); } }
/** * Populates a data object from a key/value array * * @param array $data * @param \t41\Backend\Mapper $mapper * @return \t41\ObjectModel\DataObject */ public function populate(array $data, Backend\Mapper $mapper = null) { if ($mapper) { // @todo fix compatibility with metakeys in array $data = $mapper->toDataObject($data, $this->_class); } // then sent to data object properties foreach ($data as $key => $value) { // don't use empty() to check $value to avoid zero being ignored if (($property = $this->getProperty($key)) !== false && !is_null($value)) { if ($value == Property::EMPTY_VALUE) { $property->resetValue(); continue; } if ($property instanceof ObjectProperty || $property instanceof MediaProperty) { if ($property->getParameter('instanceof') == null) { throw new DataObject\Exception("Parameter 'instanceof' for '{$key}' in class should contain a class name"); } // Specific case of MediaObject() if ($property->getParameter('instanceof') == 't41\\ObjectModel\\MediaObject') { // new file case : value is a string prepended by 'tmp:' if ($value && substr($value, 0, strlen(MediaObject::TMP_PREFIX)) == MediaObject::TMP_PREFIX) { $parts = explode('|', substr($value, 4)); // 0 => hash, 1 => original file name $file = '/tmp/' . $parts[0]; if (file_exists($file)) { $media = new MediaObject(); $media->setUri(md5(rand() * microtime())); $media->setLabel($parts[1]); $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $file); $media->setMedia($file); // blob property $media->setSize(filesize($file)); $media->setMime($mime); $media->save(); $property->setValue($media); // media property unlink($file); } continue; // don't go further in this case } } if ($value && $value != Property::EMPTY_VALUE) { if (is_object($value) && get_class($value) == $property->getParameter('instanceof')) { $property->setValue($value); } else { if (substr($value, 0, 4) == 'obj_') { // get object from cache $property->setValue(Core::cacheGet($value)); } else { if (substr($value, 0, 1) == Backend::PREFIX) { /* get & call object's backend to get a full configured object uri */ $backend = ObjectModel::getObjectBackend($property->getParameter('instanceof')); $value = $backend->buildObjectUri($value, $property->getParameter('instanceof')); $property->setValue($value instanceof ObjectUri ? $value : new ObjectUri($value)); } else { //$class = $property->getParameter('instanceof'); $backend = ObjectModel::getObjectBackend($property->getParameter('instanceof')); $value = $backend->buildObjectUri($value, $property->getParameter('instanceof')); $property->setValue($value); } } } } else { $property->resetValue(); } } else { if ($property instanceof Property\CollectionProperty) { // @todo handle collection populating here } else { // if value is an array, it is most likely a set of multiple options if (is_array($value)) { $value = implode('|', $value); } $property->setValue($value); } } } } return $this; }
/** * Detect all modules directories and try to get config for them * @param string $path */ public static function init($path) { self::$_path = $path . 'application/modules' . DIRECTORY_SEPARATOR; if (!is_dir(self::$_path)) { return false; } // get config from cache if (Core::getEnvData('cache_configs') == "bogus") { $ckey = 'configs_modules'; if (($cached = Core::cacheGet($ckey)) !== false) { self::$_config = $cached; } } if (true) { //! self::$_config) { self::$_config = array(); self::$_modules = array('enabled' => array(), 'disabled' => array()); foreach (scandir(self::$_path) as $vendor) { if (is_dir(self::$_path . $vendor) && substr($vendor, 0, 1) != '.') { foreach (scandir(self::$_path . $vendor) as $entry) { $fpath = self::$_path . $vendor . DIRECTORY_SEPARATOR . $entry . DIRECTORY_SEPARATOR; if (is_dir($fpath) && substr($entry, 0, 1) != '.') { if (is_dir($fpath . 'configs')) { // register path with $vendor as prefix Config::addPath($fpath . 'configs', Config::REALM_MODULES, null, $vendor); } } } } } // load all detected modules configuration file $config = Config\Loader::loadConfig('module.xml', Config::REALM_MODULES); // remove useless "modules" key foreach ($config as $key => $val) { self::$_config[$key] = $val['modules']; } // cache config if cache is enabled if (isset($ckey)) { Core::cacheSet(self::$_config, $ckey); } } unset($ckey); // build menu if (Core::getEnvData('cache_configs') == "bogus") { $ckey = 'configs_menu_main'; if (($cached = Core::cacheGet($ckey)) !== false) { $menu = $cached; } } if (true) { //! isset($menu)) { $menu = new Layout\Menu('main'); $menu->setLabel('Main Menu'); foreach (self::$_config as $prefix => $modules) { foreach ($modules as $key => $module) { if ($module['enabled'] != 'true') { continue; } $path = Core::$basePath . 'application/modules' . DIRECTORY_SEPARATOR . $prefix . DIRECTORY_SEPARATOR . $key; self::bind($module, $path); // if module has controllers, declare them to front controller // then add them to main menu if (isset($module['controller']) || isset($module['controllers_extends'])) { Config::addPath($path . '/controllers/', Config::REALM_CONTROLLERS, null, $module['controller']['base']); } if (isset($module['controller'])) { $menu->addItem($module['controller']['base'], $module['controller']); } // if module extends existing controllers, declare them for inclusion at the end of the process if (isset($module['controllers_extends']) && !empty($module['controllers_extends'])) { $menu->registerExtends($module['controller']['base'], $module['controllers_extends']); } } } // process extends declaration when menu is complete $menu->proceedExtends(); if (isset($ckey)) { Core::cacheSet($menu, $ckey); } } Layout::addMenu('main', $menu); }