/** * Find users with a specific permission. * * @param string $permission The permission as string. * * @return \UserModel[] */ public function findUsersWithPermission($permission) { $users = array(); $result = $this->prepareStatement()->execute($permission, $permission); while ($result->next()) { $model = $this->registry->fetch('tl_user', $result->id); if (!$model) { $model = new \UserModel($result); } $users[] = $model; } return $users; }
/** * {@inheritdoc} */ public function getRelated($strKey, array $arrOptions = array()) { $arrRelation = Relations::getRelation(static::$strTable, $strKey); if ($arrRelation !== false) { /** @type \Model $strClass */ $strClass = static::getClassFromTable($arrRelation['related_table']); if (class_exists($strClass)) { $arrIds = \Database::getInstance()->prepare("SELECT " . $arrRelation['related_field'] . " FROM " . $arrRelation['table'] . " WHERE " . $arrRelation['reference_field'] . "=?")->execute($this->{$arrRelation['reference']})->fetchEach($arrRelation['related_field']); if (empty($arrIds)) { return null; } $collection = array(); // Fetch from registry first (only possible if no options and the relation field is the PK) if (empty($arrOptions) && $arrRelation['field'] === $strClass::getPk()) { foreach ($arrIds as $k => $id) { $model = \Model\Registry::getInstance()->fetch($arrRelation['related_table'], $id); if ($model !== null) { unset($arrIds[$k]); } $collection[$id] = $model; } } // Fetch remaining if (!empty($arrIds)) { $remainingModels = $strClass::findBy(array($arrRelation['related_table'] . "." . $arrRelation['field'] . " IN('" . implode("','", $arrIds) . "')"), null, $arrOptions); foreach ($remainingModels as $remaining) { $collection[$remaining->{$arrRelation['field']}] = $remaining; } } $this->arrRelated[$strKey] = new \Model\Collection($collection, $strClass::getTable()); } } return parent::getRelated($strKey, $arrOptions); }
/** * Find a translated page. * * @param int $pageId Current page. * @param string $language Page language. * * @return PageModel|null */ public function findTranslatedPage($pageId, $language) { $query = <<<SQL SELECT p.* FROM tl_page p JOIN tl_page r ON r.id = p.cca_rr_root AND r.language = ? WHERE p.languageMain = ? SQL; $result = $this->database->prepare($query)->limit(1)->execute($language, $pageId); if ($result->numRows < 1) { return null; } $page = $this->modelRegistry->fetch('tl_page', $result->id); if ($page) { return $page; } return new PageModel($result); }
protected static function find(array $arrOptions) { if (static::$strTable == '') { return null; } $arrOptions['table'] = static::$strTable; $arrOptions['additionalWhereSql'] = static::$strAdditionalWhereSql; $arrOptions['additionalSelectSql'] = static::$strAdditionalSelectSql; $arrOptions['additionalSql'] = static::$strAdditionalSql; if ($arrOptions['additionalSql']) { $arrOptions['group'] = static::$strAdditionalGroupBy; } if (static::$strAdditionalHavingSql) { $arrOptions['having'] = static::$strAdditionalHavingSql; } $strQuery = static::buildFindQuery($arrOptions); $objStatement = \Database::getInstance()->prepare($strQuery); // Defaults for limit and offset if (!isset($arrOptions['limit'])) { $arrOptions['limit'] = 0; } if (!isset($arrOptions['offset'])) { $arrOptions['offset'] = 0; } // Limit if ($arrOptions['limit'] > 0 || $arrOptions['offset'] > 0) { $objStatement->limit($arrOptions['limit'], $arrOptions['offset']); } $objStatement = static::preFind($objStatement); $objResult = $objStatement->execute($arrOptions['value']); if ($objResult->numRows < 1) { return null; } $objResult = static::postFind($objResult); if ($arrOptions['return'] == 'Model') { $strPk = static::$strPk; $intPk = $objResult->{$strPk}; // Try to load from the registry $objModel = \Model\Registry::getInstance()->fetch(static::$strTable, $intPk); if ($objModel !== null) { return $objModel->mergeRow($objResult->row()); } return static::createModelFromDbResult($objResult); } else { return static::createCollectionFromDbResult($objResult, static::$strTable); } }
/** * @param \Database\Result|int $result Result object or id * @param string $table * @param bool $update Update model to current result if model is in the registry * * @return \Model */ public static function create($result, $table, $update = false) { $model = null; $class = \Model::getClassFromTable($table); $id = is_object($result) ? $result->id : $result; $cto32 = version_compare(VERSION, '3.2', '>='); // Check the registry in Contao 3.2 if ($cto32) { $model = \Model\Registry::getInstance()->fetch($table, $id); // model is already in the registry, shall we update the model to the current record? // This will affect every reference which uses the model // You really have to know what u need - both is dangerous if ($model && $update) { $model->setRow($result->row()); } } // Model is not in the registry if (!$model) { if (is_object($result)) { $model = new $class($result); } else { // we have to get the result from the db $result = \Database::getInstance()->prepare("SELECT * FROM {$table} WHERE id=?")->execute($id); if ($result->numRows) { $model = new $class($result); } else { $model = new $class(); // do not set id if Contao 3.2 is used because a new record is created, so that id is ignored if (!$cto32) { $model->id = $id; } } } } return $model; }
/** * Find the language fallback page by hostname * * @param string $strHost The hostname * @param array $arrOptions An optional options array * * @return PageModel|null The model or null if there is not fallback page */ public static function findPublishedFallbackByHostname($strHost, array $arrOptions = array()) { // Try to load from the registry (see #8544) if (empty($arrOptions)) { $objModel = \Model\Registry::getInstance()->fetch(static::$strTable, $strHost, 'contao.dns-fallback'); if ($objModel !== null) { return $objModel; } } $t = static::$strTable; $arrColumns = array("{$t}.dns=? AND {$t}.fallback='1'"); if (isset($arrOptions['ignoreFePreview']) || !BE_USER_LOGGED_IN) { $time = \Date::floorToMinute(); $arrColumns[] = "({$t}.start='' OR {$t}.start<='{$time}') AND ({$t}.stop='' OR {$t}.stop>'" . ($time + 60) . "') AND {$t}.published='1'"; } return static::findOneBy($arrColumns, $strHost, $arrOptions); }
/** * Get all searchable pages and return them as array * * @param integer $pid * @param string $domain * @param boolean $blnIsSitemap * * @return array */ public static function findSearchablePages($pid = 0, $domain = '', $blnIsSitemap = false) { $time = \Date::floorToMinute(); $objDatabase = \Database::getInstance(); // Get published pages $objPages = $objDatabase->prepare("SELECT * FROM tl_page WHERE pid=? AND (start='' OR start<='{$time}') AND (stop='' OR stop>'" . ($time + 60) . "') AND published='1' ORDER BY sorting")->execute($pid); if ($objPages->numRows < 1) { return array(); } $arrPages = array(); $objRegistry = \Model\Registry::getInstance(); // Recursively walk through all subpages while ($objPages->next()) { $objPage = $objRegistry->fetch('tl_page', $objPages->id); if ($objPage === null) { $objPage = new \PageModel($objPages); } if ($objPage->type == 'regular') { // Searchable and not protected if ((!$objPage->noSearch || $blnIsSitemap) && (!$objPage->protected || \Config::get('indexProtected') && (!$blnIsSitemap || $objPage->sitemap == 'map_always')) && (!$blnIsSitemap || $objPage->sitemap != 'map_never')) { // Published if ($objPage->published && ($objPage->start == '' || $objPage->start <= $time) && ($objPage->stop == '' || $objPage->stop > $time + 60)) { $arrPages[] = $objPage->getAbsoluteUrl(); // Get articles with teaser $objArticles = $objDatabase->prepare("SELECT * FROM tl_article WHERE pid=? AND (start='' OR start<='{$time}') AND (stop='' OR stop>'" . ($time + 60) . "') AND published='1' AND showTeaser='1' ORDER BY sorting")->execute($objPages->id); if ($objArticles->numRows) { $feUrl = $objPage->getAbsoluteUrl('/articles/%s'); while ($objArticles->next()) { $arrPages[] = sprintf($feUrl, $objArticles->alias != '' && !\Config::get('disableAlias') ? $objArticles->alias : $objArticles->id); } } } } } // Get subpages if ((!$objPage->protected || \Config::get('indexProtected')) && ($arrSubpages = static::findSearchablePages($objPage->id, $domain, $blnIsSitemap)) != false) { $arrPages = array_merge($arrPages, $arrSubpages); } } return $arrPages; }
/** * Add a product to the collection * @param object * @param integer * @param array * @return ProductCollectionItem */ public function addProduct(IsotopeProduct $objProduct, $intQuantity, array $arrConfig = array()) { // !HOOK: additional functionality when adding product to collection if (isset($GLOBALS['ISO_HOOKS']['addProductToCollection']) && is_array($GLOBALS['ISO_HOOKS']['addProductToCollection'])) { foreach ($GLOBALS['ISO_HOOKS']['addProductToCollection'] as $callback) { $objCallback = \System::importStatic($callback[0]); $intQuantity = $objCallback->{$callback}[1]($objProduct, $intQuantity, $this); } } if ($intQuantity == 0) { return false; } $time = time(); $this->tstamp = $time; // Make sure collection is in DB before adding product if (!\Model\Registry::getInstance()->isRegistered($this)) { $this->save(); } // Remove uploaded files from session so they are not added to the next product (see #646) unset($_SESSION['FILES']); $objItem = $this->getItemForProduct($objProduct); $intMinimumQuantity = $objProduct->getMinimumQuantity(); if (null !== $objItem) { if ($objItem->quantity + $intQuantity < $intMinimumQuantity) { $_SESSION['ISO_INFO'][] = sprintf($GLOBALS['TL_LANG']['ERR']['productMinimumQuantity'], $objProduct->name, $intMinimumQuantity); $intQuantity = $intMinimumQuantity - $objItem->quantity; } $objItem->increaseQuantityBy($intQuantity); return $objItem; } else { if ($intQuantity < $intMinimumQuantity) { $_SESSION['ISO_INFO'][] = sprintf($GLOBALS['TL_LANG']['ERR']['productMinimumQuantity'], $objProduct->name, $intMinimumQuantity); $intQuantity = $intMinimumQuantity; } $objItem = new ProductCollectionItem(); $objItem->pid = $this->id; $objItem->tstamp = $time; $objItem->type = array_search(get_class($objProduct), Product::getModelTypes()); $objItem->product_id = $objProduct->{$objProduct->getPk()}; $objItem->sku = (string) $objProduct->sku; $objItem->name = (string) $objProduct->name; $objItem->options = $objProduct->getOptions(); $objItem->quantity = (int) $intQuantity; $objItem->price = (double) ($objProduct->getPrice($this) ? $objProduct->getPrice($this)->getAmount((int) $intQuantity) : 0); $objItem->tax_free_price = (double) ($objProduct->getPrice($this) ? $objProduct->getPrice($this)->getNetAmount((int) $intQuantity) : 0); $objItem->jumpTo = (int) $arrConfig['jumpTo']->id; $objItem->save(); // Add the new item to our cache $this->arrItems[$objItem->id] = $objItem; return $objItem; } }
/** * Build model based on database result * * @param \Database\Result $objResult * * @return \Model * @deprecated use createModelFromDbResult in Contao 3.3 */ public static function buildModelType(\Database\Result $objResult = null) { if (null === $objResult) { return null; } $strPk = static::$strPk; $intPk = $objResult->{$strPk}; // Try to load from the registry /** @var \Model $objModel */ $objModel = \Model\Registry::getInstance()->fetch(static::$strTable, $intPk); if ($objModel !== null) { $objModel->mergeRow($objResult->row()); return $objModel; } return static::createModelFromDbResult($objResult); }
/** * Create a new collection from a database result * * @param \Database\Result $objResult The database result object * @param string $strTable The table name * * @return static The model collection */ public static function createFromDbResult(\Database\Result $objResult, $strTable) { $arrModels = array(); $strClass = \Model::getClassFromTable($strTable); while ($objResult->next()) { /** @var \Model $strClass */ $objModel = \Model\Registry::getInstance()->fetch($strTable, $objResult->{$strClass::getPk()}); if ($objModel !== null) { $objModel->mergeRow($objResult->row()); $arrModels[] = $objModel; } else { $arrModels[] = new $strClass($objResult); } } return new static($arrModels, $strTable); }
/** * Find records and return the model or model collection * * Supported options: * * * column: the field name * * value: the field value * * limit: the maximum number of rows * * offset: the number of rows to skip * * order: the sorting order * * eager: load all related records eagerly * * @param array $arrOptions The options array * * @return \Model|\Model\Collection|null A model, model collection or null if the result is empty */ protected static function find(array $arrOptions) { if (static::$strTable == '') { return null; } $arrOptions['table'] = static::$strTable; $strQuery = \Model\QueryBuilder::find($arrOptions); $objStatement = \Database::getInstance()->prepare($strQuery); // Defaults for limit and offset if (!isset($arrOptions['limit'])) { $arrOptions['limit'] = 0; } if (!isset($arrOptions['offset'])) { $arrOptions['offset'] = 0; } // Limit if ($arrOptions['limit'] > 0 || $arrOptions['offset'] > 0) { $objStatement->limit($arrOptions['limit'], $arrOptions['offset']); } $objStatement = static::preFind($objStatement); $objResult = $objStatement->execute($arrOptions['value']); if ($objResult->numRows < 1) { return null; } $objResult = static::postFind($objResult); if ($arrOptions['return'] == 'Model') { $strPk = static::$strPk; $intPk = $objResult->{$strPk}; // Try to load from the registry $objModel = \Model\Registry::getInstance()->fetch(static::$strTable, $intPk); if ($objModel !== null) { return $objModel->mergeRow($objResult->row()); } return new static($objResult); } else { return \Isotope\Collection\ProductPrice::createFromDbResult($objResult, static::$strTable); } }
<?php /** * Contao I18n provides some i18n structures for easily l10n websites. * * @package dev * @author David Molineus <*****@*****.**> * @copyright 2015 netzmacht creative David Molineus * @license LGPL 3.0 * @filesource * */ global $container; use Model\Registry; use Netzmacht\Contao\I18n\I18n; use Netzmacht\Contao\I18n\Model\Repository\PageRepository; use Netzmacht\Contao\I18n\Router; $container['i18n.pages'] = ['i18n_regular']; $container['i18n.page-repository'] = $container->share(function ($container) { return new PageRepository($container['database.connection'], Registry::getInstance(), Model::getClassFromTable('tl_page')); }); $container['i18n'] = $container->share(function ($container) { return new I18n($container['i18n.pages'], $container['i18n.page-repository']); }); $container['i18n.router'] = $container->share(function ($container) { $types = empty($GLOBALS['I18N_ROUTING']) ? [] : (array) $GLOBALS['I18N_ROUTING']; return new Router($container['i18n'], $types); });
return new StateRepository(); }; /** * Shared instance of the expression language being used for expession conditions. * * It's a shared service so that you can provide additional language functions for it. * * @return ExpressionLanguage */ $container['workflow.transition.expression-language'] = $container->share(function () { return new ExpressionLanguage(); }); /** * The Contao user repository. * * @return \Netzmacht\Workflow\Contao\Repository\UserRepository */ $container['workflow.repository.user'] = $container->share(function ($container) { return new \Netzmacht\Workflow\Contao\Repository\UserRepository($container['worfklow.database.connection'], \Model\Registry::getInstance()); }); /** * Workflow type provider. * * @return WorkflowTypeProvider */ $container['workflow.type-provider'] = $container->share(function () { $types = array_map(function ($typeClass) { return new $typeClass(); }, (array) $GLOBALS['WORKFLOW_TYPES']); return new WorkflowTypeProvider($types); });
/** * Generate address options and return it as HTML string * @param string * @return string */ protected function generateOptions($blnValidate = false) { $strBuffer = ''; $varValue = '0'; $arrOptions = $this->getAddressOptions(); if (!empty($arrOptions)) { foreach ($arrOptions as $option) { if ($option['default']) { $varValue = $option['value']; } } $strClass = $GLOBALS['TL_FFL']['radio']; $objWidget = new $strClass(array('id' => $this->getStepClass(), 'name' => $this->getStepClass(), 'mandatory' => true, 'options' => $arrOptions, 'value' => $varValue, 'onclick' => "Isotope.toggleAddressFields(this, '" . $this->getStepClass() . "_new');", 'storeValues' => true, 'tableless' => true)); // Validate input if ($blnValidate) { $objWidget->validate(); if ($objWidget->hasErrors()) { $this->blnError = true; } else { $varValue = (string) $objWidget->value; } } elseif ($objWidget->value != '') { \Input::setPost($objWidget->name, $objWidget->value); $objValidator = clone $objWidget; $objValidator->validate(); if ($objValidator->hasErrors()) { $this->blnError = true; } } $strBuffer .= $objWidget->parse(); } if ($varValue !== '0') { $this->Template->style = 'display:none;'; } $objAddress = $this->getAddressForOption($varValue, $blnValidate); if (null === $objAddress || !\Model\Registry::getInstance()->isRegistered($objAddress)) { $this->blnError = true; } elseif ($blnValidate) { $this->setAddress($objAddress); } return $strBuffer; }
/** * Find records and return the model or model collection * * Supported options: * * * column: the field name * * value: the field value * * limit: the maximum number of rows * * offset: the number of rows to skip * * order: the sorting order * * eager: load all related records eagerly * * @param array $arrOptions The options array * * @return static|Model\Collection|null A model, model collection or null if the result is empty */ protected static function find(array $arrOptions) { if (static::$strTable == '') { return null; } // Try to load from the registry if ($arrOptions['return'] == 'Model') { $arrColumn = (array) $arrOptions['column']; if (count($arrColumn) == 1 && ($arrColumn[0] == static::$strPk || in_array($arrColumn[0], static::getUniqueFields()))) { $varKey = is_array($arrOptions['value']) ? $arrOptions['value'][0] : $arrOptions['value']; $objModel = \Model\Registry::getInstance()->fetch(static::$strTable, $varKey, $arrColumn[0]); if ($objModel !== null) { return $objModel; } } } $arrOptions['table'] = static::$strTable; $strQuery = static::buildFindQuery($arrOptions); $objStatement = \Database::getInstance()->prepare($strQuery); // Defaults for limit and offset if (!isset($arrOptions['limit'])) { $arrOptions['limit'] = 0; } if (!isset($arrOptions['offset'])) { $arrOptions['offset'] = 0; } // Limit if ($arrOptions['limit'] > 0 || $arrOptions['offset'] > 0) { $objStatement->limit($arrOptions['limit'], $arrOptions['offset']); } $objStatement = static::preFind($objStatement); $objResult = $objStatement->execute($arrOptions['value']); if ($objResult->numRows < 1) { return null; } $objResult = static::postFind($objResult); // Try to load from the registry if ($arrOptions['return'] == 'Model') { $objModel = \Model\Registry::getInstance()->fetch(static::$strTable, $objResult->{static::$strPk}); if ($objModel !== null) { return $objModel->mergeRow($objResult->row()); } return static::createModelFromDbResult($objResult); } else { return static::createCollectionFromDbResult($objResult, static::$strTable); } }
/** * Do not allow to overwrite existing cache * @param bool * @return RequestCache * @throws \BadMethodCallException */ public function save() { if ($this->isModified() && \Model\Registry::getInstance()->isRegistered($this)) { throw new \BadMethodCallException('Can\'t save a modified cache'); } return parent::save(); }
/** * Return the debug bar string * * @return string The debug bar markup */ protected function getDebugBar() { $intReturned = 0; $intAffected = 0; // Count the totals (see #3884) if (is_array($GLOBALS['TL_DEBUG']['database_queries'])) { foreach ($GLOBALS['TL_DEBUG']['database_queries'] as $k => $v) { $intReturned += $v['return_count']; $intAffected += $v['affected_count']; unset($GLOBALS['TL_DEBUG']['database_queries'][$k]['return_count']); unset($GLOBALS['TL_DEBUG']['database_queries'][$k]['affected_count']); } } $intElapsed = microtime(true) - TL_START; $strDebug = sprintf("<!-- indexer::stop -->\n" . '<div id="contao-debug" class="%s">' . '<p>' . '<span class="debug-time">Execution time: %s ms</span>' . '<span class="debug-memory">Memory usage: %s</span>' . '<span class="debug-db">Database queries: %d</span>' . '<span class="debug-rows">Rows: %d returned, %s affected</span>' . '<span class="debug-models">Registered models: %d</span>' . '<span id="debug-tog"> </span>' . '</p>' . '<div><pre>', \Input::cookie('CONTAO_CONSOLE'), $this->getFormattedNumber($intElapsed * 1000, 0), $this->getReadableSize(memory_get_peak_usage()), count($GLOBALS['TL_DEBUG']['database_queries']), $intReturned, $intAffected, \Model\Registry::getInstance()->count()); ksort($GLOBALS['TL_DEBUG']); ob_start(); print_r($GLOBALS['TL_DEBUG']); $strDebug .= ob_get_contents(); ob_end_clean(); unset($GLOBALS['TL_DEBUG']); $strDebug .= '</pre></div></div>' . $this->generateInlineScript("(function(\$) {" . "\$\$('#contao-debug>*').setStyle('width',window.getSize().x);" . "\$(document.body).setStyle('margin-bottom',\$('contao-debug').hasClass('closed')?'60px':'320px');" . "\$('debug-tog').addEvent('click',function(e) {" . "\$('contao-debug').toggleClass('closed');" . "Cookie.write('CONTAO_CONSOLE',\$('contao-debug').hasClass('closed')?'closed':'',{path:'" . (TL_PATH ?: '/') . "'});" . "\$(document.body).setStyle('margin-bottom',\$('contao-debug').hasClass('closed')?'60px':'320px');" . "});" . "window.addEvent('resize',function() {" . "\$\$('#contao-debug>*').setStyle('width',window.getSize().x);" . "});" . "})(document.id);", $this->strFormat == 'xhtml') . "\n<!-- indexer::continue -->\n\n"; return $strDebug; }
/** * @return bool|\Model\UserSession */ public function isGuest() { $status = \Auth\Base::Check(); if ($status == false) { return true; } else { $data = User::model()->findByPk($status->iduser); \Registry::setCurrentUser($data); return false; } }