myUser.class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. class myUser extends sfBasicSecurityUser {
  3. /**
  4. * myUser::getInstance()
  5. *
  6. * @return KataoUser
  7. */
  8. public function getInstance() {
  9. return $this->getAttribute('katao_user_instance', null);
  10. }
  11. public function setInstance(KataoUser $instance) {
  12. $this->setAttribute('katao_user_instance', $instance);
  13. }
  14. /**
  15. * myUser::getActivePeriod()
  16. *
  17. * @return KataoPeriod
  18. */
  19. public function getActivePeriod() {
  20. return $this->getAttribute('katao_period_instance', KataoPeriodPeer::getActivePeriod());
  21. }
  22. public function setActivePeriod(KataoPeriod $instance = null) {
  23. $this->setAttribute('katao_period_instance', $instance);
  24. }
  25. /**
  26. * myUser::getActiveNode()
  27. *
  28. * @return KataoNode
  29. */
  30. public function getActiveNode() {
  31. return $this->getAttribute('katao_node_instance', null);
  32. }
  33. public function setActiveNode(KataoNode $instance = null) {
  34. $this->setAttribute('katao_node_instance', $instance);
  35. $this->initAvailableProducts();
  36. }
  37. /**
  38. * myUser::getDisplayOnlyActiveProducts()
  39. *
  40. * @return boolean
  41. */
  42. public function getDisplayOnlyActiveProducts() {
  43. return $this->getAttribute('katao_display_only_active_products', true);
  44. }
  45. public function setDisplayOnlyActiveProducts($value) {
  46. $this->setAttribute('katao_display_only_active_products', $value);
  47. $this->initAvailableProducts();
  48. }
  49. public function getAvailableProducts() {
  50. if (-1 == $this->getAttribute('katao_available_products', -1)) {
  51. $this->initAvailableProducts();
  52. }
  53. return $this->getAttribute('katao_available_products', array());
  54. }
  55. public function getCartableProducts() {
  56. return $this->getAttribute('katao_cartable_products', array());
  57. }
  58. public function initAvailableProducts() {
  59. $active_products = array();
  60. $all_products = array();
  61. $katao_node_id = $this->getActiveNode()?$this->getActiveNode()->getId():false;
  62. $katao_period_id = $this->getActivePeriod()?$this->getActivePeriod()->getId():false;
  63. $display_only_active_products = $this->getDisplayOnlyActiveProducts();
  64. /* Retrieve all products from complete suppliers */
  65. $criteria = new Criteria();
  66. $criteria->addSelectColumn(KataoProductPeer::ID);
  67. $criteria->addSelectColumn(KataoNodeSupplierPeer::KATAO_PERIOD_ID);
  68. $criteria->addJoin(KataoProductPeer::KATAO_SUPPLIER_ID, KataoSupplierPeer::ID);
  69. $criteria->addJoin(KataoSupplierPeer::ID, KataoNodeSupplierPeer::KATAO_SUPPLIER_ID);
  70. if ($katao_node_id) {
  71. $criteria->add(KataoNodeSupplierPeer::KATAO_NODE_ID, $katao_node_id);
  72. }
  73. $criteria->add(KataoNodeSupplierPeer::INCLUDE_ALL_PRODUCTS, true);
  74. $rs = KataoProductPeer::doSelectRS($criteria);
  75. while ($rs->next()) {
  76. if (!$display_only_active_products) {
  77. $all_products[$rs->getInt(1)] = true;
  78. if ($rs->getInt(2) == $katao_period_id) {
  79. $active_products[$rs->getInt(1)] = true;
  80. }
  81. } elseif ($rs->getInt(2) == $katao_period_id) {
  82. $all_products[$rs->getInt(1)] = true;
  83. $active_products[$rs->getInt(1)] = true;
  84. }
  85. }
  86. /* Retrieve selected products from incomplete suppliers */
  87. $criteria = new Criteria();
  88. $criteria->addSelectColumn(KataoNodeProductPeer::KATAO_PRODUCT_ID);
  89. $criteria->addSelectColumn(KataoNodeProductPeer::KATAO_PERIOD_ID);
  90. if ($katao_node_id) {
  91. $criteria->add(KataoNodeProductPeer::KATAO_NODE_ID, $katao_node_id);
  92. }
  93. $rs = KataoNodeProductPeer::doSelectRS($criteria);
  94. while ($rs->next()) {
  95. if (!$display_only_active_products) {
  96. $all_products[$rs->getInt(1)] = true;
  97. if ($rs->getInt(2) == $katao_period_id) {
  98. $active_products[$rs->getInt(1)] = true;
  99. }
  100. } elseif ($rs->getInt(2) == $katao_period_id) {
  101. $all_products[$rs->getInt(1)] = true;
  102. $active_products[$rs->getInt(1)] = true;
  103. }
  104. }
  105. $this->setAttribute('katao_available_products', array_keys($all_products));
  106. $this->setAttribute('katao_cartable_products', array_keys($active_products));
  107. }
  108. }