The class allows you to find and automatically join database records and to convert the result into objects. It also supports creating new objects and persisting them in the database. Usage: Write $user = new UserModel(); $user->name = 'Leo Feyer'; $user->city = 'Wuppertal'; $user->save(); Read $user = UserModel::findByCity('Wuppertal'); while ($user->next()) { echo $user->name; }
Inheritance: extends System
 /**
  * Set the DataContainer data with an object of type Model
  * @param Model $objModel
  */
 public function setDataFromModel(Model $objModel)
 {
     $arrData = $objModel->row();
     foreach ($arrData as $key => $value) {
         if (strpos($key, $this->strDataPrefix) !== false) {
             $this->arrData[$key] = $value;
         }
     }
     $this->parseGivenData();
 }
 /**
  * @param $dc
  */
 public static function deleteDataRecord($dc)
 {
     // If this is not the backend than return
     if (TL_MODE != 'BE') {
         return;
     }
     // Check if there is an active record
     if ($dc instanceof DataContainer && $dc->activeRecord) {
         $intId = $dc->activeRecord->id;
         $strTable = $dc->table;
         $strModel = '\\' . Model::getClassFromTable($strTable);
         // Return if the class does not exist (#9 thanks to tsarma)
         if (!class_exists($strModel)) {
             return;
         }
         // Get object from model
         $objModel = $strModel::findByPk($intId);
         if ($objModel !== null) {
             $arrData = $objModel->row();
             if (is_array($arrData) && count($arrData) > 0) {
                 // Load current data container
                 Controller::loadDataContainer($strTable);
                 // Get tl_undo data
                 $objUndo = Database::getInstance()->prepare("SELECT * FROM tl_undo WHERE fromTable=? ORDER BY id DESC")->limit(1)->execute($dc->table);
                 $arrSet = $objUndo->row();
                 // Deserialize tl_undo data
                 $arrSet['data'] = deserialize($arrSet['data']);
                 foreach ($arrData as $strField => $varValue) {
                     $strInputType = $GLOBALS['TL_DCA'][$strTable]['fields'][$strField]['inputType'];
                     switch ($strInputType) {
                         case 'TranslationInputUnit':
                         case 'TranslationTextArea':
                         case 'TranslationTextField':
                             $intFid = $varValue;
                             if ($strInputType == 'TranslationInputUnit') {
                                 $arrDeserialized = deserialize($varValue);
                                 $intFid = $arrDeserialized['value'];
                             }
                             // Get translation values
                             $objTranslation = TranslationFieldsModel::findByFid($intFid);
                             if ($objTranslation !== null) {
                                 while ($objTranslation->next()) {
                                     $t = TranslationFieldsModel::getTable();
                                     // Add cross table record to undo data
                                     $arrSet['data'][$t][] = $objTranslation->row();
                                     // Delete translation
                                     $objTranslation->delete();
                                 }
                             }
                             break;
                     }
                 }
                 // Serialize tl_undo data
                 $arrSet['data'] = serialize($arrSet['data']);
                 // Update tl_undo
                 Database::getInstance()->prepare("UPDATE tl_undo %s WHERE id=?")->set($arrSet)->execute($objUndo->id);
             }
         }
     }
 }
 /**
  * Run the controller and parse the template
  */
 public function run()
 {
     $template = new BackendTemplate('be_main');
     $template->main = '';
     // Ajax request
     if ($_POST && Environment::get('isAjaxRequest')) {
         $this->objAjax = new Ajax(Input::post('action'));
         $this->objAjax->executePreActions();
     }
     $strTable = Input::get('table');
     $strField = Input::get('field');
     // Define the current ID
     define('CURRENT_ID', Input::get('table') ? $this->Session->get('CURRENT_ID') : Input::get('id'));
     Controller::loadDataContainer($strTable);
     $strDriver = 'DC_' . $GLOBALS['TL_DCA'][$strTable]['config']['dataContainer'];
     $objDca = new $strDriver($strTable);
     $objDca->field = $strField;
     // Set the active record
     if ($this->Database->tableExists($strTable)) {
         /** @var Model $strModel $strModel */
         $strModel = Model::getClassFromTable($strTable);
         if (class_exists($strModel)) {
             $objModel = $strModel::findByPk(Input::get('id'));
             if ($objModel !== null) {
                 $objDca->activeRecord = $objModel;
             }
         }
     }
     // AJAX request
     if ($_POST && Environment::get('isAjaxRequest')) {
         $this->objAjax->executePostActions($objDca);
     }
     $partial = new BackendTemplate('be_rte_table_editor');
     $template->isPopup = true;
     $template->main = $partial->parse();
     $template->theme = Backend::getTheme();
     $template->base = Environment::get('base');
     $template->language = $GLOBALS['TL_LANGUAGE'];
     $template->title = specialchars($GLOBALS['TL_LANG']['MSC']['pagepicker']);
     $template->charset = Config::get('characterSet');
     Config::set('debugMode', false);
     $template->output();
 }
Example #4
0
 /**
  * Run the controller and parse the template
  *
  * @return Response
  */
 public function run()
 {
     /** @var SessionInterface $objSession */
     $objSession = \System::getContainer()->get('session');
     /** @var BackendTemplate|object $objTemplate */
     $objTemplate = new \BackendTemplate('be_picker');
     $objTemplate->main = '';
     // Ajax request
     if ($_POST && \Environment::get('isAjaxRequest')) {
         $this->objAjax = new \Ajax(\Input::post('action'));
         $this->objAjax->executePreActions();
     }
     $strTable = \Input::get('table');
     $strField = \Input::get('field');
     // Define the current ID
     define('CURRENT_ID', \Input::get('table') ? $objSession->get('CURRENT_ID') : \Input::get('id'));
     $this->loadDataContainer($strTable);
     $strDriver = 'DC_' . $GLOBALS['TL_DCA'][$strTable]['config']['dataContainer'];
     $objDca = new $strDriver($strTable);
     $objDca->field = $strField;
     // Set the active record
     if ($this->Database->tableExists($strTable)) {
         /** @var Model $strModel */
         $strModel = \Model::getClassFromTable($strTable);
         if (class_exists($strModel)) {
             $objModel = $strModel::findByPk(\Input::get('id'));
             if ($objModel !== null) {
                 $objDca->activeRecord = $objModel;
             }
         }
     }
     // AJAX request
     if ($_POST && \Environment::get('isAjaxRequest')) {
         $this->objAjax->executePostActions($objDca);
     }
     $objSession->set('filePickerRef', \Environment::get('request'));
     $arrValues = array_filter(explode(',', \Input::get('value')));
     // Convert UUIDs to binary
     foreach ($arrValues as $k => $v) {
         // Can be a UUID or a path
         if (\Validator::isStringUuid($v)) {
             $arrValues[$k] = \StringUtil::uuidToBin($v);
         }
     }
     // Call the load_callback
     if (is_array($GLOBALS['TL_DCA'][$strTable]['fields'][$strField]['load_callback'])) {
         foreach ($GLOBALS['TL_DCA'][$strTable]['fields'][$strField]['load_callback'] as $callback) {
             if (is_array($callback)) {
                 $this->import($callback[0]);
                 $arrValues = $this->{$callback[0]}->{$callback[1]}($arrValues, $objDca);
             } elseif (is_callable($callback)) {
                 $arrValues = $callback($arrValues, $objDca);
             }
         }
     }
     /** @var FileSelector $strClass */
     $strClass = $GLOBALS['BE_FFL']['fileSelector'];
     /** @var FileSelector $objFileTree */
     $objFileTree = new $strClass($strClass::getAttributesFromDca($GLOBALS['TL_DCA'][$strTable]['fields'][$strField], $strField, $arrValues, $strField, $strTable, $objDca));
     /** @var AttributeBagInterface $objSessionBag */
     $objSessionBag = $objSession->getBag('contao_backend');
     $objTemplate->main = $objFileTree->generate();
     $objTemplate->theme = \Backend::getTheme();
     $objTemplate->base = \Environment::get('base');
     $objTemplate->language = $GLOBALS['TL_LANGUAGE'];
     $objTemplate->title = specialchars($GLOBALS['TL_LANG']['MSC']['filepicker']);
     $objTemplate->charset = \Config::get('characterSet');
     $objTemplate->addSearch = true;
     $objTemplate->search = $GLOBALS['TL_LANG']['MSC']['search'];
     $objTemplate->searchExclude = $GLOBALS['TL_LANG']['MSC']['searchExclude'];
     $objTemplate->action = ampersand(\Environment::get('request'));
     $objTemplate->value = $objSessionBag->get('file_selector_search');
     $objTemplate->manager = $GLOBALS['TL_LANG']['MSC']['fileManager'];
     $objTemplate->managerHref = 'contao/main.php?do=files&popup=1';
     $objTemplate->breadcrumb = $GLOBALS['TL_DCA']['tl_files']['list']['sorting']['breadcrumb'];
     if (\Input::get('switch')) {
         $objTemplate->switch = $GLOBALS['TL_LANG']['MSC']['pagePicker'];
         $objTemplate->switchHref = str_replace('contao/file?', 'contao/page?', ampersand(\Environment::get('request')));
     }
     return $objTemplate->getResponse();
 }
Example #5
0
 /**
  * @param null $dc
  * @throws \Exception
  */
 public function setAutoPassword($dc = null)
 {
     // Front end call
     if (!$dc instanceof DataContainer) {
         return;
     }
     if ($this->isDisabledAccountmail($dc)) {
         return;
     }
     $intId = $dc->id;
     if (Input::get('act') == 'overrideAll' && Input::get('fields') && $intId === null) {
         // Define indicator for given or not given password on overrideAll mode
         if (!isset($GLOBALS['ACCOUNTMAIL']['AUTO_PASSWORD'])) {
             $strPassword = $this->getPostPassword();
             $GLOBALS['ACCOUNTMAIL']['AUTO_PASSWORD'] = $strPassword == '' || $strPassword == '*****' ? true : false;
             if ($GLOBALS['ACCOUNTMAIL']['AUTO_PASSWORD'] === true) {
                 // Set password, that no error occurs with "password not set"
                 $strNewPassword = substr(str_shuffle('abcdefghkmnpqrstuvwxyzABCDEFGHKMNOPQRSTUVWXYZ0123456789'), 0, 8);
                 $this->setPostPassword($strNewPassword);
             }
             Message::addConfirmation($GLOBALS['TL_LANG']['MSC']['pw_changed']);
         }
         return;
     }
     $strPassword = $this->getPostPassword($intId);
     if ($strPassword !== null && $strPassword == '') {
         $strModel = Model::getClassFromTable($dc->table);
         $objAccount = $strModel::findByPk($intId);
         if ($objAccount !== null) {
             $strNewPassword = substr(str_shuffle('abcdefghkmnpqrstuvwxyzABCDEFGHKMNOPQRSTUVWXYZ0123456789'), 0, 8);
             $this->setPostPassword($strNewPassword, $intId);
             Message::addConfirmation($GLOBALS['TL_LANG']['MSC']['pw_changed']);
             $objAccount->password = Encryption::hash($strNewPassword);
             $objAccount->save();
         }
     }
 }
 protected function getModel($id)
 {
     /** @var Model $class */
     $class = Model::getClassFromTable($this->table);
     return $class::findByPk($id);
 }
Example #7
0
 /**
  * Unregister the contao.dns-fallback alias when the model is detached from the registry
  *
  * @param Model\Registry $registry The model registry
  */
 public function onUnregister(Model\Registry $registry)
 {
     parent::onUnregister($registry);
     // Unregister the fallback page
     if ($this->fallback && $this->type == 'root' && $registry->isRegisteredAlias($this, 'contao.dns-fallback', $this->dns)) {
         $registry->unregisterAlias($this, 'contao.dns-fallback', $this->dns);
     }
 }
Example #8
0
 /**
  * Compile a Model class name from a table name (e.g. tl_form_field becomes FormFieldModel)
  *
  * @param string $strTable The table name
  *
  * @return string The model class name
  *
  * @deprecated Deprecated since Contao 4.0, to be removed in Contao 5.0.
  *             Use Model::getClassFromTable() instead.
  */
 public static function getModelClassFromTable($strTable)
 {
     @trigger_error('Using System::getModelClassFromTable() has been deprecated and will no longer work in Contao 5.0. Use Model::getClassFromTable() instead.', E_USER_DEPRECATED);
     return \Model::getClassFromTable($strTable);
 }
Example #9
0
 /**
  * Check if an alias is registered
  *
  * @param Model  $objModel The model object
  * @param string $strAlias The alias name
  * @param mixed  $varValue The value of the alias
  *
  * @return boolean True if the alias is registered
  */
 public function isRegisteredAlias(Model $objModel, $strAlias, $varValue)
 {
     $strTable = $objModel->getTable();
     return isset($this->arrAliases[$strTable][$strAlias][$varValue]);
 }
 private function getModelClass()
 {
     Controller::loadDataContainer($this->getTable());
     return Model::getClassFromTable($GLOBALS['TL_DCA'][$this->getTable()]['config']['ptable']);
 }