Open Journal Systems  2.4.3
 All Classes Namespaces Functions Variables Groups Pages
ReferralDAO.inc.php
1 <?php
2 
17 class ReferralDAO extends DAO {
23  function &getReferral($referralId) {
24  $result =& $this->retrieve(
25  'SELECT * FROM referrals WHERE referral_id = ?', $referralId
26  );
27 
28  $returner = null;
29  if ($result->RecordCount() != 0) {
30  $returner =& $this->_returnReferralFromRow($result->GetRowAssoc(false));
31  }
32  $result->Close();
33  return $returner;
34  }
35 
40  function getLocaleFieldNames() {
41  return array('name');
42  }
43 
49  function &_returnReferralFromRow(&$row) {
50  $referral = new Referral();
51  $referral->setId($row['referral_id']);
52  $referral->setArticleId($row['article_id']);
53  $referral->setStatus($row['status']);
54  $referral->setUrl($row['url']);
55  $referral->setDateAdded($this->datetimeFromDB($row['date_added']));
56  $referral->setLinkCount($row['link_count']);
57 
58  $this->getDataObjectSettings('referral_settings', 'referral_id', $row['referral_id'], $referral);
59 
60  return $referral;
61  }
62 
69  function referralExistsByUrl($articleId, $url) {
70  $result =& $this->retrieve(
71  'SELECT COUNT(*)
72  FROM referrals
73  WHERE article_id = ? AND
74  url = ?',
75  array(
76  (int) $articleId,
77  $url
78  )
79  );
80  $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
81 
82  $result->Close();
83  unset($result);
84 
85  return $returner;
86  }
87 
94  function incrementReferralCount($articleId, $url) {
95  return $this->update(
96  'UPDATE referrals SET link_count = link_count + 1 WHERE article_id = ? AND url = ?',
97  array((int) $articleId, $url)
98  );
99  }
100 
105  function updateLocaleFields(&$referral) {
106  $this->updateDataObjectSettings('referral_settings', $referral, array(
107  'referral_id' => $referral->getId()
108  ));
109  }
110 
116  function replaceReferral(&$referral) {
117  $date = trim($this->datetimeToDB($referral->getDateAdded()), "'");
118  $this->replace(
119  'referrals',
120  array(
121  'status' => (int) $referral->getStatus(),
122  'article_id' => (int) $referral->getArticleId(),
123  'url' => $referral->getUrl(),
124  'date_added' => $date,
125  'link_count' => (int) $referral->getLinkCount(),
126  ),
127  array('article_id', 'url')
128  );
129 
130  $referral->setId($this->getInsertObjectId());
131  $this->updateLocaleFields($referral);
132  return $referral->getId();
133  }
134 
140  function updateReferral(&$referral) {
141  $returner = $this->update(
142  sprintf('UPDATE referrals
143  SET status = ?,
144  article_id = ?,
145  url = ?,
146  date_added = %s,
147  link_count = ?
148  WHERE referral_id = ?',
149  $this->datetimeToDB($referral->getDateAdded())
150  ),
151  array(
152  (int) $referral->getStatus(),
153  (int) $referral->getArticleId(),
154  $referral->getUrl(),
155  (int) $referral->getLinkCount(),
156  (int) $referral->getId()
157  )
158  );
159  $this->updateLocaleFields($referral);
160  return $returner;
161  }
162 
169  function deleteReferral($referral) {
170  return $this->deleteReferralById($referral->getId());
171  }
172 
178  function deleteReferralById($referralId) {
179  $this->update('DELETE FROM referral_settings WHERE referral_id = ?', (int) $referralId);
180  return $this->update('DELETE FROM referrals WHERE referral_id = ?', (int) $referralId);
181  }
182 
191  function getByUserId($userId, $journalId, $status = null, $rangeInfo = null) {
192  $params = array((int) $userId, (int) $journalId);
193  if ($status !== null) $params[] = (int) $status;
194  $result = $this->retrieveRange(
195  'SELECT r.*
196  FROM referrals r,
197  articles a
198  WHERE r.article_id = a.article_id AND
199  a.user_id = ? AND
200  a.journal_id = ?' .
201  ($status !== null?' AND r.status = ?':'') . '
202  ORDER BY r.date_added',
203  $params,
204  $rangeInfo
205  );
206 
207  return new DAOResultFactory($result, $this, '_returnReferralFromRow');
208  }
209 
216  function &getPublishedReferralsForArticle($articleId, $rangeInfo = null) {
217  $result =& $this->retrieveRange(
218  'SELECT r.*
219  FROM referrals r
220  WHERE r.article_id = ? AND
221  r.status = ?',
222  array((int) $articleId, REFERRAL_STATUS_ACCEPT),
223  $rangeInfo
224  );
225 
226  $returner = new DAOResultFactory($result, $this, '_returnReferralFromRow');
227  return $returner;
228  }
229 
234  function getInsertObjectId() {
235  return $this->getInsertId('referrals', 'referral_id');
236  }
237 }
238 
239 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& getPublishedReferralsForArticle($articleId, $rangeInfo=null)
Basic class describing a referral.
referralExistsByUrl($articleId, $url)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
updateLocaleFields(&$referral)
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:460
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
datetimeFromDB($dt)
Definition: DAO.inc.php:316
Operations for retrieving and modifying Referral objects.
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
incrementReferralCount($articleId, $url)
deleteReferral($referral)
updateReferral(&$referral)
replaceReferral(&$referral)
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:240
deleteReferralById($referralId)
datetimeToDB($dt)
Definition: DAO.inc.php:296
getByUserId($userId, $journalId, $status=null, $rangeInfo=null)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
& getReferral($referralId)
& _returnReferralFromRow(&$row)