Created
July 10, 2012 13:58
-
-
Save DavidOliver/3083394 to your computer and use it in GitHub Desktop.
Symphony CMS event to save a customer order
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 | |
require_once(TOOLKIT . '/class.event.php'); | |
Class eventsave_order extends Event{ | |
const ROOTELEMENT = 'save-order'; | |
public $eParamFILTERS = array( | |
'etm-new-order-notification' | |
); | |
public static function about(){ | |
return array( | |
'name' => 'Save Order', | |
'author' => array( | |
'name' => 'David Oliver', | |
'website' => 'http://doliver.co.uk', | |
'email' => ''), | |
'version' => 'Symphony 2.2.5', | |
'release-date' => '2012-07-06T19:01:53+00:00', | |
'trigger-condition' => 'action[save-order]' | |
); | |
} | |
public static function getSource(){ | |
return '45'; | |
} | |
public static function allowEditorToParse(){ | |
return false; | |
} | |
public static function documentation(){ | |
return '<p>Saves a customer order.</p>'; | |
} | |
public function load(){ | |
if(isset($_POST['action']['save-order'])) return $this->__trigger(); | |
} | |
protected function __trigger(){ | |
require_once(EXTENSIONS . '/symql/lib/class.symql.php'); | |
function in_array_r($needle, $haystack, $strict = true) { | |
foreach ($haystack as $item) { | |
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { | |
return true; | |
} | |
} | |
return false; | |
} | |
// Grab Instances | |
$instances_query = new SymQLQuery(); | |
$instances_query | |
->select('code, name, description') | |
->from('instances') | |
->perPage(9999); | |
foreach ($_SESSION['sym-cart'] as $key => $value) { | |
$instances_query->where('system:id', $key); | |
} | |
$instances = SymQL::run($instances_query); | |
$instances_xml = new DOMDocument(); | |
$instances_xml->loadXML($instances->generate()); | |
$instances_xpath = new DOMXPath($instances_xml); | |
//echo "<h2>Instances</h2><pre>";print_r($instances);echo "</pre>";die; | |
// Grab Products | |
/*$keys = array(); | |
foreach ($_SESSION['sym-cart'] as $key => $value) { | |
$keys[] = $key; | |
} | |
echo '<pre>'.implode(', ', $keys).'</pre>';*/ | |
/*$products_query = new SymQLQuery(); | |
$products_query | |
->select('name, brand, instances') | |
->from('products') | |
//->where('instances', implode(', ', $keys)) | |
->perPage(999); | |
$products = SymQL::run($products_query);*/ | |
$keys = array(); | |
foreach ($_SESSION['sym-cart'] as $key => $value) { | |
$keys[] = $key; | |
} | |
$product_em = new EntryManager(); | |
$product_fields = array ('name','brand','instances'); | |
$products = $product_em->fetch( | |
false, | |
1, | |
false, | |
false, | |
"AND sym_entries_data_137.relation_id IN (" . implode(', ', $keys) . ") AND sym_entries_data_137.entry_id = e.id", | |
"JOIN sym_entries_data_137", | |
false, | |
true, | |
$product_fields, | |
false, | |
true | |
); | |
//echo "<h2>Products</h2><pre>";print_r($products);echo "</pre>";die; | |
// Grab Brands | |
$brands_query = new SymQLQuery(); | |
$brands_query | |
->select('*') | |
->from('brands') | |
->perPage(9999); | |
$brands = SymQL::run($brands_query); | |
$brands_xml = new DOMDocument(); | |
$brands_xml->loadXML($brands->generate()); | |
$brands_xpath = new DOMXPath($brands_xml); | |
// Map data to $_POST | |
$_POST['fields']['member'] = $_SESSION['sym-members']['id']; | |
foreach($_SESSION['sym-cart'] as $instance_id => $instance) { | |
unset($instance_code,$instance_name,$product_name,$product_brand,$product_brand_id); | |
$instance_code = $instances_xpath->evaluate("string(/symql/entry[@id='$instance_id']/code)"); | |
$instance_name = $instances_xpath->evaluate("string(/symql/entry[@id='$instance_id']/name)"); | |
foreach($products as $product_entry) { | |
$product_entry_data = $product_entry->getData(); | |
if (in_array_r($instance_id, $product_entry_data, false)) { | |
$product_name = $product_entry_data['1']['value']; | |
$product_brand_id = $product_entry_data['9']['relation_id']; | |
$product_brand = $brands_xpath->evaluate("string(/symql/entry[@id='$product_brand_id']/name)"); | |
} | |
} | |
$_POST['fields']['items']['instance-id'][] = $instance_id; | |
$_POST['fields']['items']['quantity'][] = $instance['num']; | |
$_POST['fields']['items']['item'][] = "$instance_code - $product_brand $product_name $instance_name"; | |
$_POST['fields']['items']['price'][] = number_format($instance['sum'] / $instance['num'], 2, '.', ''); | |
$_POST['fields']['items']['line-total'][] = number_format($instance['sum'], 2, '.', ''); | |
$total += $instance['sum']; | |
} | |
$_POST['fields']['total'] = number_format($total, 2, '.', ''); | |
//echo '<h2>$_POST</h2><pre>';print_r($_POST);echo '</pre>';die; | |
include(TOOLKIT . '/events/event.section.php'); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment