Created
March 19, 2013 17:15
-
-
Save unstoppablecarl/5198063 to your computer and use it in GitHub Desktop.
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 | |
namespace Drive\Model\Page; | |
class FormHelper{ | |
/** | |
* | |
* @var type \Drive\Model\Page | |
*/ | |
protected $page; | |
/** | |
* array output of query with field objects | |
* @var type | |
*/ | |
protected $propertyData; | |
public function __construct($page, $useDefaultValues = false){ | |
$this->page = $page; | |
$this->getPropertyData(false, $useDefaultValues); | |
} | |
public function getProperties($forceRefresh = false, $setValueToDefaultValue = false){ | |
if($forceRefresh || empty($this->propertyData)){ | |
$db = \fORMDatabase::retrieve(); | |
$sql = " | |
SELECT | |
page_properties.id as page_property_id, | |
page_properties.name, | |
page_properties.is_core, | |
page_properties.display_name, | |
page_properties.type, | |
page_properties.description, | |
page_properties.apply_all, | |
page_properties.tool_tip, | |
-- page_type_properties.id, | |
-- page_type_properties.page_type_id, | |
-- page_type_properties.page_property_id, | |
page_type_properties.required, | |
page_type_properties.display_order, | |
page_type_properties.default_value, | |
page_type_properties.page_property_group_id, | |
page_type_properties.config_data, | |
page_property_values.value | |
FROM | |
pages | |
INNER JOIN | |
page_types | |
ON | |
pages.page_type_id = page_types.id | |
INNER JOIN | |
page_type_properties | |
ON | |
page_types.id = page_type_properties.page_type_id | |
INNER JOIN | |
page_properties | |
ON | |
page_type_properties.page_property_id = page_properties.id | |
INNER JOIN | |
page_property_values | |
ON | |
page_properties.name = page_property_values.page_property_name | |
AND | |
pages.id = page_property_values.page_id | |
WHERE | |
pages.id = %i | |
"; | |
$query = $db->query($sql, $this->page->getId()); | |
$rows = $query->fetchAllRows(); | |
foreach ($rows as $index => $row) { | |
if($setValueToDefaultValue){ | |
$row['value'] = $row['default_vale']; | |
} | |
$field = \Drive\Field::buildFromArray($row); | |
$rows[$index]['field'] = $field; | |
} | |
$this->propertyData = $rows; | |
} | |
return $this->propertyData; | |
} | |
public function populateValues(){ | |
$props = $this->getProperties(); | |
foreach($props as $index => $prop){ | |
$props[$index]['field']->populateValue(); | |
} | |
} | |
public function validateValues($returnMessages = false){ | |
$props = $this->getProperties(); | |
$validationMessagesList = array(); | |
foreach($props as $prop){ | |
$displayName = $prop['display_name']; | |
$field = $prop['field']; | |
$messages = $field->validateValue(); | |
if(!empty($messages)){ | |
if(is_array($messages)){ | |
foreach($messages as $index => $message){ | |
$messages[$index] = $displayName . ' : ' . $message; | |
} | |
} else { | |
$messages = array($displayName . ' : ' .$messages); | |
} | |
$validationMessagesList = array_merge($validationMessagesList, $messages); | |
} | |
} | |
if(!empty($validationMessagesList)){ | |
if(!$returnMessages){ | |
throw new \fValidationException('The following problems were found', $validationMessagesList); | |
} else { | |
return $validationMessages; | |
} | |
} | |
} | |
public function storeValues(){ | |
$pagePropertyValueTable = new \Drive\Model\pagePropertyValueTable; | |
$props = $this->getProperties(); | |
$db = \fORMDatabase::retrieve(); | |
$db->execute('BEGIN'); | |
foreach($props as $prop){ | |
$displayName = $prop['display_name']; | |
$field = $prop['field']; | |
$data = array( | |
'page_id' => $this->page->getId(), | |
'page_path' => $this->page->getPath(), | |
'page_type_id' => $this->page->getPageTypeId(), | |
'page_type_name' => $this->page->createPageType()->getName(), | |
'page_property_id' => $prop['page_property_id'], | |
'page_property_name' => $prop['name'], | |
'page_property_type' => $prop['type'], | |
'page_property_apply_all' => $field->info['page_property_apply_all'], | |
'page_property_value' => $field->prepareValueForStorage(), | |
'flourish_escape_string' => $field->getFlourishEscapeString(), | |
); | |
$pagePropertyValueTable->storeValueRaw($data); | |
} | |
// Commit changes made during this transaction | |
$db->execute('COMMIT'); | |
} | |
/** | |
* caches and retrieves recordset of all page property groups | |
* @return \Drive\Model\PagePropertyGroupRecordSet | |
*/ | |
public function getPagePropertyGroups(){ | |
$cacheId = 'page_property_groups'; | |
$pageTypePropertyTable = new \Drive\Model\PageTypePropertyTable; | |
if(($pagePropertyGroups = \Drive\Cache::load($cacheId)) === false){ | |
\Drive::log('PPG cache miss'); | |
$pagePropertyGroupRecordSet = $pageTypePropertyTable->fetch(); | |
\Drive\Cache::save($pagePropertyGroupRecordSet, $cacheId, array('page_property_groups')); | |
} else{ | |
\Drive::log('PPG cache HIT'); | |
} | |
return $pagePropertyGroupRecordSet; | |
} | |
/** | |
* generates the final array needed by the template to render the page type update form | |
* @param \Drive\Model\PagePropertyValueRecordSet $propertyValueRecordSet array of property_name => value pairs | |
* @return type | |
*/ | |
public function assembleForAdminRendering(){ | |
$pageProeprtyGroups = $this->getPagePropertyGroups(); | |
$pagePropertyGroups = $pagePropertyGroups->toIndexedArray(); | |
$props = $this->getFields(); | |
if(!empty($propertyValueRecordSet)){ | |
$propertyValues = $propertyValueRecordSet->toValueArray(); | |
} else{ | |
$propertyValues = array(); | |
} | |
// filter unauthorized users | |
foreach($pagePropertyGroups as $index => $group){ | |
if(!\Drive\Authorization::userHasAccessTo('page_property_group', $group['name'], 'view')){ | |
unset($pagePropertyGroups[$index]); | |
} else{ | |
$pagePropertyGroups[$index]['properties'] = array(); | |
$pagePropertyGroups[$index]['has_validation_messages'] = false; | |
} | |
} | |
foreach($props as $prop){ | |
$propertyName = $prop['name']; | |
$pagePropertyGroupId = $prop['page_property_group_id']; | |
// if user does not have access group will not be in array | |
if(isset($pagePropertyGroups[$pagePropertyGroupId])){ | |
$pagePropertyGroups[$pagePropertyGroupId]['properties'][$propertyName] = $prop; | |
if($prop['field']->hasValidationMessages()){ | |
$pagePropertyGroups[$pagePropertyGroupId]['has_validation_messages'] = true; | |
} | |
} | |
} | |
// remove empty groups and sort properties within groups | |
foreach($pagePropertyGroups as $index => $group){ | |
if(empty($group['properties']) && !$group['is_default']){ | |
unset($pagePropertyGroups[$index]); | |
} else { | |
usort($pagePropertyGroups[$index]['properties'], function($a, $b){ | |
if($a['display_order'] == $b['display_order']){ | |
return 0; | |
} | |
return ($a['display_order'] < $b['display_order']) ? -1 : 1; | |
} | |
); | |
} | |
} | |
return $pagePropertyGroups; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment