Contao\Versions::create PHP Method

create() public method

Create a new version of a record
public create ( )
    public function create()
    {
        if (!$GLOBALS['TL_DCA'][$this->strTable]['config']['enableVersioning']) {
            return;
        }
        // Delete old versions from the database
        $tstamp = time() - intval(\Config::get('versionPeriod'));
        $this->Database->query("DELETE FROM tl_version WHERE tstamp<{$tstamp}");
        // Get the new record
        $objRecord = $this->Database->prepare("SELECT * FROM " . $this->strTable . " WHERE id=?")->limit(1)->execute($this->intPid);
        if ($objRecord->numRows < 1 || $objRecord->tstamp < 1) {
            return;
        }
        if ($this->strPath !== null) {
            $objFile = new \File($this->strPath);
            if ($objFile->extension == 'svgz') {
                $objRecord->content = gzdecode($objFile->getContent());
            } else {
                $objRecord->content = $objFile->getContent();
            }
        }
        $intVersion = 1;
        $objVersion = $this->Database->prepare("SELECT MAX(version) AS version FROM tl_version WHERE pid=? AND fromTable=?")->execute($this->intPid, $this->strTable);
        if ($objVersion->version !== null) {
            $intVersion = $objVersion->version + 1;
        }
        $strDescription = '';
        if (!empty($objRecord->title)) {
            $strDescription = $objRecord->title;
        } elseif (!empty($objRecord->name)) {
            $strDescription = $objRecord->name;
        } elseif (!empty($objRecord->firstname)) {
            $strDescription = $objRecord->firstname . ' ' . $objRecord->lastname;
        } elseif (!empty($objRecord->headline)) {
            $chunks = deserialize($objRecord->headline);
            if (is_array($chunks) && isset($chunks['value'])) {
                $strDescription = $chunks['value'];
            } else {
                $strDescription = $objRecord->headline;
            }
        } elseif (!empty($objRecord->selector)) {
            $strDescription = $objRecord->selector;
        } elseif (!empty($objRecord->subject)) {
            $strDescription = $objRecord->subject;
        }
        $this->Database->prepare("UPDATE tl_version SET active='' WHERE pid=? AND fromTable=?")->execute($this->intPid, $this->strTable);
        $this->Database->prepare("INSERT INTO tl_version (pid, tstamp, version, fromTable, username, userid, description, editUrl, active, data) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, ?)")->execute($this->intPid, time(), $intVersion, $this->strTable, $this->getUsername(), $this->getUserId(), $strDescription, $this->getEditUrl(), serialize($objRecord->row()));
        // Trigger the oncreate_version_callback
        if (is_array($GLOBALS['TL_DCA'][$this->strTable]['config']['oncreate_version_callback'])) {
            foreach ($GLOBALS['TL_DCA'][$this->strTable]['config']['oncreate_version_callback'] as $callback) {
                if (is_array($callback)) {
                    $this->import($callback[0]);
                    $this->{$callback[0]}->{$callback[1]}($this->strTable, $this->intPid, $intVersion, $objRecord->row());
                } elseif (is_callable($callback)) {
                    $callback($this->strTable, $this->intPid, $intVersion, $objRecord->row());
                }
            }
        }
        $this->log('Version ' . $intVersion . ' of record "' . $this->strTable . '.id=' . $this->intPid . '" has been created' . $this->getParentEntries($this->strTable, $this->intPid), __METHOD__, TL_GENERAL);
    }

Usage Example

 /**
  * Set the new password
  */
 protected function setNewPassword()
 {
     $objMember = \MemberModel::findOneByActivation(\Input::get('token'));
     if ($objMember === null || $objMember->login == '') {
         $this->strTemplate = 'mod_message';
         /** @var FrontendTemplate|object $objTemplate */
         $objTemplate = new \FrontendTemplate($this->strTemplate);
         $this->Template = $objTemplate;
         $this->Template->type = 'error';
         $this->Template->message = $GLOBALS['TL_LANG']['MSC']['accountError'];
         return;
     }
     $strTable = $objMember->getTable();
     // Initialize the versioning (see #8301)
     $objVersions = new \Versions($strTable, $objMember->id);
     $objVersions->setUsername($objMember->username);
     $objVersions->setUserId(0);
     $objVersions->setEditUrl('contao/main.php?do=member&act=edit&id=%s&rt=1');
     $objVersions->initialize();
     // Define the form field
     $arrField = $GLOBALS['TL_DCA']['tl_member']['fields']['password'];
     /** @var Widget $strClass */
     $strClass = $GLOBALS['TL_FFL']['password'];
     // Fallback to default if the class is not defined
     if (!class_exists($strClass)) {
         $strClass = 'FormPassword';
     }
     /** @var Widget $objWidget */
     $objWidget = new $strClass($strClass::getAttributesFromDca($arrField, 'password'));
     // Set row classes
     $objWidget->rowClass = 'row_0 row_first even';
     $objWidget->rowClassConfirm = 'row_1 odd';
     $this->Template->rowLast = 'row_2 row_last even';
     /** @var SessionInterface $objSession */
     $objSession = \System::getContainer()->get('session');
     // Validate the field
     if (strlen(\Input::post('FORM_SUBMIT')) && \Input::post('FORM_SUBMIT') == $objSession->get('setPasswordToken')) {
         $objWidget->validate();
         // Set the new password and redirect
         if (!$objWidget->hasErrors()) {
             $objSession->set('setPasswordToken', '');
             $objMember->tstamp = time();
             $objMember->activation = '';
             $objMember->password = $objWidget->value;
             $objMember->save();
             // Create a new version
             if ($GLOBALS['TL_DCA'][$strTable]['config']['enableVersioning']) {
                 $objVersions->create();
             }
             // HOOK: set new password callback
             if (isset($GLOBALS['TL_HOOKS']['setNewPassword']) && is_array($GLOBALS['TL_HOOKS']['setNewPassword'])) {
                 foreach ($GLOBALS['TL_HOOKS']['setNewPassword'] as $callback) {
                     $this->import($callback[0]);
                     $this->{$callback[0]}->{$callback[1]}($objMember, $objWidget->value, $this);
                 }
             }
             // Redirect to the jumpTo page
             if (($objTarget = $this->objModel->getRelated('reg_jumpTo')) instanceof PageModel) {
                 /** @var PageModel $objTarget */
                 $this->redirect($objTarget->getFrontendUrl());
             }
             // Confirm
             $this->strTemplate = 'mod_message';
             /** @var FrontendTemplate|object $objTemplate */
             $objTemplate = new \FrontendTemplate($this->strTemplate);
             $this->Template = $objTemplate;
             $this->Template->type = 'confirm';
             $this->Template->message = $GLOBALS['TL_LANG']['MSC']['newPasswordSet'];
             return;
         }
     }
     $strToken = md5(uniqid(mt_rand(), true));
     $objSession->set('setPasswordToken', $strToken);
     $this->Template->formId = $strToken;
     $this->Template->fields = $objWidget->parse();
     $this->Template->action = \Environment::get('indexFreeRequest');
     $this->Template->slabel = \StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['setNewPassword']);
 }
All Usage Examples Of Contao\Versions::create