/** * Validate the given request data. * * @param ZenMagick\Http\Request request The current request. * @param array data The data. * @return boolean <code>true</code> if the value for <code>$name</code> is valid, <code>false</code> if not. */ public function validate($request, $data) { if (!Runtime::getSettings()->get('isAccountState')) { return true; } //todo: this should not be here, but in the corresponding controller classes - BEFORE the validation is done $state = isset($data['state']) ? $data['state'] : null; $zoneId = isset($data['zoneId']) ? $data['zoneId'] : null; $zones = $this->container->get('countryService')->getZonesForCountryId($data['countryId']); $valid = false; if (0 < count($zones)) { // need $state to match either an id or name foreach ($zones as $zone) { if ($zone->getName() == $state || $zone->getId() == $state || $zone->getId() == $zoneId || $zone->getCode() == $state) { $zoneId = $zone->getId(); $state = ''; $valid = true; break; } } } else { if (!empty($state)) { $valid = true; $zoneId = 0; } } // check for form bean if (array_key_exists('__obj', $data)) { $data['__obj'] = Beans::setAll($data['__obj'], array('state' => $state, 'zoneId' => $zoneId)); } return $valid; }
/** * Test setAll with array */ public function testSetAllArray() { // set all $map = array('foo' => 'bar', 'doh' => 'nut'); $expectAll = array('foo' => 'bar', 'doh' => 'nut', 'deng' => 'foo'); $map = Beans::setAll($map, array('deng' => 'foo')); $this->assertEquals($expectAll, $map); }
/** * {@inheritDoc} */ public function processGet($request) { $fromDate = $request->query->get('fromDate'); $toDate = $request->query->get('toDate'); $exportFormat = $request->query->get('exportFormat'); $viewData = array(); if (null != $fromDate) { $dateFormat = $this->getDateFormat(); $orderDateFrom = \DateTime::createFromFormat($dateFormat . ' H:i:s', $fromDate . ' 00:00:00'); if (!empty($toDate)) { $orderDateTo = \DateTime::createFromFormat($dateFormat . ' H:i:s', $toDate . ' 00:00:00'); } else { $orderDateTo = new \DateTime(); $toDate = $orderDateTo->format($dateFormat); } // TODO: use new ZMOrders method $sql = "SELECT o.*, s.orders_status_name, ots.value as shippingValue\n FROM %table.orders% o, %table.orders_total% ot, %table.orders_status% s, %table.orders_total% ots\n WHERE date_purchased >= :1#orderDate AND date_purchased < :2#orderDate\n AND o.orders_id = ot.orders_id\n AND ot.class = 'ot_total'\n AND o.orders_id = ots.orders_id\n AND ots.class = 'ot_shipping'\n AND o.orders_status = s.orders_status_id\n AND s.language_id = :languageId\n ORDER BY orders_id DESC"; $args = array('languageId' => 1, '1#orderDate' => $orderDateFrom, '2#orderDate' => $orderDateTo); // load as array to save memory $results = \ZMRuntime::getDatabase()->fetchAll($sql, $args, array('orders', 'orders_total', 'orders_status')); // prepare data $header = array('Date', 'Order Id', 'Customer Name', 'Shipping Country', 'Products Ordered', 'Quantity', 'Unit Products Net Price', 'Products Line Net Total', 'Products Net Total', 'Shipping Cost', 'Discount Amount', 'Discount Coupon', 'Gift Voucher Amount', 'Payment Type', 'Order Tax', 'Order Total'); $rows = array(); $order = Beans::getBean('ZenMagick\\StoreBundle\\Entity\\Order\\Order'); foreach ($results as $ii => $orderData) { $order->reset(); Beans::setAll($order, $orderData); $rows[] = $this->processOrder($order); } // additional view data $viewData = array('header' => $header, 'rows' => $rows, 'toDate' => $toDate); if ('csv' == $exportFormat) { // @todo should use StreamedResponse $response = new Response(); $d = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'orders.csv'); $response->headers->set('Content-Type', 'application/csv'); $response->headers->set('Content-Disposition', $d); ob_start(); $fp = fopen('php://output', 'w'); fputcsv($fp, $header); foreach ($rows as $orderRows) { foreach ($orderRows as $row) { fputcsv($fp, $row); } } fclose($fp); $response->setContent(ob_get_clean()); return $response; } } return $this->findView(null, $viewData); }
/** * Get a populated <code>ZenMagick\StoreBundle\Entity\Account</code> instance. * * @return ZenMagick\StoreBundle\Entity\Account An account. */ public function getAccount() { $account = Beans::getBean('ZenMagick\\StoreBundle\\Entity\\Account'); $properties = $this->getProperties(); // TODO: see comment in c'tor // don't need these foreach (array('formId', 'action') as $name) { unset($properties[$name]); } // special treatment $properties['dob'] = \DateTime::createFromFormat($this->container->get('localeService')->getFormat('date', 'short'), $properties['dob']); $account = Beans::setAll($account, $properties); return $account; }
/** * Get a populated <code>ZenMagick\StoreBundle\Entity\Address</code> instance. * * @return ZenMagick\StoreBundle\Entity\Address An address. */ public function getAddress() { $address = Beans::getBean('ZenMagick\\StoreBundle\\Entity\\Address'); $properties = $this->getProperties(); // don't need these foreach (array('formId', 'action', 'password', 'confirmation') as $name) { unset($properties[$name]); } // special treatment if (empty($properties['countryId'])) { $properties['countryId'] = 0; } $address = Beans::setAll($address, $properties); return $address; }
/** * Prepare widgets for editing. * * @param array options The widget options. * @return array Widget map. * @todo make more generic? */ protected function widgets(array $options) { $widgets = array(); foreach ($options['properties'] as $name => $property) { $type = isset($property['type']) ? $property['type'] : 'text'; // @todo: allow class, id as-is $id = sprintf('%sFormWidget', $type); if ($this->container->has($id)) { $widget = $this->container->get($id); Beans::setAll($widget, $property); if (isset($property['config'])) { Beans::setAll($widget, $property['config']); } $widget->setName($name); $widgets[] = $widget; } } return $widgets; }
/** * Set page property. */ public function setEZPageProperty($rpcRequest) { $data = $rpcRequest->getData(); $pageId = $data->pageId; $languageId = $data->languageId; $property = $data->property; $value = $data->value; if (in_array($property, array('NewWin', 'SSL', 'header', 'sidebox', 'footer', 'toc'))) { $value = Toolbox::asBoolean($value); } $rpcResponse = $rpcRequest->createResponse(); $ezPageService = $this->container->get('ezPageService'); if (null != ($ezPage = $ezPageService->getPageForId($pageId, $languageId))) { Beans::setAll($ezPage, array($property => $value)); $ezPageService->updatePage($ezPage); $rpcResponse->setStatus(true); } else { $rpcResponse->setStatus(false); } return $rpcResponse; }
/** * Get all blocks for the given block group id. * * @param ZenMagick\Http\Request request The current request. * @param string groupId The block group id. * @param array args Optional parameter; default is an empty array. * @return array List of <code>BlockWidget</code> instances. */ public function getBlocksForId($request, $groupId, $args = array()) { if (array_key_exists($groupId, $this->mappings)) { // ensure bean definitions are resolved first... $group = array(); foreach ($this->mappings[$groupId] as $block) { $widget = null; if (is_string($block)) { $widget = Beans::getBean($block, $this->container); } elseif (is_object($block) && $block instanceof BlockWidget) { $widget = $block; Beans::setAll($widget, $args); } if (null != $widget) { $group[] = $widget; } } $this->mappings[$groupId] = $group; return $this->mappings[$groupId]; } return array(); }
/** * Render a widget. * * @param mixed widget Either a <code>Widget</code> instance or a widget bean definition. * @param string name Optional name; default is <code>null</code> for none. * @param string value Optional value; default is <code>null</code> for none. * @param mixed args Optional parameter; a map of widget properties; default is <code>null</code>. * @return string The widget contents. */ public function widget($widget, $name = null, $value = null, $args = null) { $wObj = $widget; if (is_string($widget)) { $wObj = Beans::getBean($widget); } if (!$wObj instanceof Widget) { Runtime::getLogging()->debug('invalid widget: ' . $widget); return ''; } if (null !== $name) { $wObj->setName($name); if (null === $args || !array_key_exists('id', $args)) { // no id set, so default to name $wObj->setId($name); } } if (null !== $value) { $wObj->setValue($value); } if (null !== $args) { Beans::setAll($wObj, $args); } return $wObj->render($this->container->get('request'), $this->container->get('defaultView')); }
/** * {@inheritDoc} */ public function processPost($request) { $ezPageService = $this->container->get('ezPageService'); $languageId = $request->request->get('languageId'); $viewId = null; if (null !== ($ezPageId = $request->request->get('updateId'))) { if (0 == $ezPageId) { // create $ezPage = Beans::getBean('ZenMagick\\StoreBundle\\Entity\\EZPage'); Beans::setAll($ezPage, $request->request->all()); $ezPage->setStatic(true); $ezPage = $ezPageService->createPage($ezPage); if (0 < $ezPage->getId()) { $this->get('session.flash_bag')->success('Page #' . $ezPage->getId() . ' saved'); $viewId = 'success'; } else { $this->get('session.flash_bag')->error('Could not save page'); } } elseif (null != ($ezPage = $ezPageService->getPageForId($ezPageId, $languageId))) { // no sanitize! Beans::setAll($ezPage, $request->request->all()); $ezPage->setStatic(true); $ezPageService->updatePage($ezPage); $this->get('session.flash_bag')->success('Page #' . $ezPageId . ' updated'); $viewId = 'success'; } else { $this->get('session.flash_bag')->error('Could not save page - invalid request data'); } } elseif (null !== ($ezPageId = $request->request->get('deleteId'))) { $ezPageId = (int) $ezPageId; if (null != ($ezPage = $ezPageService->getPageForId($ezPageId, $languageId))) { $ezPageService->removePage($ezPage); $this->get('session.flash_bag')->success('Page #' . $ezPage->getId() . ' deleted'); $viewId = 'success'; } else { $this->get('session.flash_bag')->error('Could not find Page to delete: #' . $ezPageId); } } return $this->findView($viewId); }
/** * Get the form data object (if any) for this request. * * @param ZenMagick\Http\Request request The request to process. * @param string formDef Optional form container definition; default is <code>null</code> to use the global mapping. * @param string formId Optional form id; default is <code>null</code> to use the global mapping. * @return ZMObject An object instance or <code>null</code> */ public function getFormData($request, $formDef = null, $formId = null) { $router = $this->get('router'); if (null != ($routeData = $router->match($request->getPathInfo()))) { $route = $router->getRouteCollection()->get($routeData['_route']); if (null != ($options = $route->getOptions()) && array_key_exists('form', $options)) { $this->formData = Beans::getBean($options['form']); if ($this->formData instanceof Form) { $this->formData->populate($request); } else { $this->formData = Beans::setAll($this->formData, $request->getParameterMap()); } } } return $this->formData; }
/** * Restore persisted services. */ public function restorePersistedServices() { // restore persisted services foreach ((array) $this->get(self::AUTO_SAVE_KEY) as $id => $serdat) { $obj = unserialize($serdat['ser']); $isService = !isset($serdat['type']) || 'service' == $serdat['type']; if ($isService) { $service = $this->container->get($id); Beans::setAll($service, $obj->getSerializableProperties()); $obj = $service; } foreach ($serdat['restore'] as $rid) { if ($this->container->has($rid)) { $rid = trim($rid); $method = 'set' . ucwords($rid); $obj->{$method}($this->container->get($rid)); } } if (!$isService) { // preserve definition $definition = $this->container->getDefinition($id); $this->container->set($id, $obj); $this->container->setDefinition($id, $definition); } } }
/** * {@inheritDoc} */ public function processPost($request) { $languageId = $request->getSelectedLanguage()->getId(); $data = $this->getViewData($request); $fieldList = $data['fieldList']; $fieldMap = $data['fieldMap']; $productIdList = $this->container->get('productService')->getProductIdsForCategoryId($request->attributes->get('categoryId'), $languageId, false, false); foreach ($productIdList as $productId) { // build a data map for each submitted product $formData = array(); // and one with the original value to compare and detect state data $_formData = array(); foreach ($fieldList as $field) { $widget = $field['widget']; if ($widget instanceof FormWidget) { $fieldName = $field['name'] . '_' . $productId; // use widget to *read* the value to allow for optional conversions, etc $widget->setValue($request->request->get($fieldName, null, false)); $formData[$fieldMap[$field['name']]] = $widget->getStringValue(); $widget->setValue($request->request->get(self::STALE_CHECK_FIELD_PREFIX . $fieldName, null, false)); $_formData[$fieldMap[$field['name']]] = $widget->getStringValue(); } } // load product, convert to map and compare with the submitted form data $product = $this->container->get('productService')->getProductForId($productId, $languageId); $productData = Beans::obj2map($product, $fieldMap); $isUpdate = false; foreach ($formData as $key => $value) { if (array_key_exists($key, $productData) && $value != $productData[$key]) { if ($_formData[$key] == $productData[$key]) { $isUpdate = true; } else { $isUpdate = false; $this->get('session.flash_bag')->warn('Found stale data (' . $key . ') for productId ' . $productId . ' - skipping update'); } break; } } if ($isUpdate) { $product = Beans::setAll($product, $formData); $this->container->get('productService')->updateProduct($product); $this->get('session.flash_bag')->success('All changes saved'); } } return $this->findView('catalog-redirect'); }
/** * Execute a query. * * <p>If <code>$resultClass</code> is <code>null</code>, the returned * list will contain a map of <em>columns</em> => <em>value</em> for each selected row.</p> * * <p><code>$modelClass</code> may be set to the magic value of <code>Connection::MODEL_RAW</code> to force * returning the raw data without applying any mappings or conversions.</p> * * @param string sql The query. * @param array params Optional query parameters; default is an empty array. * @param mixed mapping The field mappings or table name (list); default is <code>null</code>. * @param string modelClass The class name to be used to build result obects; default is <code>null</code>. * @return array List of populated objects of class <code>$resultClass</code> or map if <em>modelClass</em> is <code>null</code>. */ public function fetchAll($sql, array $params = array(), $mapping = null, $modelClass = null) { $mapping = $this->getMapper()->ensureMapping($mapping); $stmt = $this->prepareStatement($sql, $params, $mapping); $stmt->execute(); $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); $stmt->closeCursor(); if (self::MODEL_RAW == $modelClass) { return $rows; } $results = array(); foreach ($rows as $result) { if (null !== $mapping) { $result = $this->translateRow($result, $mapping); } if (null != $modelClass) { if (null != ($obj = new $modelClass())) { if ($obj instanceof \Symfony\Component\DependencyInjection\ContainerAwareInterface) { $obj->setContainer(Runtime::getContainer()); } } $result = Beans::setAll($obj, $result); } $results[] = $result; } return $results; }
/** * Unserialize. * * @param string serialized The serialized data. */ public function unserialize($serialized) { $this->__construct(); $this->container = Runtime::getContainer(); if (function_exists('gzuncompress')) { $serialized = gzuncompress(base64_decode($serialized)); } $sprops = unserialize($serialized); foreach ($sprops as $name => $sprop) { $sprops[$name] = unserialize($sprop); } Beans::setAll($this, $sprops); }
/** * {@inheritDoc} */ public function populate(Request $request) { Beans::setAll($this, $request->getParameterMap(), null); }