BusinessLogicProcessor.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. class BusinessLogicProcessor
  3. {
  4. public static function getInstance()
  5. {
  6. return new BusinessLogicProcessor(MySqliWrapper::getInstance(), BusinessLogicFactory::getInstance());
  7. }
  8. private $mysqli;
  9. private $businessLogicFactory;
  10. private function __construct(MySqliWrapper $mysqli, BusinessLogicFactory $businessLogicFactory)
  11. {
  12. $this->mysqli = $mysqli;
  13. $this->businessLogicFactory = $businessLogicFactory;
  14. }
  15. public function processLoad($pageId)
  16. {
  17. $data = array();
  18. $rulesetArray = $this->getLoadingRuleset($pageId);
  19. foreach ($rulesetArray as $rule)
  20. {
  21. $result = $rule->process();
  22. if(is_array($result))
  23. {
  24. $data = array_merge($data, $result);
  25. }
  26. }
  27. return $data;
  28. }
  29. // This is just a quick hack; there will be no distinction between ajax and
  30. // "normal" submit
  31. public function processAjaxSubmit($pageId)
  32. {
  33. $coregList = explode(",", $_POST['coregs']);
  34. if(!isset($_POST['coregs']) || empty($_POST['coregs'])) {
  35. return array("next" => 15);
  36. }
  37. if(in_array('13005', $coregList))
  38. { // man
  39. if(count($coregList) >= 2)
  40. {
  41. return array("next" => 14);
  42. }
  43. return array("next" => 12);
  44. }
  45. if(in_array('13006', $coregList))
  46. { // woman
  47. if(count($coregList) >= 2)
  48. {
  49. return array("next" => 14);
  50. }
  51. return array("next" => 13);
  52. }
  53. return array("next" => 2, "redirect" => true);
  54. }
  55. public function processSubmit($pageId)
  56. {
  57. $data = array();
  58. $rulesetArray = $this->getSubmittingRuleset($pageId);
  59. foreach ($rulesetArray as $rule)
  60. {
  61. $result = $rule->process();
  62. if(is_array($result))
  63. {
  64. $data = array_merge($data, $result);
  65. }
  66. }
  67. return $data;
  68. }
  69. private function getLoadingRuleset($pageId)
  70. {
  71. $sql = sprintf("SELECT `logic_id` FROM `page_instructions` WHERE `page_id` = %s AND `on_submit` = 0 ORDER BY `order`", $pageId);
  72. $result = $this->mysqli->get($sql);
  73. $rules = array();
  74. foreach ($result as $row)
  75. {
  76. $rules[] = $this->businessLogicFactory->createRule($row['logic_id']);
  77. }
  78. return $rules;
  79. }
  80. private function getSubmittingRuleset($pageId)
  81. {
  82. $sql = sprintf("SELECT `logic_id` FROM `page_instructions` WHERE `page_id` = %s AND `on_submit` = 1 ORDER BY `order`", $pageId);
  83. $result = $this->mysqli->get($sql);
  84. $rules = array();
  85. foreach ($result as $row)
  86. {
  87. $rules[] = $this->businessLogicFactory->createRule($row['logic_id']);
  88. }
  89. return $rules;
  90. }
  91. }