/** * Get Options builder for collection. * * @param Result $result Database result. * @param string $valueColumn Value column. * @param string|\callable $labelColumn Label column or callback. * * @return OptionsBuilder */ public static function fromResult(Result $result = null, $valueColumn = 'id', $labelColumn = null) { if ($result->numRows < 1) { return new static(new ArrayOptions()); } $options = new ArrayListOptions($result->fetchAllAssoc()); $options->setValueKey($valueColumn); if (is_callable($labelColumn)) { $options->setLabelCallback($labelColumn); } elseif ($labelColumn) { $options->setLabelKey($labelColumn); } else { $options->setLabelKey($valueColumn); } return new static($options); }
/** * Fetch the next result row and create the model * * @return boolean True if there was another row */ protected function fetchNext() { if ($this->objResult->next() == false) { return false; } $strClass = $this->getModelClassFromTable($this->strTable); $this->arrModels[$this->intIndex + 1] = new $strClass($this->objResult); return true; }
/** * Load the relations and optionally process a result set * * @param \Database\Result $objResult An optional database result */ public function __construct(\Database\Result $objResult = null) { parent::__construct(); $objRelations = new \DcaExtractor(static::$strTable); $this->arrRelations = $objRelations->getRelations(); if ($objResult !== null) { $this->arrData = $objResult->row(); // Look for joined fields foreach ($this->arrData as $k => $v) { if (strpos($k, '__') !== false) { list($key, $field) = explode('__', $k, 2); // Create the related model if (!isset($this->arrRelated[$key])) { $table = $this->arrRelations[$key]['table']; $strClass = $this->getModelClassFromTable($table); $this->arrRelated[$key] = new $strClass(); } $this->arrRelated[$key]->{$field} = $v; unset($this->arrData[$k]); } } } }
/** * Create a new setting. * * @param Result $dbResult The information from which to initialize the setting from. * * @param ICollection $filterSettings The MetaModel filter settings. * * @return ISimple|null */ protected function createSetting($dbResult, $filterSettings) { $factory = $this->getTypeFactory($dbResult->type); if ($factory) { $setting = $factory->createInstance($dbResult->row(), $filterSettings); if (!$setting) { return null; } // Collect next level. if ($factory->isNestedType()) { /** @var IWithChildren $setting */ $this->collectRulesFor($setting, $filterSettings); } return $setting; } return null; }
/** * Create an entity based on a database result. * * @param Result $result The database result. * @param string $tableName The table name. * * @return Entity */ public function createFromResult(Result $result, $tableName) { return $this->createFromArray($result->row(), $tableName); }
/** * Add a data row to the XML document * @param \DOMDocument * @param \DOMElement * @param \Database\Result * @param array */ protected function addDataRow(\DOMDocument $xml, \DOMElement $table, \Database\Result $objData, array $arrOrder = array()) { $t = $table->getAttribute('name'); $row = $xml->createElement('row'); $row = $table->appendChild($row); foreach ($objData->row() as $k => $v) { $field = $xml->createElement('field'); $field->setAttribute('name', $k); $field = $row->appendChild($field); if ($v === null) { $v = 'NULL'; } elseif ($GLOBALS['TL_DCA'][$t]['fields'][$k]['inputType'] == 'fileTree' && !$GLOBALS['TL_DCA'][$t]['fields'][$k]['eval']['multiple']) { $objFile = \FilesModel::findByUuid($v); if ($objFile !== null) { // Standardize the upload path if it is not "files" if (\Config::get('uploadPath') != 'files') { $v = 'files/' . preg_replace('@^' . preg_quote(\Config::get('uploadPath'), '@') . '/@', '', $objFile->path); } else { $v = $objFile->path; } } else { $v = 'NULL'; } } elseif ($GLOBALS['TL_DCA'][$t]['fields'][$k]['inputType'] == 'fileTree' || in_array($k, $arrOrder)) { $arrFiles = deserialize($v); if (!empty($arrFiles) && is_array($arrFiles)) { $objFiles = \FilesModel::findMultipleByUuids($arrFiles); if ($objFiles !== null) { // Standardize the upload path if it is not "files" if (\Config::get('uploadPath') != 'files') { $arrTmp = array(); while ($objFiles->next()) { $arrTmp[] = 'files/' . preg_replace('@^' . preg_quote(\Config::get('uploadPath'), '@') . '/@', '', $objFiles->path); } $v = serialize($arrTmp); } else { $v = serialize($objFiles->fetchEach('path')); } } else { $v = 'NULL'; } } } $value = $xml->createTextNode($v); $field->appendChild($value); } }
/** * 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); }
/** * Run the tests on a table * * @param string $table * @param Result $records * * @return array */ protected function runTests($table, Result $records) { System::loadLanguageFile('seo_serp_tests'); $result = ['errors' => 0, 'warnings' => 0]; /** @var TestInterface $test */ foreach (TestsManager::getAll() as $test) { // Skip the unsupported tests if (!$test->supports($table)) { continue; } while ($records->next()) { try { $test->run($records->row(), $table); } catch (ErrorException $e) { $result['errors']++; } catch (WarningException $e) { $result['warnings']++; } } $records->reset(); } return $result; }
/** * Convert a database result to a result array. * * @param \Database\Result $objRow The database result. * * @param string[] $arrAttrOnly The list of attributes to return, if any. * * @return array */ protected function convertRowsToResult($objRow, $arrAttrOnly = array()) { $arrResult = array(); while ($objRow->next()) { $arrData = array(); foreach ($objRow->row() as $strKey => $varValue) { if (!$arrAttrOnly || in_array($strKey, $arrAttrOnly)) { $arrData[$strKey] = $this->tryUnserialize($varValue); } } /** @noinspection PhpUndefinedFieldInspection */ $arrResult[$objRow->id] = $arrData; } return $arrResult; }
/** * Add the table tl_theme * * @param \DOMDocument $xml * @param \DOMNode|\DOMElement $tables * @param \Database\Result|object $objTheme */ protected function addTableTlTheme(\DOMDocument $xml, \DOMNode $tables, \Database\Result $objTheme) { // Add the table $table = $xml->createElement('table'); $table->setAttribute('name', 'tl_theme'); $table = $tables->appendChild($table); // Load the DCA $this->loadDataContainer('tl_theme'); // Get the order fields $objDcaExtractor = \DcaExtractor::getInstance('tl_theme'); $arrOrder = $objDcaExtractor->getOrderFields(); // Add the row $this->addDataRow($xml, $table, $objTheme->row(), $arrOrder); }
/** * Create a model from a database result. * * @param \Database\Result $dbResult The database result to create a model from. * * @return ModelInterface */ protected function createModelFromDatabaseResult($dbResult) { $objModel = $this->getEmptyModel(); /** @var \Contao\Database\Result $dbResult */ foreach ($dbResult->row() as $key => $value) { if ($key == 'id') { $objModel->setID($value); } $objModel->setPropertyRaw($key, deserialize($value)); } return $objModel; }
/** * Compile the newsletter and send it * @param \Email * @param \Database\Result * @param array * @param string * @param string * @param string * @return string */ protected function sendNewsletter(\Email $objEmail, \Database\Result $objNewsletter, $arrRecipient, $text, $body, $css = null) { // Prepare the text content $text = \String::parseSimpleTokens($text, $arrRecipient); // add piwik campaign links $text = $this->addPiwikCampaignText($text, $objNewsletter->hoja_piwik_campaign); $objEmail->text = $text; // Add the HTML content if (!$objNewsletter->sendText) { // Default template if ($objNewsletter->template == '') { $objNewsletter->template = 'mail_default'; } // Load the mail template $objTemplate = new \BackendTemplate($objNewsletter->template); $objTemplate->setData($objNewsletter->row()); $objTemplate->title = $objNewsletter->subject; $objTemplate->body = $body; $objTemplate->charset = \Config::get('characterSet'); $objTemplate->css = $css; // Backwards compatibility $this->addCssToTemplate($objTemplate); $objTemplate->recipient = $arrRecipient['email']; // Parse template $html = $objTemplate->parse(); $html = $this->convertRelativeUrls($html); $html = $this->replaceInsertTags($html); // $html = $this->prepareLinkTracking($html, $objNewsletter->id, $arrRecipient['email'], $arrRecipient['extra'] ?: ''); $html = $this->parseSimpleTokens($html, $arrRecipient); // add piwik campaign links $html = $this->addPiwikCampaignHtml($html, $objNewsletter->hoja_piwik_campaign); // Append to mail object $objEmail->html = $html; $objEmail->imageDir = TL_ROOT . '/'; } // Deactivate invalid addresses try { $objEmail->sendTo($arrRecipient['email']); } catch (\Swift_RfcComplianceException $e) { $_SESSION['REJECTED_RECIPIENTS'][] = $arrRecipient['email']; } // Rejected recipients if ($objEmail->hasFailures()) { $_SESSION['REJECTED_RECIPIENTS'][] = $arrRecipient['email']; } // HOOK: add custom logic if (isset($GLOBALS['TL_HOOKS']['sendNewsletter']) && is_array($GLOBALS['TL_HOOKS']['sendNewsletter'])) { foreach ($GLOBALS['TL_HOOKS']['sendNewsletter'] as $callback) { $this->import($callback[0]); $this->{$callback}[0]->{$callback}[1]($objEmail, $objNewsletter, $arrRecipient, $text, $html); } } }
/** * Compile the newsletter and send it * @param \Email * @param \Database\Result * @param array * @param string * @param string * @param string * @return string */ protected function sendNewsletter(\Email $objEmail, \Database\Result $objNewsletter, $arrRecipient, $text, $html, $css = null) { // Prepare the text content $objEmail->text = \String::parseSimpleTokens($text, $arrRecipient); // Add the HTML content if (!$objNewsletter->sendText) { // Default template if ($objNewsletter->template == '') { $objNewsletter->template = 'mail_default'; } // Load the mail template $objTemplate = new \BackendTemplate($objNewsletter->template); $objTemplate->setData($objNewsletter->row()); $objTemplate->title = $objNewsletter->subject; $objTemplate->body = \String::parseSimpleTokens($html, $arrRecipient); $objTemplate->charset = $GLOBALS['TL_CONFIG']['characterSet']; $objTemplate->css = $css; // Backwards compatibility // Parse template $objEmail->html = $objTemplate->parse(); $objEmail->imageDir = TL_ROOT . '/'; } // Deactivate invalid addresses try { $objEmail->sendTo($arrRecipient['email']); } catch (Swift_RfcComplianceException $e) { $_SESSION['REJECTED_RECIPIENTS'][] = $arrRecipient['email']; } // Rejected recipients if ($objEmail->hasFailures()) { $_SESSION['REJECTED_RECIPIENTS'][] = $arrRecipient['email']; } }
/** * Clear the cache for a given database result and return the number of affected files. * * @param Result $result The database result. * * @return int */ private function clearCache($result) { $delete = array(); while ($result->next()) { $cacheFile = sprintf('system/cache/html/%s/%s.html', substr($result->cacheKey, 0, 1), $result->cacheKey); $this->fileSystem->delete($cacheFile); $delete[] = $result->id; } if ($delete) { $result = $this->database->execute(Query::removeEntries($delete)); return $result->affectedRows; } return 0; }
/** * Get Options builder for collection. * * @param Result $result Database result. * @param string|\callable $labelColumn Label column or callback. * @param string $valueColumn Value column. * * @return OptionsBuilder */ public static function fromResult(Result $result = null, $labelColumn = null, $valueColumn = 'id') { return static::fromArrayList($result->fetchAllAssoc(), $valueColumn, $labelColumn); }
/** * Convert the database result into a proper result array. * * @param \Database\Result $values The database result. * * @param string $aliasColumn The name of the alias column to be used. * * @param string $valueColumn The name of the value column. * * @param array $count The optional count array. * * @return array */ protected function convertOptionsList($values, $aliasColumn, $valueColumn, &$count = null) { $arrReturn = array(); while ($values->next()) { if (is_array($count)) { /** @noinspection PhpUndefinedFieldInspection */ $count[$values->{$aliasColumn}] = $values->mm_count; } $arrReturn[$values->{$aliasColumn}] = $values->{$valueColumn}; } return $arrReturn; }
/** * Compile the newsletter and send it * * @param \Email $objEmail * @param \Database\Result|object $objNewsletter * @param array $arrRecipient * @param string $text * @param string $html * @param string $css * * @return string */ protected function sendNewsletter(\Email $objEmail, \Database\Result $objNewsletter, $arrRecipient, $text, $html, $css = null) { // Prepare the text content $objEmail->text = \String::parseSimpleTokens($text, $arrRecipient); if (!$objNewsletter->sendText) { // Default template if ($objNewsletter->template == '') { $objNewsletter->template = 'mail_default'; } /** @var \BackendTemplate|object $objTemplate */ $objTemplate = new \BackendTemplate($objNewsletter->template); $objTemplate->setData($objNewsletter->row()); $objTemplate->title = $objNewsletter->subject; $objTemplate->body = \String::parseSimpleTokens($html, $arrRecipient); $objTemplate->charset = \Config::get('characterSet'); $objTemplate->css = $css; // Backwards compatibility $objTemplate->recipient = $arrRecipient['email']; // Parse template $objEmail->html = $objTemplate->parse(); $objEmail->imageDir = TL_ROOT . '/'; } // Deactivate invalid addresses try { $objEmail->sendTo($arrRecipient['email']); } catch (\Swift_RfcComplianceException $e) { $_SESSION['REJECTED_RECIPIENTS'][] = $arrRecipient['email']; } // Rejected recipients if ($objEmail->hasFailures()) { $_SESSION['REJECTED_RECIPIENTS'][] = $arrRecipient['email']; } // HOOK: add custom logic if (isset($GLOBALS['TL_HOOKS']['sendNewsletter']) && is_array($GLOBALS['TL_HOOKS']['sendNewsletter'])) { foreach ($GLOBALS['TL_HOOKS']['sendNewsletter'] as $callback) { $this->import($callback[0]); $this->{$callback}[0]->{$callback}[1]($objEmail, $objNewsletter, $arrRecipient, $text, $html); } } }
/** * 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); }
/** * Load the relations and optionally process a result set * * @param \Database\Result $objResult An optional database result */ public function __construct(\Database\Result $objResult = null) { $this->arrModified = array(); $objDca = \DcaExtractor::getInstance(static::$strTable); $this->arrRelations = $objDca->getRelations(); if ($objResult !== null) { $arrRelated = array(); $arrData = $objResult->row(); // Look for joined fields foreach ($arrData as $k => $v) { if (strpos($k, '__') !== false) { list($key, $field) = explode('__', $k, 2); if (!isset($arrRelated[$key])) { $arrRelated[$key] = array(); } $arrRelated[$key][$field] = $v; unset($arrData[$k]); } } $objRegistry = \Model\Registry::getInstance(); // Create the related models foreach ($arrRelated as $key => $row) { $table = $this->arrRelations[$key]['table']; /** @var static $strClass */ $strClass = static::getClassFromTable($table); $intPk = $strClass::getPk(); // If the primary key is empty, set null (see #5356) if (!isset($row[$intPk])) { $this->arrRelated[$key] = null; } else { $objRelated = $objRegistry->fetch($table, $row[$intPk]); if ($objRelated !== null) { $objRelated->mergeRow($row); } else { /** @var static $objRelated */ $objRelated = new $strClass(); $objRelated->setRow($row); $objRegistry->register($objRelated); } $this->arrRelated[$key] = $objRelated; } } $this->setRow($arrData); // see #5439 $objRegistry->register($this); } }
/** * Add a data row to the XML document * @param \DOMDocument * @param \DOMElement * @param \Database\Result */ protected function addDataRow(\DOMDocument $xml, \DOMElement $table, \Database\Result $objData) { $row = $xml->createElement('row'); $row = $table->appendChild($row); foreach ($objData->row() as $k => $v) { $field = $xml->createElement('field'); $field->setAttribute('name', $k); $field = $row->appendChild($field); if ($v === null) { $v = 'NULL'; } elseif ($table->getAttribute('name') == 'tl_theme' && $k == 'screenshot' || $table->getAttribute('name') == 'tl_module' && $k == 'singleSRC' || $table->getAttribute('name') == 'tl_module' && $k == 'reg_homeDir') { $objFile = \FilesModel::findByPk($v); if ($objFile !== null) { // Standardize the upload path if it is not "files" if ($GLOBALS['TL_CONFIG']['uploadPath'] != 'files') { $v = 'files/' . preg_replace('@^' . preg_quote($GLOBALS['TL_CONFIG']['uploadPath'], '@') . '/@', '', $objFile->path); } else { $v = $objFile->path; } } } elseif ($table->getAttribute('name') == 'tl_theme' && $k == 'folders' || $table->getAttribute('name') == 'tl_module' && $k == 'multiSRC' || $table->getAttribute('name') == 'tl_layout' && $k == 'external') { $arrFiles = deserialize($v); if (is_array($arrFiles) && !empty($arrFiles)) { $objFiles = \FilesModel::findMultipleByIds($arrFiles); if ($objFiles !== null) { // Standardize the upload path if it is not "files" if ($GLOBALS['TL_CONFIG']['uploadPath'] != 'files') { $arrTmp = array(); while ($objFiles->next()) { $arrTmp[] = 'files/' . preg_replace('@^' . preg_quote($GLOBALS['TL_CONFIG']['uploadPath'], '@') . '/@', '', $objFiles->path); } $v = serialize($arrTmp); } else { $v = serialize($objFiles->fetchEach('path')); } } } } $value = $xml->createTextNode($v); $field->appendChild($value); } }