public function result() { $result = parent::result(); $raw = (array) json_decode($result); $data = array(); foreach ($raw as $key => $row) { unset($row->_id); unset($row->_csfr); $data[$key] = (array) $row; } return yaml::encode($data); }
/** * After a successful transaction, update product stock * Expects an array * $items = [ * [uri, variant, quantity] * ] */ function updateStock($items) { foreach ($items as $i => $item) { $product = page($item['uri']); $variants = $product->variants()->yaml(); foreach ($variants as $key => $variant) { if (str::slug($variant['name']) === $item['variant']) { // Edit stock in the original $variants array. If $newStock is false/blank, that means it's unlimited. $newStock = $variant['stock'] - $item['quantity']; $variants[$key]['stock'] = $newStock ? $newStock : ''; // Update the entire variants field (only one variant has changed) $product->update(array('variants' => yaml::encode($variants))); } } } }
// loop through all fields and add them to the content foreach ($fields as $field) { $pos = strpos($field, ':'); $key = str_replace(array('-', ' '), '_', strtolower(trim(substr($field, 0, $pos)))); // Don't add fields with empty keys if (empty($key)) { continue; } $data[$key] = trim(substr($field, $pos + 1)); } return $data; }); /** * PHP serializer adapter */ data::$adapters['php'] = array('extension' => array('php'), 'encode' => function ($array) { return '<?php ' . PHP_EOL . PHP_EOL . 'return ' . var_export($array, true) . PHP_EOL . PHP_EOL . '?>'; }, 'decode' => function ($string) { throw new Error('Decoding PHP strings is not supported'); }, 'read' => function ($file) { $array = (require $file); return $array; }); /** * YAML adapter */ data::$adapters['yaml'] = array('extension' => array('yaml', 'yml'), 'encode' => function ($data) { return yaml::encode($data); }, 'decode' => function ($string) { return yaml::decode($string); });
/** * Setter and getter for the file content * * @param string $content * @return string */ public function content($content = null, $format = null) { if (!is_null($content)) { if (is_array($content)) { switch ($format) { case 'json': $content = json_encode($content); break; case 'yaml': $content = yaml::encode($content); break; default: $content = serialize($content); break; } } else { if (is_object($content)) { $content = serialize($content); } } return $this->content = $content; } if (is_null($this->content)) { $this->content = file_get_contents($this->root); } return $this->content; }
protected function updateMethodPage($method, $methodPage) { try { $methodPage->update(array('title' => $method['name'], 'excerpt' => $method['excerpt'], 'call' => $method['call'], 'return' => yaml::encode($method['return']), 'params' => yaml::encode($method['params']))); var_dump($methodPage->uri() . ' has been updated'); } catch (Exception $e) { var_dump($e->getMessage()); } }
try { // Make sure we're on a product page if ($page->template() !== 'product') { return true; } // Numeric stock, price and sale price $variants = $page->variants()->yaml(); foreach ($variants as $key => $variant) { if (!is_numeric($variant['price'])) { $variants[$key]['price'] = preg_replace('/[^0-9.]/', '', $variant['price']); } if (!is_numeric($variant['sale_price'])) { $variants[$key]['sale_price'] = preg_replace('/[^0-9.]/', '', $variant['sale_price']); } if (!is_numeric($variant['stock'])) { $variants[$key]['stock'] = preg_replace('/[^0-9.]/', '', $variant['stock']); } } // Numeric tax rates $taxes = $page->tax()->yaml(); foreach ($taxes as $key => $tax) { if (!is_numeric($tax['rate'])) { $taxes[$key]['rate'] = preg_replace('/[^0-9.]/', '', $tax['rate']); } } // Save changes $page->update(['variants' => yaml::encode($variants), 'tax' => yaml::encode($taxes)]); } catch (Exception $e) { return response::error($e->getMessage()); } });
/** * Save the order to persistent storage * * @return mixed Order ID if successfully saved, otherwise false */ public function store($data) { $id = time() . dechex(intval(time())); $guests = []; foreach ($this->guests() as $guest) { $guests[] = ['name' => $guest, 'checkedIn' => false]; } $order = ['title' => $id, 'customerName' => $data['name'], 'customerPhone' => $data['phone'], 'customerEmail' => $data['email'], 'comments' => $data['comments'], 'date' => date('Y-m-d'), 'time' => date('g:ia'), 'total' => $this->total(), 'guests' => yaml::encode($guests)]; try { $orderPage = $this->event->children()->find('orders')->children()->create($id, 'order', $order); } catch (Exception $e) { die($e->getMessage()); } return $orderPage; }
public function result() { $result = parent::result(); return yaml::encode($result); }
$urls[] = $param; s::set('stats', $urls); } require_once __DIR__ . DS . '../widgets/stats/helpers.php'; $stats = getPage(); // Get data $data = $stats->pages()->yaml(); $dates = $stats->dates()->yaml(); $date = date($date_format); if ($data == null) { $data = array(); } if ($dates == null) { $dates = array(); } // calculate new values $val = array_key_exists($param, $data) ? (int) $data[$param]['count'] + 1 : 1; $today = array_key_exists($date, $dates) ? (int) $dates[$date]['count'] + 1 : 1; $total = !$stats->total_stats_count()->isEmpty() ? $stats->total_stats_count()->int() + 1 : 1; // update arrays $data[$param] = array('count' => $val); $dates[$date] = array('count' => $today); // keep only the last $days days $dates = array_slice($dates, $days * -1, $days, true); try { $stats->update(array('pages' => yaml::encode($data), 'dates' => yaml::encode($dates), 'total_stats_count' => $total)); } catch (Exception $e) { echo $e->getMessage(); exit; } //}
public function toYaml() { return trim(yaml::encode($this->toArray())); }
/** * After a successful transaction, update product stock * * $txn page object */ function updateStock($txn) { foreach ($txn->products()->toStructure() as $i => $item) { $product = page($item->uri()); $variants = $product->variants()->yaml(); foreach ($variants as $key => $variant) { if (str::slug($variant['name']) === $item->variant()->value) { if ($variant['stock'] === '') { // Unlimited stock $variants[$key]['stock'] = ''; } else { // Limited stock $variants[$key]['stock'] = intval($variant['stock']) - intval($item->quantity()->value); } // Update the entire variants field (only one variant has changed) $product->update(array('variants' => yaml::encode($variants))); } } } }
$item->downloads = ['files' => $files, 'expires' => $variant->download_days()->isEmpty() ? NULL : $timestamp + $variant->download_days()->value * 60 * 60 * 24]; } } } $items[] = (array) $item; } // Unique transaction ID $txn_id = get('gateway') . '-' . $timestamp . '-' . bin2hex(openssl_random_pseudo_bytes(2)); try { // Create the pending order file page('shop/orders')->children()->create($txn_id, 'order', ['txn-id' => $txn_id, 'txn-date' => $timestamp, 'txn-currency' => page('shop')->currency_code(), 'status' => $status, 'products' => "\n" . yaml::encode($items), 'subtotal' => number_format($cart->getAmount(), 2, '.', ''), 'discount' => number_format($discount['amount'], 2, '.', ''), 'shipping' => $shipping['rate'], 'shipping-method' => $shipping['title'], 'tax' => number_format($cart->getTax(), 2, '.', ''), 'giftcertificate' => null !== get('giftCertificateAmount') ? number_format(get('giftCertificateAmount'), 2, '.', '') : '0.00']); // Add payer info if it's available at this point if ($user = site()->user()) { page('shop/orders/' . $txn_id)->update(['payer-id' => $user->username(), 'payer-name' => $user->firstname() . ' ' . $user->lastname(), 'payer-email' => $user->email(), 'payer-address' => page('shop/countries/' . $user->country())->title()], site()->defaultLanguage()->code()); } // Update the giftcard balance if ($giftCertificateRemaining = get('giftCertificateRemaining')) { $certificates = page('shop')->gift_certificates()->yaml(); foreach ($certificates as $key => $certificate) { if (strtoupper($certificate['code']) == s::get('giftCertificateCode')) { $certificates[$key]['amount'] = number_format($giftCertificateRemaining, 2, '.', ''); } } page('shop')->update(['gift-certificates' => yaml::encode($certificates)]); } } catch (Exception $e) { // Order creation failed echo $e->getMessage(); } // Redirect to self with GET, passing along the order ID go('shop/cart/process/' . get('gateway') . '/' . $txn_id);