/system/app/Tools/GroupAssignment.php

https://github.com/seosamba/ecommerce · PHP · 271 lines · 211 code · 26 blank · 34 comment · 57 complexity · 4508fec50c3c4bdd3c78f389d8e2b634 MD5 · raw file

  1. <?php
  2. /**
  3. * Tool GroupAssignment.php
  4. */
  5. class Tools_GroupAssignment
  6. {
  7. /**
  8. * Assign user group based on the user custom param data
  9. *
  10. * @param int $userId system user id
  11. * @param array $userCustomParams custom params name value pair
  12. * @return array
  13. */
  14. public static function processGroupsByUserCustomParams($userId, $userCustomParams)
  15. {
  16. if (empty($userCustomParams)) {
  17. return array('error' => '1', 'message' => 'Empty custom params');
  18. }
  19. $userMapper = Application_Model_Mappers_UserMapper::getInstance();
  20. $userModel = $userMapper->find($userId);
  21. if (!$userModel instanceof Application_Model_Models_User) {
  22. return array('error' => '1', 'User not found');
  23. }
  24. $attributes = $userMapper->fetchUniqueAttributesNames();
  25. if (empty($attributes)) {
  26. return array('error' => '1', 'message' => 'There are no attributes has been found.');
  27. }
  28. $cleanAttributes = array();
  29. foreach ($userCustomParams as $attrName => $attrValue) {
  30. //Disabled until we have custom params config
  31. //if (in_array($attrName, $attributes, true)) {
  32. $cleanAttributes[$attrName] = $attrValue;
  33. //}
  34. }
  35. if (empty($cleanAttributes)) {
  36. return array('error' => '1', 'message' => 'There are no attributes has been found.');
  37. }
  38. $customerRulesGeneralConfigMapper = Store_Mapper_CustomerRulesGeneralConfigMapper::getInstance();
  39. $rules = $customerRulesGeneralConfigMapper->getConfigIds();
  40. if (empty($rules)) {
  41. return array('error' => '1', 'message' => 'No rules');
  42. }
  43. $ruleIds = array_combine(array_keys($rules), array_keys($rules));
  44. $rulesWithFields = $customerRulesGeneralConfigMapper->getRulesByIds(array_keys($ruleIds));
  45. if (!empty($rulesWithFields)) {
  46. foreach ($rulesWithFields as $rulesWithField) {
  47. unset($ruleIds[$rulesWithField['id']]);
  48. }
  49. $ruleIdsToAdd = self::getRuleIdsBasedOnData($rulesWithFields, $cleanAttributes);
  50. if (!empty($ruleIdsToAdd)) {
  51. foreach ($ruleIdsToAdd as $ruleIdToAdd => $ruleToAdd) {
  52. $ruleIds[$ruleIdToAdd] = $ruleIdToAdd;
  53. }
  54. }
  55. }
  56. if (!empty($ruleIds)) {
  57. self::applyRuleActions($ruleIds, $userModel, $cleanAttributes);
  58. }
  59. return array('error' => '0');
  60. }
  61. /**
  62. * Get rule ids based on form data
  63. *
  64. * @param array $rules form rules
  65. * @param array $data form data
  66. * @return array
  67. */
  68. public static function getRuleIdsBasedOnData($rules, $data)
  69. {
  70. $ruleIds = array();
  71. $skipId = 0;
  72. foreach ($rules as $rule) {
  73. $skipStatus = false;
  74. $operator = $rule['rule_comparison_operator'];
  75. $fieldName = $rule['field_name'];
  76. $fieldNameAlternative = $rule['field_name'] . '[]';
  77. $fieldValue = mb_strtolower($rule['field_value']);
  78. if (isset($data[$fieldNameAlternative])) {
  79. $fieldName = $fieldNameAlternative;
  80. }
  81. if (!isset($data[$fieldName])) {
  82. if (isset($ruleIds[$rule['id']])) {
  83. unset($ruleIds[$rule['id']]);
  84. }
  85. $skipId = $rule['id'];
  86. continue;
  87. }
  88. if (is_array($data[$fieldName])) {
  89. $dataValue = array_map('mb_strtolower', $data[$fieldName]);
  90. } else {
  91. $dataValue = mb_strtolower($data[$fieldName]);
  92. }
  93. $ruleId = $rule['id'];
  94. if ($skipId === $ruleId) {
  95. continue;
  96. }
  97. if (array_key_exists($fieldName, $data)) {
  98. if ($operator === Leads_Model_LeadsFormRulesConfigModel::RULE_COMPARISON_OPERATOR_EQUAL) {
  99. if (is_array($dataValue)) {
  100. if (!in_array($fieldValue, $dataValue)) {
  101. $skipStatus = true;
  102. }
  103. } else {
  104. if ($dataValue !== $fieldValue) {
  105. $skipStatus = true;
  106. }
  107. }
  108. }
  109. if ($operator === Leads_Model_LeadsFormRulesConfigModel::RULE_COMPARISON_OPERATOR_IN) {
  110. $compareToArray = explode(',', $fieldValue);
  111. $compareToArray = array_map("mb_strtolower", $compareToArray);
  112. if (is_array($dataValue)) {
  113. $comparisonResult = array_intersect($compareToArray, $dataValue);
  114. if (count($comparisonResult) == 0) {
  115. $skipStatus = true;
  116. }
  117. } else {
  118. if (!in_array($dataValue, $compareToArray)) {
  119. $skipStatus = true;
  120. }
  121. }
  122. }
  123. if ($operator === Leads_Model_LeadsFormRulesConfigModel::RULE_COMPARISON_OPERATOR_LIKE) {
  124. if (is_array($dataValue)) {
  125. $inputSearchPattern = preg_quote($fieldValue, '~');
  126. $inputSearchResult = preg_grep('~' . $inputSearchPattern . '~', $dataValue);
  127. if (empty($inputSearchResult)) {
  128. $skipStatus = true;
  129. }
  130. } else {
  131. $matchFound = mb_strpos($dataValue, $fieldValue);
  132. if ($matchFound === false) {
  133. $skipStatus = true;
  134. }
  135. }
  136. }
  137. if ($operator === Leads_Model_LeadsFormRulesConfigModel::RULE_COMPARISON_OPERATOR_NOTEQUAL) {
  138. if (is_array($dataValue)) {
  139. if (in_array($fieldValue, $dataValue)) {
  140. $skipStatus = true;
  141. }
  142. } else {
  143. if ($dataValue === $fieldValue) {
  144. $skipStatus = true;
  145. }
  146. }
  147. }
  148. if ($operator === Leads_Model_LeadsFormRulesConfigModel::RULE_COMPARISON_GREATER_THAN) {
  149. if (is_numeric($dataValue)) {
  150. if ($fieldValue > $dataValue) {
  151. $skipStatus = true;
  152. }
  153. } elseif (is_array($dataValue)) {
  154. foreach ($dataValue as $value) {
  155. if (is_numeric($value)) {
  156. if ($fieldValue > $dataValue) {
  157. $skipStatus = true;
  158. }
  159. } else {
  160. $skipStatus = true;
  161. }
  162. }
  163. } else {
  164. $skipStatus = true;
  165. }
  166. }
  167. if ($operator === Leads_Model_LeadsFormRulesConfigModel::RULE_COMPARISON_LESS_THAN) {
  168. if (is_numeric($dataValue)) {
  169. if ($fieldValue < $dataValue) {
  170. $skipStatus = true;
  171. }
  172. } elseif (is_array($dataValue)) {
  173. foreach ($dataValue as $value) {
  174. if (is_numeric($value)) {
  175. if ($fieldValue < $dataValue) {
  176. $skipStatus = true;
  177. }
  178. } else {
  179. $skipStatus = true;
  180. }
  181. }
  182. } else {
  183. $skipStatus = true;
  184. }
  185. }
  186. if ($skipStatus === true) {
  187. $skipId = $ruleId;
  188. unset($ruleIds[$ruleId]);
  189. } else {
  190. $ruleIds[$ruleId] = $ruleId;
  191. }
  192. }
  193. }
  194. return $ruleIds;
  195. }
  196. /**
  197. * Apply actions based on the matched rules
  198. *
  199. * @param array $ruleIds rule ids to apply
  200. * @param Application_Model_Models_User $userModel user Model
  201. * @param array $data form data
  202. */
  203. public static function applyRuleActions(
  204. $ruleIds = array(),
  205. Application_Model_Models_User $userModel,
  206. $data = array()
  207. ) {
  208. $actions = Store_Mapper_CustomerRulesActionsMapper::getInstance()->getActionByRuleIds($ruleIds);
  209. if (!empty($actions)) {
  210. foreach ($actions as $action) {
  211. $actionType = $action['action_type'];
  212. $actionConfig = $action['action_config'];
  213. if (!empty($actionConfig)) {
  214. $actionConfig = json_decode($actionConfig, true);
  215. if ($actionType === Store_Model_CustomerRulesActionModel::ACTION_TYPE_ASSIGN_GROUP) {
  216. self::assignGroup($actionConfig, $userModel, $data);
  217. }
  218. }
  219. }
  220. }
  221. }
  222. /**
  223. * Assign customer group
  224. *
  225. * @param array $actionConfig action config
  226. * @param Application_Model_Models_User $userModel user model
  227. * @param array $data
  228. */
  229. public static function assignGroup($actionConfig = array(), Application_Model_Models_User $userModel, $data)
  230. {
  231. $groupId = $actionConfig['customer_group_id'];
  232. $groupMapper = Store_Mapper_GroupMapper::getInstance();
  233. $groupModel = $groupMapper->find($groupId);
  234. if ($groupModel instanceof Store_Model_Group) {
  235. $customerMapper = Models_Mapper_CustomerMapper::getInstance();
  236. $customerModel = $customerMapper->find($userModel->getId());
  237. if ($customerModel instanceof Models_Model_Customer) {
  238. $customerModel->setGroupId($groupId);
  239. $dataGroup = array('userId' => $customerModel->getId(), 'groupId' => $groupId);
  240. $customerMapper->save($customerModel);
  241. Tools_System_Tools::firePluginMethodByTagName('assigngroup', 'assignLeadGroup', $dataGroup, true);
  242. }
  243. }
  244. }
  245. }