Created
December 23, 2013 03:15
-
-
Save anonymous/8091249 to your computer and use it in GitHub Desktop.
Implementing a generateAjax method, which should work similar to the default generate method and is compatible with the Contao Ajax-Tools.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Ajaxify package for Contao CMS | |
* | |
* Developed by http://codeq.at | |
* | |
* @package ajaxify | |
* @author codeQ e.U. | |
* @license GPL | |
* @copyright codeQ e.U. 2013 | |
*/ | |
/** | |
* Namespace | |
*/ | |
namespace CodeQ; | |
/** | |
* Class ModuleRegistrationAjax | |
* | |
* This class extends the registration module to support json requests. | |
* | |
* Currently this class does not support Captchas. | |
* | |
* @copyright codeQ e.U. 2013 | |
* @author codeQ e.U. | |
* @package ajaxify | |
*/ | |
class ModuleRegistrationAjax extends \ModuleRegistration | |
{ | |
/** | |
* Display a wildcard in the back end | |
* @return string | |
*/ | |
public function generateAjax() | |
{ | |
if (TL_MODE == 'BE') | |
{ | |
header('HTTP/1.1 412 Precondition Failed'); | |
return 'This ajax method is only for the frontend.'; | |
} | |
if ($this->Input->get('pageId') == null) | |
{ | |
header('HTTP/1.1 412 Precondition Failed'); | |
return 'The registration module requires a pageId to determine the contect on registration.'; // PageModel jumpTo is looked up | |
} | |
// like normal generate, deserialize editable | |
$this->editable = deserialize($this->editable); | |
// Return if there are no editable fields | |
if (!is_array($this->editable) || empty($this->editable)) | |
{ | |
header('HTTP/1.1 412 Precondition Failed'); | |
return 'This module does not have any editable fields.'; | |
} | |
// taken from parent::generate | |
$this->Template = new \FrontendTemplate($this->strTemplate); | |
$this->Template->setData($this->arrData); | |
$this->compileAjax(); | |
// instead of parsing the template now we collect the data and | |
// create an response array | |
return array( | |
'headline' => $this->arrData['headline'], | |
'editable' => $this->arrData['editable'], | |
'enctype' => $this->Template->enctype, | |
'hasErrors' => $this->Template->hasError, | |
'fields' => $this->Template->fields | |
); | |
} | |
/** | |
* Generate the module data, this is very much the same as the original compile | |
*/ | |
protected function compileAjax() | |
{ | |
global $objPage; | |
$GLOBALS['TL_LANGUAGE'] = $objPage->language; | |
\System::loadLanguageFile('tl_member'); | |
$this->loadDataContainer('tl_member'); | |
// Call onload_callback (e.g. to check permissions) | |
if (is_array($GLOBALS['TL_DCA']['tl_member']['config']['onload_callback'])) | |
{ | |
foreach ($GLOBALS['TL_DCA']['tl_member']['config']['onload_callback'] as $callback) | |
{ | |
if (is_array($callback)) | |
{ | |
$this->import($callback[0]); | |
$this->$callback[0]->$callback[1](); | |
} | |
elseif (is_callable($callback)) | |
{ | |
$callback(); | |
} | |
} | |
} | |
// Activate account | |
if (\Input::get('token') != '') | |
{ | |
$this->activateAcount(); | |
return; | |
} | |
$this->Template->fields = array(); // <---------- changed for Ajax | |
$objCaptcha = null; | |
$doNotSubmit = false; | |
// Captcha | |
if (!$this->disableCaptcha) | |
{ | |
header('HTTP/1.1 412 Precondition Failed'); | |
return 'The ModuleRegistration::compileAjax does currently not support captchas.'; | |
} | |
$arrUser = array(); | |
$arrFields = array(); | |
$hasUpload = false; | |
$i = 0; | |
// Build form | |
foreach ($this->editable as $field) | |
{ | |
$arrData = $GLOBALS['TL_DCA']['tl_member']['fields'][$field]; | |
// Map checkboxWizard to regular checkbox widget | |
if ($arrData['inputType'] == 'checkboxWizard') | |
{ | |
$arrData['inputType'] = 'checkbox'; | |
} | |
$strClass = $GLOBALS['TL_FFL'][$arrData['inputType']]; | |
// Continue if the class is not defined | |
if (!class_exists($strClass)) | |
{ | |
continue; | |
} | |
$arrData['eval']['tableless'] = $this->tableless; | |
$arrData['eval']['required'] = $arrData['eval']['mandatory']; | |
$objWidget = new $strClass($strClass::getAttributesFromDca($arrData, $field, $arrData['default'], '', '', $this)); | |
$objWidget->storeValues = true; | |
$objWidget->rowClass = 'row_' . $i . (($i == 0) ? ' row_first' : '') . ((($i % 2) == 0) ? ' even' : ' odd'); | |
// Increase the row count if its a password field | |
if ($objWidget instanceof \FormPassword) | |
{ | |
$objWidget->rowClassConfirm = 'row_' . ++$i . ((($i % 2) == 0) ? ' even' : ' odd'); | |
} | |
// Validate input | |
if (\Input::post('FORM_SUBMIT') == 'tl_registration') | |
{ | |
$objWidget->validate(); | |
$varValue = $objWidget->value; | |
// Check whether the password matches the username | |
if ($objWidget instanceof \FormPassword && $varValue == \Input::post('username')) | |
{ | |
$objWidget->addError($GLOBALS['TL_LANG']['ERR']['passwordName']); | |
} | |
$rgxp = $arrData['eval']['rgxp']; | |
// Convert date formats into timestamps (check the eval setting first -> #3063) | |
if (($rgxp == 'date' || $rgxp == 'time' || $rgxp == 'datim') && $varValue != '') | |
{ | |
try | |
{ | |
$objDate = new \Date($varValue); | |
$varValue = $objDate->tstamp; | |
} | |
catch (\OutOfBoundsException $e) | |
{ | |
$objWidget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidDate'], $varValue)); | |
} | |
} | |
// Make sure that unique fields are unique (check the eval setting first -> #3063) | |
if ($arrData['eval']['unique'] && $varValue != '' && !$this->Database->isUniqueValue('tl_member', $field, $varValue)) | |
{ | |
$objWidget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['unique'], $arrData['label'][0] ?: $field)); | |
} | |
// Save callback | |
if ($objWidget->submitInput() && !$objWidget->hasErrors() && is_array($arrData['save_callback'])) | |
{ | |
foreach ($arrData['save_callback'] as $callback) | |
{ | |
try | |
{ | |
if (is_array($callback)) | |
{ | |
$this->import($callback[0]); | |
$varValue = $this->$callback[0]->$callback[1]($varValue, null); | |
} | |
elseif (is_callable($callback)) | |
{ | |
$varValue = $callback($varValue, null); | |
} | |
} | |
catch (\Exception $e) | |
{ | |
$objWidget->class = 'error'; | |
$objWidget->addError($e->getMessage()); | |
} | |
} | |
} | |
// Store the current value | |
if ($objWidget->hasErrors()) | |
{ | |
$doNotSubmit = true; | |
} | |
elseif ($objWidget->submitInput()) | |
{ | |
$arrUser[$field] = $varValue; | |
} | |
} | |
if ($objWidget instanceof \uploadable) | |
{ | |
$hasUpload = true; | |
} | |
// following lines changed for Ajax | |
//$this->Tempalte->fields .= $objWidget->parse(); | |
// because Template->fields is a magic function, array_push doesn't work | |
$arr = $this->Template->fields; | |
array_push($arr, array( | |
'name' => $objWidget->name, | |
'label' => $objWidget->label, | |
'value' => $objWidget->value, | |
'template' => $objWidget->template, | |
'wizard' => $objWidget->wizard, | |
'required' => $objWidget->required, | |
'errors' => $objWidget->getErrors() | |
)); | |
$this->Template->fields = $arr; | |
$arrFields[$arrData['eval']['feGroup']][$field] .= $objWidget->parse(); // <---------- changed for Ajax | |
++$i; | |
} | |
// !!!!!!! | |
// Captcha part removed, not supported in Ajax | |
$this->Template->rowLast = 'row_' . ++$i . ((($i % 2) == 0) ? ' even' : ' odd'); | |
$this->Template->enctype = $hasUpload ? 'multipart/form-data' : 'application/x-www-form-urlencoded'; | |
$this->Template->hasError = $doNotSubmit; | |
// Create new user if there are no errors | |
if (\Input::post('FORM_SUBMIT') == 'tl_registration' && !$doNotSubmit) | |
{ | |
$this->createNewUser($arrUser); | |
} | |
$this->Template->loginDetails = $GLOBALS['TL_LANG']['tl_member']['loginDetails']; | |
$this->Template->addressDetails = $GLOBALS['TL_LANG']['tl_member']['addressDetails']; | |
$this->Template->contactDetails = $GLOBALS['TL_LANG']['tl_member']['contactDetails']; | |
$this->Template->personalData = $GLOBALS['TL_LANG']['tl_member']['personalData']; | |
$this->Template->captchaDetails = $GLOBALS['TL_LANG']['MSC']['securityQuestion']; | |
// Add groups | |
foreach ($arrFields as $k=>$v) | |
{ | |
$this->Template->$k = $v; | |
} | |
$this->Template->captcha = $arrFields['captcha']; | |
$this->Template->formId = 'tl_registration'; | |
$this->Template->slabel = specialchars($GLOBALS['TL_LANG']['MSC']['register']); | |
$this->Template->action = \Environment::get('indexFreeRequest'); | |
// HOOK: add memberlist fields | |
if (in_array('memberlist', \ModuleLoader::getActive())) | |
{ | |
$this->Template->profile = $arrFields['profile']; | |
$this->Template->profileDetails = $GLOBALS['TL_LANG']['tl_member']['profileDetails']; | |
} | |
// HOOK: add newsletter fields | |
if (in_array('newsletter', \ModuleLoader::getActive())) | |
{ | |
$this->Template->newsletter = $arrFields['newsletter']; | |
$this->Template->newsletterDetails = $GLOBALS['TL_LANG']['tl_member']['newsletterDetails']; | |
} | |
// HOOK: add helpdesk fields | |
if (in_array('helpdesk', \ModuleLoader::getActive())) | |
{ | |
$this->Template->helpdesk = $arrFields['helpdesk']; | |
$this->Template->helpdeskDetails = $GLOBALS['TL_LANG']['tl_member']['helpdeskDetails']; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment