Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
paypal_order.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
31 class Paypal_Order extends Asset
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  $this->_ser_attrs = TRUE;
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
59  protected function _createAdditional(Array &$link)
60  {
61  if (!parent::_createAdditional($link)) return FALSE;
62 
63  return $this->makeAndSaveInitialWebPath('paypal_order_'.$this->id, $link);
64 
65  }//end _createAdditional()
66 
67 
82  public function getAvailableKeywords()
83  {
84  $keywords = parent::getAvailableKeywords();
85 
86  $keywords['asset_attribute_summary'] = 'The summary of the Paypal order';
87 
88  return $keywords;
89 
90  }// end getAvailableKeywords()
91 
92 
102  public function getKeywordReplacement($keyword)
103  {
104  if ($keyword == 'asset_attribute_summary') {
105  return $this->getProductSummary();
106  }
107 
108  return parent::getKeywordReplacement($keyword);
109 
110  }//end getKeywordReplacement()
111 
112 
119  public function getProductSummary()
120  {
121  $products = $this->attr('products');
122  $products_string = Array();
123 
124  foreach ($products as $product) {
125  $product_string = Array();
126  $product_number = ($product['number'] != '') ? ' (#'.$product['number'].')' : '';
127  $product_string['name'] = $product['name'].$product_number;
128  $product_string['quantity'] = sprintf('%d', $product['quantity']);
129  $product_string['price'] = sprintf('%01.2f', $product['price']);
130  $product_string['subtotal'] = sprintf('%01.2f', $product['subtotal']);
131  $products_string[] = $product_string;
132  }
133 
134  $name_padding_len = $this->_getPaddingLength($products_string, 'name');
135  $quantity_padding_len = $this->_getPaddingLength($products_string, 'quantity');
136  $price_padding_len = $this->_getPaddingLength($products_string, 'price');
137  $subtotal_padding_len = $this->_getPaddingLength($products_string, 'subtotal');
138 
139  $format = "%-{$name_padding_len}s%-{$quantity_padding_len}s%-{$price_padding_len}s%-{$subtotal_padding_len}s\n";
140  $split_string = str_repeat('-', $name_padding_len + $quantity_padding_len + $price_padding_len + $subtotal_padding_len)."\n";
141 
142  //print header
143  $summary = sprintf($format, 'Name', 'Quantity', 'Price', 'Subtotal');
144  $summary .= $split_string;
145 
146  //print products' details
147  foreach ($products_string as $product_string) {
148  $summary .= sprintf($format, $product_string['name'], $product_string['quantity'], $product_string['price'], $product_string['subtotal']);
149  }
150 
151  //print total price
152  $summary .= $split_string;
153  $total = sprintf('%01.2f', $this->attr('total_amount')).' '.$this->attr('currency');
154  $summary .= sprintf($format, 'Total', '', '', $total);
155 
156  return '<pre>'.htmlspecialchars($summary).'</pre>';
157 
158  }//end getProductSummary()
159 
160 
169  private function _getPaddingLength($products_string, $index)
170  {
171  $max_length = strlen($index);
172  foreach ($products_string as $product_string) {
173  if (strlen($product_string[$index]) > $max_length) {
174  $max_length = strlen($product_string[$index]);
175  }
176  }
177 
178  return $max_length + 2; //add 2 more padding characters for better view
179 
180  }//end _getPaddingLength()
181 
182 }//end class
183 ?>