/** * Init with current account settings. */ private function loadAccount() { // prepopulate with current account $account = $this->container->get('security.context')->getToken()->getUser(); // move into Beans to wrap unsets of propertynames, attachedM, etc. $map = Beans::obj2map($account); // TODO: all this should be in a base class (but not ZMModel) - perhaps FormData in rp? // also, it should be possible/required to specify the fields that should be merged, plus // table names for custom fields (how could we find that out automatically??) unset($map['propertyNames']); unset($map['password']); unset($map['attachedMethods']); Beans::setAll($this, $map); }
/** * Validate the given request/object using the named (id) rule set. * * <p>If the request parameter is an object, it will be added to the * internally used data map using the <em>magic key</em> <code>__obj</code>.</p> * * @param ZenMagick\Http\Request request The current request. * @param mixed data The data (map or object) to validate. * @param string id The ruleset id. * @return boolean <code>true</code> if the validation was successful, <code>false</code> if not. */ public function validate($request, $data, $id) { $this->messages = array(); $set = $this->getRuleSet($id, true); if (null == $set) { return true; } if (is_object($data)) { $map = Beans::obj2map($data); $map['__obj'] = $data; } else { $map = $data; } // initial status $status = true; // iterate over rules foreach ($set->getRules() as $rule) { if (!$rule->validate($request, $map)) { $status = false; $msgList = array(); if (array_key_exists($rule->getName(), $this->messages)) { $msgList = $this->messages[$rule->getName()]; } if (null != $rule->getErrorMsg()) { array_push($msgList, $rule->getErrorMsg()); } $this->messages[$rule->getName()] = $msgList; } } return $status; }
/** * {@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'); }
/** * Test getBean. */ public function testGetBean() { $expect = array('foo' => 'bar', 'doh' => 'nut', 'properties' => array('foo' => 'bar', 'doh' => 'nut'), 'propertyNames' => array('foo', 'doh'), 'attachedMethods' => array()); $definition = 'ZenMagick\\Base\\ZMObject#foo=bar&doh=nut'; $obj = Beans::getBean($definition); $map = Beans::obj2map($obj); $this->assertEquals($expect, $map); $this->assertTrue($obj instanceof ZMObject); // test empty properties $expect = array('properties' => array(), 'propertyNames' => array(), 'attachedMethods' => array()); $definition = 'ZenMagick\\Base\\ZMObject'; $obj = Beans::getBean($definition); $map = Beans::obj2map($obj); $this->assertEquals($expect, $map); $this->assertTrue($obj instanceof ZMObject); }
/** * Create a prepared statement. * * @param string sql The initial SQL. * @param mixed args The data either as map or ZMObject instance. * @param array mapping The field mapping. * @return A <code>PreparedStatement</code> or null; */ protected function prepareStatement($sql, $params, $mapping = null) { $PDO_INDEX_SEP = '__'; // make sure we are working on a map if (is_object($params)) { $params = Beans::obj2map($params, array_keys($mapping)); } // PDO doesn't allow '#' in param names, so use something else $nargs = array(); foreach (array_keys($params) as $name) { $nname = str_replace('#', $PDO_INDEX_SEP, $name); if ($name != $nname) { $sql = str_replace(':' . $name, ':' . $nname, $sql); } $nargs[$nname] = $params[$name]; } $params = $nargs; // handle array args foreach ($params as $name => $value) { if (is_array($value)) { $aargs = array(); $index = 1; foreach ($value as $vv) { $aargs[$index++ . $PDO_INDEX_SEP . $name] = $vv; } // remove original unset($params[$name]); // add new split up values $params = array_merge($params, $aargs); // update SQL $sql = str_replace(':' . $name, ':' . implode(', :', array_keys($aargs)), $sql); } } // create statement $sql = $this->resolveTablePlaceHolders($sql); $stmt = $this->prepare($sql); foreach ($params as $name => $value) { $typeName = preg_replace('/[0-9]+' . $PDO_INDEX_SEP . '/', '', $name); if (false !== strpos($sql, ':' . $name) && array_key_exists($typeName, $mapping)) { // only bind if actually used $type = $mapping[$typeName]['type']; // @todo do we really want to keep self::NULL_DATE* for native ZM code/plugins or keep it at all? if ('datetime' == $type && null == $value) { $value = self::NULL_DATETIME; } if ('date' == $type && null == $value) { $value = self::NULL_DATE; } $dbalType = $this->getDatabasePlatform()->getDoctrineTypeMapping($type); $x = $stmt->bindValue(':' . $name, $value, $dbalType); } } return $stmt; }
/** * Turn an object into a map (array) * * @see Beans::obj2map * @param object obj * @param array properties * @param bool useGeneric * @return array */ public function obj2Map($obj, $properties = null, $addGeneric = true) { return Beans::obj2map($obj, $properties, $addGeneric); }