|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Oro Inc. |
| 4 | + * |
| 5 | + * NOTICE OF LICENSE |
| 6 | + * |
| 7 | + * This source file is subject to the Open Software License (OSL 3.0) |
| 8 | + * that is published at http://opensource.org/licenses/osl-3.0.php. |
| 9 | + * If you did not receive a copy of the license and are unable to |
| 10 | + * obtain it through the world-wide-web, please send an email |
| 11 | + * to [email protected] so we can send you a copy immediately |
| 12 | + * |
| 13 | + * @category Oro |
| 14 | + * @package Api |
| 15 | + * @copyright Copyright 2013 Oro Inc. (http://www.orocrm.com) |
| 16 | + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
| 17 | + */ |
| 18 | +class Oro_Api_Helper_Data |
| 19 | + extends Mage_Api_Helper_Data |
| 20 | +{ |
| 21 | + const XPATH_ATTRIBUTES_ENABLED = 'oro/api/enable_attributes'; |
| 22 | + |
| 23 | + /** |
| 24 | + * @return string |
| 25 | + */ |
| 26 | + public function getModuleName() |
| 27 | + { |
| 28 | + return $this->_getModuleName(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Parse filters and format them to be applicable for collection filtration |
| 33 | + * |
| 34 | + * @param null|object|array $filters |
| 35 | + * @param array $fieldsMap Map of field names in format: array('field_name_in_filter' => 'field_name_in_db') |
| 36 | + * @return array |
| 37 | + */ |
| 38 | + public function parseFilters($filters, $fieldsMap = null) |
| 39 | + { |
| 40 | + // if filters are used in SOAP they must be represented in array format to be used for collection filtration |
| 41 | + if (is_object($filters)) { |
| 42 | + $parsedFilters = array(); |
| 43 | + // parse simple filter |
| 44 | + if (isset($filters->filter) && is_array($filters->filter)) { |
| 45 | + foreach ($filters->filter as $field => $value) { |
| 46 | + if (is_object($value) && isset($value->key) && isset($value->value)) { |
| 47 | + $parsedFilters[$value->key] = $value->value; |
| 48 | + } else { |
| 49 | + $parsedFilters[$field] = $value; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + // parse complex filter |
| 54 | + if (isset($filters->complex_filter) && is_array($filters->complex_filter)) { |
| 55 | + $parsedFilters += $this->_parseComplexFilter($filters->complex_filter); |
| 56 | + } |
| 57 | + |
| 58 | + $filters = $parsedFilters; |
| 59 | + } |
| 60 | + // make sure that method result is always array |
| 61 | + if (!is_array($filters)) { |
| 62 | + $filters = array(); |
| 63 | + } |
| 64 | + // apply fields mapping |
| 65 | + if (isset($fieldsMap) && is_array($fieldsMap)) { |
| 66 | + foreach ($filters as $field => $value) { |
| 67 | + if (isset($fieldsMap[$field])) { |
| 68 | + unset($filters[$field]); |
| 69 | + $field = $fieldsMap[$field]; |
| 70 | + $filters[$field] = $value; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + return $filters; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Parses complex filter, which may contain several nodes, e.g. when user want to fetch orders which were updated |
| 79 | + * between two dates. |
| 80 | + * |
| 81 | + * @param array $complexFilter |
| 82 | + * @return array |
| 83 | + */ |
| 84 | + protected function _parseComplexFilter($complexFilter) |
| 85 | + { |
| 86 | + $parsedFilters = array(); |
| 87 | + |
| 88 | + foreach ($complexFilter as $filter) { |
| 89 | + if (!isset($filter->key) || !isset($filter->value)) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + list($fieldName, $condition) = array($filter->key, $filter->value); |
| 94 | + $conditionName = $condition->key; |
| 95 | + $conditionValue = $condition->value; |
| 96 | + $this->formatFilterConditionValue($conditionName, $conditionValue); |
| 97 | + |
| 98 | + if (array_key_exists($fieldName, $parsedFilters)) { |
| 99 | + $parsedFilters[$fieldName] += array($conditionName => $conditionValue); |
| 100 | + } else { |
| 101 | + $parsedFilters[$fieldName] = array($conditionName => $conditionValue); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return $parsedFilters; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Convert condition value from the string into the array |
| 110 | + * for the condition operators that require value to be an array. |
| 111 | + * Condition value is changed by reference |
| 112 | + * |
| 113 | + * @param string $conditionOperator |
| 114 | + * @param string $conditionValue |
| 115 | + */ |
| 116 | + public function formatFilterConditionValue($conditionOperator, &$conditionValue) |
| 117 | + { |
| 118 | + if (is_string($conditionOperator) && in_array($conditionOperator, array('in', 'nin', 'finset')) |
| 119 | + && is_string($conditionValue) |
| 120 | + ) { |
| 121 | + $delimiter = ','; |
| 122 | + $conditionValue = explode($delimiter, $conditionValue); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param Varien_Data_Collection_Db $collection |
| 128 | + * @param \stdClass|null $pager |
| 129 | + * |
| 130 | + * @return boolean |
| 131 | + */ |
| 132 | + public function applyPager($collection, $pager) |
| 133 | + { |
| 134 | + if ($pager->pageSize && $pager->page) { |
| 135 | + $collection->setCurPage($pager->page); |
| 136 | + $collection->setPageSize($pager->pageSize); |
| 137 | + |
| 138 | + if ($collection->getCurPage() != $pager->page) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return true; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @return bool |
| 148 | + */ |
| 149 | + public function isOroRequest() |
| 150 | + { |
| 151 | + return (bool) Mage::registry('is-oro-request'); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Get WSDL/WSI complexType attributes by complex type name. |
| 156 | + * |
| 157 | + * @param string $typeName |
| 158 | + * @return array |
| 159 | + */ |
| 160 | + public function getComplexTypeAttributes($typeName) |
| 161 | + { |
| 162 | + /** @var Mage_Api_Model_Wsdl_Config $wsdlModel */ |
| 163 | + $wsdlModel = Mage::getModel('api/wsdl_config'); |
| 164 | + $wsdlModel->init(); |
| 165 | + |
| 166 | + $elements = array(); |
| 167 | + if ($this->isComplianceWSI()) { |
| 168 | + $elements = $wsdlModel->getXpath( |
| 169 | + 'wsdl:types/xsd:schema/xsd:complexType[@name="' . $typeName . '"]/xsd:sequence/xsd:element' |
| 170 | + ); |
| 171 | + } else { |
| 172 | + $typeDefinition = $wsdlModel->getNode('types/schema/complexType@name="' . $typeName . '"/all'); |
| 173 | + if ($typeDefinition && $typeDefinition->children()->count() > 0) { |
| 174 | + $elements = $typeDefinition->children(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + $exposedAttributes = array(); |
| 179 | + /** @var Mage_Api_Model_Wsdl_Config_Element $definitionNode */ |
| 180 | + foreach ($elements as $definitionNode) { |
| 181 | + $name = (string)$definitionNode->getAttribute('name'); |
| 182 | + $type = (string)$definitionNode->getAttribute('type'); |
| 183 | + $exposedAttributes[$name] = $type; |
| 184 | + } |
| 185 | + |
| 186 | + return $exposedAttributes; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Get WSDL/WSI complexType scalar attributes by complex type name. |
| 191 | + * |
| 192 | + * @param string $typeName |
| 193 | + * @return array |
| 194 | + */ |
| 195 | + public function getComplexTypeScalarAttributes($typeName) |
| 196 | + { |
| 197 | + $scalarTypes = array('xsd:string', 'xsd:int', 'xsd:double', 'xsd:boolean', 'xsd:long'); |
| 198 | + |
| 199 | + $result = array(); |
| 200 | + $attributes = $this->getComplexTypeAttributes($typeName); |
| 201 | + foreach ($attributes as $typeName => $type) { |
| 202 | + if (in_array($type, $scalarTypes, true)) { |
| 203 | + $result[] = $typeName; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + return $result; |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Get entity attributes that are not not present in known attributes list. |
| 212 | + * |
| 213 | + * @param Varien_Object $entity |
| 214 | + * @param array $data |
| 215 | + * @param array $exclude |
| 216 | + * @param array $include |
| 217 | + * @return array |
| 218 | + */ |
| 219 | + public function getNotIncludedAttributes( |
| 220 | + Varien_Object $entity, |
| 221 | + array $data, |
| 222 | + array $exclude = array(), |
| 223 | + array $include = array() |
| 224 | + ) { |
| 225 | + if (!Mage::getStoreConfig(self::XPATH_ATTRIBUTES_ENABLED)) { |
| 226 | + return array(); |
| 227 | + } |
| 228 | + |
| 229 | + $entityData = $entity->__toArray(); |
| 230 | + $knownAttributes = array_diff(array_keys($entityData), $exclude); |
| 231 | + $attributesToExpose = array_merge($knownAttributes, $include); |
| 232 | + |
| 233 | + $attributes = array(); |
| 234 | + |
| 235 | + if (!empty($attributesToExpose)) { |
| 236 | + $attributes = array_intersect_key( |
| 237 | + array_merge($data, $entityData), |
| 238 | + array_combine($attributesToExpose, $attributesToExpose) |
| 239 | + ); |
| 240 | + } |
| 241 | + |
| 242 | + return $this->packAssoc($attributes); |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Pack associative array to format supported by API. |
| 247 | + * |
| 248 | + * @param array $data |
| 249 | + * @return array |
| 250 | + */ |
| 251 | + public function packAssoc(array $data) |
| 252 | + { |
| 253 | + $result = array(); |
| 254 | + foreach ($data as $key => $value) { |
| 255 | + $result[] = array( |
| 256 | + 'key' => $key, |
| 257 | + 'value' => $value |
| 258 | + ); |
| 259 | + } |
| 260 | + |
| 261 | + return $result; |
| 262 | + } |
| 263 | +} |
0 commit comments