baseline
[omp.pkp.sfu.ca.git] / lib / pkp / classes / announcement / PKPAnnouncementTypeDAO.inc.php
blob74d72ce436d8ccda97652c047a65cd58946054b5
1 <?php
3 /**
4 * @file PKPAnnouncementTypeDAO.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class PKPAnnouncementTypeDAO
10 * @ingroup announcement
11 * @see AnnouncementType, PKPAnnouncementType
13 * @brief Operations for retrieving and modifying AnnouncementType objects.
16 //$Id: PKPAnnouncementTypeDAO.inc.php,v 1.6 2009/08/18 23:44:08 asmecher Exp $
18 import('announcement.PKPAnnouncementType');
20 class PKPAnnouncementTypeDAO extends DAO {
21 /**
22 * Retrieve an announcement type by announcement type ID.
23 * @param $typeId int
24 * @return AnnouncementType
26 function &getAnnouncementType($typeId) {
27 $result =& $this->retrieve(
28 'SELECT * FROM announcement_types WHERE type_id = ?', $typeId
31 $returner = null;
32 if ($result->RecordCount() != 0) {
33 $returner =& $this->_returnAnnouncementTypeFromRow($result->GetRowAssoc(false));
35 $result->Close();
36 return $returner;
39 /**
40 * Retrieve announcement type Assoc ID by announcement type ID.
41 * @param $typeId int
42 * @return int
44 function getAnnouncementTypeAssocId($typeId) {
45 $result =& $this->retrieve(
46 'SELECT assoc_id FROM announcement_types WHERE type_id = ?', $typeId
49 return isset($result->fields[0]) ? $result->fields[0] : 0;
52 /**
53 * Retrieve announcement type name by ID.
54 * @param $typeId int
55 * @return string
57 function getAnnouncementTypeName($typeId) {
58 $result =& $this->retrieve(
59 'SELECT COALESCE(l.setting_value, p.setting_value) FROM announcement_type_settings l LEFT JOIN announcement_type_settings p ON (p.type_id = ? AND p.setting_name = ? AND p.locale = ?) WHERE l.type_id = ? AND l.setting_name = ? AND l.locale = ?',
60 array(
61 $typeId, 'name', Locale::getLocale(),
62 $typeId, 'name', Locale::getPrimaryLocale()
66 $returner = isset($result->fields[0]) ? $result->fields[0] : false;
68 $result->Close();
69 unset($result);
71 return $returner;
75 /**
76 * Check if a announcement type exists with the given type id for a assoc type/id pair.
77 * @param $typeId int
78 * @param $assocType int
79 * @return boolean
81 function announcementTypeExistsByTypeId($typeId, $assocType, $assocId) {
82 $result =& $this->retrieve(
83 'SELECT COUNT(*)
84 FROM announcement_types
85 WHERE type_id = ? AND
86 assoc_type = ? AND
87 assoc_id = ?',
88 array(
89 $typeId,
90 $assocType,
91 $assocId
94 $returner = isset($result->fields[0]) && $result->fields[0] != 0 ? true : false;
96 $result->Close();
97 unset($result);
99 return $returner;
102 function getLocaleFieldNames() {
103 return array('name');
107 * Return announcement type ID based on a type name for an assoc type/id pair.
108 * @param $typeName string
109 * @param $assocType int
110 * @return int
112 function getAnnouncementTypeByTypeName($typeName, $assocType) {
113 $result =& $this->retrieve(
114 'SELECT type_id
115 FROM announcement_types
116 WHERE type_name = ?
117 AND assoc_type = ?
118 AND assoc_id = ?',
119 array(
120 $typeName,
121 $assocType
124 $returner = isset($result->fields[0]) ? $result->fields[0] : 0;
126 $result->Close();
127 unset($result);
129 return $returner;
133 * Internal function to return an AnnouncementType object from a row.
134 * @param $row array
135 * @return AnnouncementType
137 function &_returnAnnouncementTypeFromRow(&$row) {
138 $announcementType = new AnnouncementType();
139 $announcementType->setId($row['type_id']);
140 $announcementType->setAssocType($row['assoc_type']);
141 $announcementType->setAssocId($row['assoc_id']);
142 $this->getDataObjectSettings('announcement_type_settings', 'type_id', $row['type_id'], $announcementType);
144 return $announcementType;
148 * Update the localized settings for this object
149 * @param $announcementType object
151 function updateLocaleFields(&$announcementType) {
152 $this->updateDataObjectSettings('announcement_type_settings', $announcementType, array(
153 'type_id' => $announcementType->getId()
158 * Insert a new AnnouncementType.
159 * @param $announcementType AnnouncementType
160 * @return int
162 function insertAnnouncementType(&$announcementType) {
163 $this->update(
164 sprintf('INSERT INTO announcement_types
165 (assoc_type, assoc_id)
166 VALUES
167 (?, ?)'),
168 array(
169 $announcementType->getAssocType(),
170 $announcementType->getAssocId()
173 $announcementType->setId($this->getInsertTypeId());
174 $this->updateLocaleFields($announcementType);
175 return $announcementType->getId();
179 * Update an existing announcement type.
180 * @param $announcement AnnouncementType
181 * @return boolean
183 function updateObject(&$announcementType) {
184 $returner = $this->update(
185 'UPDATE announcement_types
186 SET assoc_type = ?,
187 assoc_id = ?
188 WHERE type_id = ?',
189 array(
190 $announcementType->getAssocType(),
191 $announcementType->getAssocId(),
192 $announcementType->getId()
196 $this->updateLocaleFields($announcementType);
197 return $returner;
200 function updateAnnouncementType(&$announcementType) {
201 trigger_error('Deprecated function.');
202 return $this->updateObject($announcementType);
206 * Delete an announcement type. Note that all announcements with this type are also
207 * deleted.
208 * @param $announcementType AnnouncementType
209 * @return boolean
211 function deleteObject($announcementType) {
212 return $this->deleteAnnouncementTypeById($announcementType->getId());
215 function deleteAnnouncementType($announcementType) {
216 trigger_error('Deprecated function.');
217 return $this->deleteObject($announcementType);
221 * Delete an announcement type by announcement type ID. Note that all announcements with
222 * this type ID are also deleted.
223 * @param $typeId int
224 * @return boolean
226 function deleteAnnouncementTypeById($typeId) {
227 $this->update('DELETE FROM announcement_type_settings WHERE type_id = ?', $typeId);
228 $ret = $this->update('DELETE FROM announcement_types WHERE type_id = ?', $typeId);
230 // Delete all announcements with this announcement type
231 if ($ret) {
232 $announcementDao =& DAORegistry::getDAO('AnnouncementDAO');
233 return $announcementDao->deleteAnnouncementByTypeId($typeId);
234 } else {
235 return $ret;
240 * Delete announcement types by Assoc ID.
241 * @param $assocType int
243 function deleteAnnouncementTypesByAssocId($assocType, $assocId) {
244 $types =& $this->getAnnouncementTypesByAssocId($assocType, $assocId);
245 while (($type =& $types->next())) {
246 $this->deleteObject($type);
247 unset($type);
252 * Retrieve an array of announcement types matching a particular Assoc ID.
253 * @param $assocType int
254 * @return object DAOResultFactory containing matching AnnouncementTypes
256 function &getAnnouncementTypesByAssocId($assocType, $assocId, $rangeInfo = null) {
257 $result =& $this->retrieveRange(
258 'SELECT * FROM announcement_types WHERE assoc_type = ? AND assoc_id = ? ORDER BY type_id', array($assocType, $assocId), $rangeInfo
261 $returner = new DAOResultFactory($result, $this, '_returnAnnouncementTypeFromRow');
262 return $returner;
266 * Get the ID of the last inserted announcement type.
267 * @return int
269 function getInsertTypeId() {
270 return $this->getInsertId('announcement_types', 'type_id');