00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Poll_Model_Mysql4_Poll_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
00035 {
00036 public function _construct()
00037 {
00038 $this->_init('poll/poll');
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048 public function addFieldToFilter($field, $condition=null)
00049 {
00050 if ($field == 'stores') {
00051 return $this->addStoresFilter($condition);
00052 }
00053 else {
00054 return parent::addFieldToFilter($field, $condition);
00055 }
00056 }
00057
00058
00059
00060
00061
00062
00063
00064 public function addStoresFilter($store)
00065 {
00066 return $this->addStoresFilter($store);
00067 }
00068
00069
00070
00071
00072
00073
00074
00075 public function addStoreFilter($storeId, $withAdmin = true)
00076 {
00077 $this->getSelect()->join(
00078 array('store_table' => $this->getTable('poll/poll_store')),
00079 'main_table.poll_id = store_table.poll_id',
00080 array()
00081 )
00082 ->where('store_table.store_id in (?)', ($withAdmin ? array(0, $storeId) : $storeId))
00083 ->group('main_table.poll_id');
00084
00085 return $this;
00086 }
00087
00088
00089
00090
00091
00092
00093 public function addStoreData()
00094 {
00095 $pollIds = $this->getColumnValues('poll_id');
00096 $storesToPoll = array();
00097
00098 if (count($pollIds) > 0) {
00099 $select = $this->getConnection()->select()
00100 ->from($this->getTable('poll/poll_store'))
00101 ->where('poll_id IN(?)', $pollIds);
00102 $result = $this->getConnection()->fetchAll($select);
00103
00104 foreach ($result as $row) {
00105 if (!isset($storesToPoll[$row['poll_id']])) {
00106 $storesToPoll[$row['poll_id']] = array();
00107 }
00108 $storesToPoll[$row['poll_id']][] = $row['store_id'];
00109 }
00110 }
00111
00112 foreach ($this as $item) {
00113 if(isset($storesToPoll[$item->getId()])) {
00114 $item->setStores($storesToPoll[$item->getId()]);
00115 } else {
00116 $item->setStores(array());
00117 }
00118 }
00119
00120 return $this;
00121 }
00122
00123 public function addSelectStores()
00124 {
00125 $pollId = $this->getId();
00126 $select = $this->getConnection()->select()
00127 ->from($this->getTable('poll/poll_store'))
00128 ->where('poll_id = ?', $pollId);
00129 $result = $this->getConnection()->fetchAll($select);
00130 $stores = array();
00131 foreach ($result as $row) {
00132 $stores[] = $row['stor_id'];
00133 }
00134 $this->setSelectStores($stores);
00135
00136 return $this;
00137 }
00138 }