Last active
March 6, 2020 09:43
-
-
Save cgi-caesar/b5ceb9fffa305fcc298e1268a35ab637 to your computer and use it in GitHub Desktop.
aMember (site.php): Profile form as brick for Signup form
This file contains hidden or 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 | |
/** | |
* @title Add All Profile Forms as Available bricks for Signup Forms | |
* | |
* If you have aMember version 6.1.7 or below then it is necessary to apply diff (x.diff) | |
* to file library/Am/Model/SavedForm.php in order to this code snippet work | |
* @path application/configs/site.php | |
*/ | |
Am_Di::getInstance()->hook->add(Am_Event::LOAD_BRICKS, function (Am_Event $e) { | |
class Am_Form_Brick_ProfileForm extends Am_Form_Brick | |
{ | |
/** @var SavedForm */ | |
protected $profileForm; | |
public function __construct($id = null, $config = null) | |
{ | |
$formId = str_replace('profile_', '', $id); | |
$this->profileForm = Am_Di::getInstance()->savedFormTable->load($formId); | |
parent::__construct($id, $config); | |
} | |
static function createAvailableBricks($className) | |
{ | |
$res = []; | |
foreach (Am_Di::getInstance()->savedFormTable->getOptions(SavedForm::T_PROFILE) as $id => $fn) { | |
$res[] = new self('profile_' . $id); | |
} | |
return $res; | |
} | |
function getName() | |
{ | |
return "Profile Form: {$this->profileForm->title}"; | |
} | |
function insertBrick(HTML_QuickForm2_Container $form) | |
{ | |
foreach ($this->profileForm->getBricks() as $brick) { | |
$brick->insertBrick($form); | |
} | |
} | |
function isAcceptableForForm(Am_Form_Bricked $form) | |
{ | |
return $form instanceof Am_Form_Signup; | |
} | |
} | |
}); |
This file contains hidden or 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
commit 78587a7a54940f7023524834d6371dbdfd31a8c3 | |
Author: Andrey Yerokhin <[email protected]> | |
Date: Thu Mar 5 20:13:25 2020 +0400 | |
- ret is same for all objects if it is static within method definition | |
diff --git a/library/Am/Model/SavedForm.php b/library/Am/Model/SavedForm.php | |
index 5af75f03c..28a6b813e 100644 | |
--- a/library/Am/Model/SavedForm.php | |
+++ b/library/Am/Model/SavedForm.php | |
@@ -27,6 +27,8 @@ class SavedForm extends Am_Record_WithData | |
const T_CART = 'cart'; | |
const T_PROFILE = 'profile'; | |
+ protected $_bricks = null; | |
+ | |
public function getTypeDef() | |
{ | |
return $this->getTable()->getTypeDef($this->type); | |
@@ -105,8 +107,7 @@ class SavedForm extends Am_Record_WithData | |
/** @return array of Am_Form_Brick */ | |
function getBricks() | |
{ | |
- static $ret = null; | |
- if (is_null($ret)) { | |
+ if (is_null($this->_bricks)) { | |
$ret = []; | |
foreach ($this->getFields() as $brickConfig) | |
{ | |
@@ -124,8 +125,9 @@ class SavedForm extends Am_Record_WithData | |
foreach ($ret as $brick) { | |
$brick->init(); | |
} | |
+ $this->_bricks = $ret; | |
} | |
- return $ret; | |
+ return $this->_bricks; | |
} | |
function isSingle() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment