Mage_Poll_Model_Mysql4_Poll Class Reference

Inheritance diagram for Mage_Poll_Model_Mysql4_Poll:

Mage_Core_Model_Mysql4_Abstract Mage_Core_Model_Resource_Abstract

List of all members.

Public Member Functions

 getRandomId ($object)
 checkAnswerId ($poll, $answerId)
 getVotedPollIdsByIp ($ipAddress, $pollId=false)
 resetVotesCount ($object)
 loadStoreIds (Mage_Poll_Model_Poll $object)
 _afterSave (Mage_Core_Model_Abstract $object)
 lookupStoreIds ($id)

Protected Member Functions

 _construct ()
 _initUniqueFields ()


Detailed Description

Poll Mysql4 resource model

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 33 of file Poll.php.


Member Function Documentation

_afterSave ( Mage_Core_Model_Abstract object  ) 

Perform actions after object save

Parameters:
Varien_Object $object

stores

answers

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 159 of file Poll.php.

00160     {
00161         /** stores */
00162         $deleteWhere = $this->_getWriteAdapter()->quoteInto('poll_id = ?', $object->getId());
00163         $this->_getWriteAdapter()->delete($this->getTable('poll/poll_store'), $deleteWhere);
00164 
00165         foreach ($object->getStoreIds() as $storeId) {
00166             $pollStoreData = array(
00167             'poll_id'   => $object->getId(),
00168             'store_id'  => $storeId
00169             );
00170             $this->_getWriteAdapter()->insert($this->getTable('poll/poll_store'), $pollStoreData);
00171         }
00172 
00173         /** answers */
00174         foreach ($object->getAnswers() as $answer) {
00175             $answer->setPollId($object->getId());
00176             $answer->save();
00177         }
00178     }

_construct (  )  [protected]

Resource initialization

Reimplemented from Mage_Core_Model_Resource_Abstract.

Definition at line 35 of file Poll.php.

00036     {
00037         $this->_init('poll/poll', 'poll_id');
00038     }

_initUniqueFields (  )  [protected]

Initialize unique fields

Returns:
Mage_Core_Model_Mysql4_Abstract

Reimplemented from Mage_Core_Model_Mysql4_Abstract.

Definition at line 45 of file Poll.php.

00046     {
00047         $this->_uniqueFields = array(array(
00048             'field' => 'poll_title',
00049             'title' => Mage::helper('poll')->__('Poll with the same question')
00050         ));
00051         return $this;
00052     }

checkAnswerId ( poll,
answerId 
)

Check answer id existing for poll

Parameters:
Mage_Poll_Model_Poll $poll
int $answerId
Returns:
bool

Definition at line 92 of file Poll.php.

00093     {
00094         $select = $this->_getReadAdapter()->select()
00095             ->from($this->getTable('poll_answer'), 'answer_id')
00096             ->where('poll_id=?', $poll->getId())
00097             ->where('answer_id=?', $answerId);
00098         return $this->_getReadAdapter()->fetchOne($select);
00099     }

getRandomId ( object  ) 

Get random identifier not closed poll

Parameters:
Mage_Poll_Model_Poll $object
Returns:
int

Definition at line 60 of file Poll.php.

00061     {
00062         $read = $this->_getReadAdapter();
00063         $select = $read->select();
00064 
00065         if ($object->getExcludeFilter()) {
00066             $select->where('main_table.poll_id NOT IN(?)', $object->getExcludeFilter());
00067         }
00068 
00069         $select->from(array('main_table'=>$this->getMainTable()), $this->getIdFieldName())
00070             ->where('closed = ?', 0)
00071             ->order(new Zend_Db_Expr('RAND()'))
00072             ->limit(1);
00073 
00074         if (($storeId = $object->getStoreFilter())) {
00075             $select->join(
00076                 array('store' => $this->getTable('poll/poll_store')),
00077                 $read->quoteInto('main_table.poll_id=store.poll_id AND store.store_id = ?', $storeId),
00078                 array()
00079             );
00080         }
00081 
00082         return $read->fetchOne($select);
00083     }

getVotedPollIdsByIp ( ipAddress,
pollId = false 
)

Get voted poll ids by specified IP-address

Will return non-empty only if appropriate option in config is enabled If poll id is not empty, it will look only for records with specified value

Parameters:
string $ipAddress
int $pollId
Returns:
array

Definition at line 111 of file Poll.php.

00112     {
00113         // check if validation by ip is enabled
00114         if (!Mage::getModel('poll/poll')->isValidationByIp()) {
00115             return array();
00116         }
00117 
00118         // look for ids in database
00119         $select = $this->_getReadAdapter()->select()
00120             ->distinct()
00121             ->from($this->getTable('poll_vote'), 'poll_id')
00122             ->where('ip_address=?', ip2long($ipAddress));
00123         if (!empty($pollId)) {
00124             $select->where('poll_id=?', $pollId);
00125         }
00126         $result = $this->_getReadAdapter()->fetchCol($select);
00127         if (empty($result)) {
00128             $result = array();
00129         }
00130         return $result;
00131     }

loadStoreIds ( Mage_Poll_Model_Poll object  ) 

Definition at line 149 of file Poll.php.

00150     {
00151         $pollId   = $object->getId();
00152         $storeIds = array();
00153         if ($pollId) {
00154             $storeIds = $this->lookupStoreIds($pollId);
00155         }
00156         $object->setStoreIds($storeIds);
00157     }

lookupStoreIds ( id  ) 

Get store ids to which specified item is assigned

Parameters:
int $id
Returns:
array

Definition at line 186 of file Poll.php.

00187     {
00188         return $this->_getReadAdapter()->fetchCol($this->_getReadAdapter()->select()
00189             ->from($this->getTable('poll/poll_store'), 'store_id')
00190             ->where("{$this->getIdFieldName()} = ?", $id)
00191         );
00192     }

resetVotesCount ( object  ) 

Definition at line 133 of file Poll.php.

00134     {
00135         $read = $this->_getReadAdapter();
00136         $select = $read->select();
00137         $select->from($this->getTable('poll_answer'), new Zend_Db_Expr("SUM(votes_count)"))
00138             ->where("poll_id = ?", $object->getPollId());
00139 
00140         $count = $read->fetchOne($select);
00141 
00142         $write = $this->_getWriteAdapter();
00143         $condition = $write->quoteInto("{$this->getIdFieldName()} = ?", $object->getPollId());
00144         $write->update($this->getMainTable(), array('votes_count' => $count), $condition);
00145         return $object;
00146     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:34 2009 for Magento by  doxygen 1.5.8