Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
CategoryDAO.inc.php
1 <?php
2 
18 import ('classes.press.Category');
19 
20 class CategoryDAO extends DAO {
24  function CategoryDAO() {
25  parent::DAO();
26  }
27 
35  function getById($categoryId, $pressId = null, $parentId = null) {
36  $params = array((int) $categoryId);
37  if ($pressId) $params[] = (int) $pressId;
38  if ($parentId) $params[] = (int) $parentId;
39 
40  $result = $this->retrieve(
41  'SELECT *
42  FROM categories
43  WHERE category_id = ?
44  ' . ($pressId?' AND press_id = ?':'') . '
45  ' . ($parentId?' AND parent_id = ?':''),
46  $params
47  );
48 
49  $returner = null;
50  if ($result->RecordCount() != 0) {
51  $returner = $this->_fromRow($result->GetRowAssoc(false));
52  }
53  $result->Close();
54  return $returner;
55  }
56 
63  function getByPath($path, $pressId) {
64  $returner = null;
65  $result = $this->retrieve(
66  'SELECT * FROM categories WHERE path = ? AND press_id = ?',
67  array((string) $path, (int) $pressId)
68  );
69 
70  if ($result->RecordCount() != 0) {
71  $returner = $this->_fromRow($result->GetRowAssoc(false));
72  }
73 
74  $result->Close();
75  return $returner;
76  }
77 
85  function getByTitle($categoryTitle, $pressId, $locale = null) {
86  $params = array('title', $categoryTitle, (int) $pressId);
87  if ($locale) $params[] = $locale;
88 
89  $result = $this->retrieve(
90  'SELECT a.*
91  FROM categories a,
92  category_settings l
93  WHERE l.category_id = a.category_id AND
94  l.setting_name = ? AND
95  l.setting_value = ?
96  AND a.press_id = ?
97  ' . ($locale?' AND locale = ?':''),
98  $params
99  );
100 
101  $returner = null;
102  if ($result->RecordCount() != 0) {
103  $returner = $this->_fromRow($result->GetRowAssoc(false));
104  }
105 
106  $result->Close();
107  return $returner;
108  }
109 
115  function categoryExistsByPath($path) {
116  $result = $this->retrieve(
117  'SELECT COUNT(*) FROM categories WHERE path = ?', $path
118  );
119  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
120 
121  $result->Close();
122  return $returner;
123  }
124 
129  function newDataObject() {
130  return new Category();
131  }
132 
138  function _fromRow($row) {
139  $category = $this->newDataObject();
140 
141  $category->setId($row['category_id']);
142  $category->setPressId($row['press_id']);
143  $category->setParentId($row['parent_id']);
144  $category->setPath($row['path']);
145  $category->setImage(unserialize($row['image']));
146 
147  $this->getDataObjectSettings('category_settings', 'category_id', $row['category_id'], $category);
148 
149  HookRegistry::call('CategoryDAO::_fromRow', array(&$category, &$row));
150 
151  return $category;
152  }
153 
158  function getLocaleFieldNames() {
159  return array('title', 'description');
160  }
161 
166  function updateLocaleFields($category) {
168  'category_settings', $category,
169  array(
170  'category_id' => $category->getId()
171  )
172  );
173  }
174 
180  function insertObject($category) {
181  $this->update(
182  'INSERT INTO categories
183  (press_id, parent_id, path, image)
184  VALUES
185  (?, ?, ?, ?)',
186  array(
187  (int) $category->getPressId(),
188  (int) $category->getParentId(),
189  $category->getPath(),
190  serialize($category->getImage() ? $category->getImage() : array()),
191  )
192  );
193 
194  $category->setId($this->getInsertId());
195  $this->updateLocaleFields($category);
196  return $category->getId();
197  }
198 
203  function updateObject($category) {
204  $returner = $this->update(
205  'UPDATE categories
206  SET press_id = ?,
207  parent_id = ?,
208  path = ?,
209  image = ?
210  WHERE category_id = ?',
211  array(
212  (int) $category->getPressId(),
213  (int) $category->getParentId(),
214  $category->getPath(),
215  serialize($category->getImage() ? $category->getImage() : array()),
216  (int) $category->getId()
217  )
218  );
219  $this->updateLocaleFields($category);
220  return $returner;
221  }
222 
227  function deleteObject($category) {
228  return $this->deleteById(
229  $category->getId(),
230  $category->getPressId()
231  );
232  }
233 
239  function deleteById($categoryId, $pressId = null) {
240  $params = array((int) $categoryId);
241  if ($pressId) $params[] = (int) $pressId;
242 
243  $this->update(
244  'DELETE FROM categories
245  WHERE category_id = ?
246  ' . ($pressId?' AND press_id = ?':''),
247  $params
248  );
249 
250  // If the category was deleted (this validates press_id,
251  // if specified), delete any associated settings as well.
252  if ($this->getAffectedRows()) {
253  $this->update(
254  'DELETE FROM category_settings WHERE category_id = ?',
255  array((int) $categoryId)
256  );
257 
258  // remove any monograph assignments for this category.
259  $this->update(
260  'DELETE FROM submission_categories WHERE category_id = ?',
261  array((int) $categoryId)
262  );
263  }
264  }
265 
272  function deleteByPressId($pressId) {
273  $categories = $this->getByPressId($pressId);
274  while ($category = $categories->next()) {
275  $this->deleteObject($category, $pressId);
276  }
277  }
278 
283  function getByPressId($pressId, $rangeInfo = null) {
284  // The strange ORDER BY clause is to return subcategories
285  // immediately after their parent category's entry.
286  $result = $this->retrieveRange(
287  'SELECT *
288  FROM categories
289  WHERE press_id = ?
290  ORDER BY CASE WHEN parent_id = 0 THEN category_id * 2 ELSE (parent_id * 2) + 1 END ASC',
291  array((int) $pressId)
292  );
293 
294  return new DAOResultFactory($result, $this, '_fromRow');
295  }
296 
301  function getCountByPressId($pressId) {
302  $result = $this->retrieve(
303  'SELECT COUNT(*)
304  FROM categories
305  WHERE press_id = ?',
306  (int) $pressId
307  );
308 
309  $returner = $result->fields[0];
310  $result->Close();
311  return $returner;
312  }
313 
318  function getByParentId($parentId, $pressId = null, $rangeInfo = null) {
319  $params = array((int) $parentId);
320  if ($pressId) $params[] = (int) $pressId;
321 
322  $result = $this->retrieveRange(
323  'SELECT *
324  FROM categories
325  WHERE parent_id = ?
326  ' . ($pressId?' AND press_id = ?':''),
327  $params
328  );
329  return new DAOResultFactory($result, $this, '_fromRow');
330  }
331 
336  function getInsertId() {
337  return $this->_getInsertId('categories', 'category_id');
338  }
339 }
340 
341 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
Operations for retrieving and modifying Category objects.
updateLocaleFields($category)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
deleteByPressId($pressId)
getCountByPressId($pressId)
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:457
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
deleteObject($category)
getAffectedRows()
Definition: DAO.inc.php:258
static call($hookName, $args=null)
getByTitle($categoryTitle, $pressId, $locale=null)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
getById($categoryId, $pressId=null, $parentId=null)
Describes basic Category properties.
deleteById($categoryId, $pressId=null)
categoryExistsByPath($path)
updateObject($category)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
insertObject($category)
getByParentId($parentId, $pressId=null, $rangeInfo=null)
getByPath($path, $pressId)
getByPressId($pressId, $rangeInfo=null)