Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issue #17120: After picking a new question theme, the settings …
…are not properly updated (#1842) Code reorganization. - Added some comments. - Have created helper functions as to be able to have a better outlook of what some function do. Solution Summary. The key of the solution is to be able to pickup theme attributes from a theme name and not from a the attribute set on the question. Then added a theme override parameter which will set which theme attributes should be picked up. So now, question attributes may contain the attributes of a temp theme, which is not set on the DB. * Move QuestionAttribute related static methods to a new service class * Move QuestionAttribute related static methods to a new service class - Missing class Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>
- Loading branch information
1 parent
e48a951
commit fe3a90d
Showing
5 changed files
with
184 additions
and
72 deletions.
There are no files selected for viewing
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
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
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
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
129 changes: 129 additions & 0 deletions
129
application/models/services/QuestionAttributeHelper.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
namespace LimeSurvey\Models\Services; | ||
|
||
class QuestionAttributeHelper | ||
{ | ||
/** | ||
* Merges the 'base' attributes (ex: core question attributes) with the extended question attributes | ||
* (ex: question theme attributes). It also removes all attributes where extended attribute's inputType is | ||
* empty. | ||
* If an extended attribute's name cannot be determined, it's omitted. | ||
* | ||
* @param array $aBaseAttributes the base set of attributes | ||
* @param array $aExtendedAttributes the attributes to merge into the base set | ||
* | ||
* @return array the merged attributes | ||
*/ | ||
public function mergeQuestionAttributes($aBaseAttributes, $aExtendedAttributes) | ||
{ | ||
$aAttributes = $aBaseAttributes; | ||
foreach ($aExtendedAttributes as $key => $attribute) { | ||
// TODO: move to sanitizeQuestionAttributes() | ||
// Determine the attribute name or continue with the next | ||
if (!isset($attribute['name'])) { | ||
if (!is_numeric($key)) { | ||
$attribute['name'] = $key; | ||
} else { | ||
continue; | ||
} | ||
} | ||
|
||
$sAttributeName = $attribute['name']; | ||
$sInputType = $attribute['inputtype']; | ||
// remove attribute if inputtype is empty | ||
if (empty($sInputType)) { | ||
unset($aAttributes[$sAttributeName]); | ||
} else { | ||
$aCustomAttribute = array_merge( | ||
\QuestionAttribute::getDefaultSettings(), | ||
array("category" => gT("Template")), | ||
$attribute | ||
); | ||
$aAttributes[$sAttributeName] = $aCustomAttribute; | ||
} | ||
} | ||
return $aAttributes; | ||
} | ||
|
||
/** | ||
* Sanitizes an array of question attributes. | ||
* Currently just replaces empty arrays (generally resulting from empty xml nodes) with null. | ||
* | ||
* @param array $aAttributes the array of attributes to sanitize | ||
* | ||
* @return array the array of sanitized attributes | ||
*/ | ||
public function sanitizeQuestionAttributes($aAttributes) | ||
{ | ||
// Replace empty arrays with null | ||
foreach ($aAttributes as &$aAttribute) { | ||
foreach ($aAttribute as $propertyName => $propertyValue) { | ||
if ($propertyValue === []) { | ||
$aAttribute[$propertyName] = null; | ||
} | ||
} | ||
} | ||
return $aAttributes; | ||
} | ||
|
||
/** | ||
* Returns the received array of attributes filled with the values specified, taking into account the | ||
* 'i18n' property of the attributes. | ||
* | ||
* Both this and rewriteQuestionAttributeArray() are helper methods and accomplish quite similar tasks, | ||
* but the output is different: rewriteQuestionAttributeArray returns a name -> value array, while here | ||
* we return a complete definition map and the value as a piece of information mingled into it. | ||
* | ||
* @param array $aAttributes the attributes to be filled | ||
* @param array $aAttributeValues the values for the attributes | ||
* @param array $aLanguages the languages to use for i18n attributes | ||
* | ||
* @return array the same source attributes with their corresponding values (when available) | ||
*/ | ||
public function fillAttributesWithValues($aAttributes, $aAttributeValues, $aLanguages = []) | ||
{ | ||
foreach ($aAttributes as $iKey => $aAttribute) { | ||
if ($aAttribute['i18n'] == false) { | ||
if (isset($aAttributeValues[$aAttribute['name']][''])) { | ||
$aAttributes[$iKey]['value'] = $aAttributeValues[$aAttribute['name']]['']; | ||
} else { | ||
$aAttributes[$iKey]['value'] = $aAttribute['default']; | ||
} | ||
// Sanitize value in case it's saved as empty array | ||
if ($aAttributes[$iKey]['value'] === []) { | ||
$aAttributes[$iKey]['value'] = null; | ||
} | ||
} else { | ||
foreach ($aLanguages as $sLanguage) { | ||
if (isset($aAttributeValues[$aAttribute['name']][$sLanguage])) { | ||
$aAttributes[$iKey][$sLanguage]['value'] = $aAttributeValues[$aAttribute['name']][$sLanguage]; | ||
} else { | ||
$aAttributes[$iKey][$sLanguage]['value'] = $aAttribute['default']; | ||
} | ||
// Sanitize value in case it's saved as empty array | ||
if ($aAttributes[$iKey][$sLanguage]['value'] === []) { | ||
$aAttributes[$iKey][$sLanguage]['value'] = null; | ||
} | ||
} | ||
} | ||
} | ||
return $aAttributes; | ||
} | ||
|
||
/** | ||
* Receives an array of question attributes and groups them by category. | ||
* Used by advanced settings widget. | ||
* | ||
* @param array $aAttributes | ||
* @return array Grouped question attributes, with category as array key | ||
*/ | ||
public function groupAttributesByCategory($aAttributes) | ||
{ | ||
$aByCategory = []; | ||
foreach ($aAttributes as $aAttribute) { | ||
$aByCategory[$aAttribute['category']][] = $aAttribute; | ||
} | ||
return $aByCategory; | ||
} | ||
} |