Open Journal Systems  3.3.0
CategoryDAO.inc.php
1 <?php
2 
17 import ('lib.pkp.classes.context.Category');
18 
19 class CategoryDAO extends DAO {
27  function getById($categoryId, $contextId = null, $parentId = null) {
28  $params = array((int) $categoryId);
29  if ($contextId) $params[] = (int) $contextId;
30  if ($parentId) $params[] = (int) $parentId;
31 
32  $result = $this->retrieve(
33  'SELECT *
34  FROM categories
35  WHERE category_id = ?
36  ' . ($contextId?' AND context_id = ?':'') . '
37  ' . ($parentId?' AND parent_id = ?':''),
38  $params
39  );
40 
41  $returner = null;
42  if ($result->RecordCount() != 0) {
43  $returner = $this->_fromRow($result->GetRowAssoc(false));
44  }
45  $result->Close();
46  return $returner;
47  }
48 
55  function getByPath($path, $contextId) {
56  $returner = null;
57  $result = $this->retrieve(
58  'SELECT * FROM categories WHERE path = ? AND context_id = ?',
59  array((string) $path, (int) $contextId)
60  );
61 
62  if ($result->RecordCount() != 0) {
63  $returner = $this->_fromRow($result->GetRowAssoc(false));
64  }
65 
66  $result->Close();
67  return $returner;
68  }
69 
77  function getByTitle($categoryTitle, $contextId, $locale = null) {
78  $params = array('title', $categoryTitle, (int) $contextId);
79  if ($locale) $params[] = $locale;
80 
81  $result = $this->retrieve(
82  'SELECT a.*
83  FROM categories a,
84  category_settings l
85  WHERE l.category_id = a.category_id AND
86  l.setting_name = ? AND
87  l.setting_value = ?
88  AND a.context_id = ?
89  ' . ($locale?' AND locale = ?':'') . '
90  ORDER BY seq',
91  $params
92  );
93 
94  $returner = null;
95  if ($result->RecordCount() != 0) {
96  $returner = $this->_fromRow($result->GetRowAssoc(false));
97  }
98 
99  $result->Close();
100  return $returner;
101  }
102 
109  public function getByPublicationId($publicationId) {
110  $result = $this->retrieve(
111  'SELECT c.*
112  FROM categories c
113  INNER JOIN publication_categories pc ON (pc.category_id = c.category_id)
114  WHERE pc.publication_id = ?',
115  (int) $publicationId
116  );
117 
118  return new DAOResultFactory($result, $this, '_fromRow');
119  }
120 
126  function categoryExistsByPath($path) {
127  $result = $this->retrieve(
128  'SELECT COUNT(*) FROM categories WHERE path = ?', $path
129  );
130  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
131 
132  $result->Close();
133  return $returner;
134  }
135 
140  function newDataObject() {
141  return new Category();
142  }
143 
149  function _fromRow($row) {
150  $category = $this->newDataObject();
151 
152  $category->setId($row['category_id']);
153  $category->setContextId($row['context_id']);
154  $category->setParentId($row['parent_id']);
155  $category->setPath($row['path']);
156  $category->setImage(unserialize($row['image']));
157  $category->setSequence($row['seq']);
158 
159  $this->getDataObjectSettings('category_settings', 'category_id', $row['category_id'], $category);
160 
161  HookRegistry::call('CategoryDAO::_fromRow', array(&$category, &$row));
162 
163  return $category;
164  }
165 
170  function getLocaleFieldNames() {
171  return array('title', 'description');
172  }
173 
179  return array_merge(
180  parent::getAdditionalFieldNames(),
181  array(
182  'sortOption',
183  )
184  );
185  }
186 
191  function updateLocaleFields($category) {
193  'category_settings', $category,
194  array(
195  'category_id' => $category->getId()
196  )
197  );
198  }
199 
205  function insertObject($category) {
206  $this->update(
207  'INSERT INTO categories
208  (context_id, parent_id, path, image, seq)
209  VALUES
210  (?, ?, ?, ?, ?)',
211  array(
212  (int) $category->getContextId(),
213  (int) $category->getParentId(),
214  $category->getPath(),
215  serialize($category->getImage() ? $category->getImage() : array()),
216  (int) $category->getSequence()
217  )
218  );
219 
220  $category->setId($this->getInsertId());
221  $this->updateLocaleFields($category);
222  return $category->getId();
223  }
224 
229  function updateObject($category) {
230  $returner = $this->update(
231  'UPDATE categories
232  SET context_id = ?,
233  parent_id = ?,
234  path = ?,
235  image = ?,
236  seq = ?
237  WHERE category_id = ?',
238  array(
239  (int) $category->getContextId(),
240  (int) $category->getParentId(),
241  $category->getPath(),
242  serialize($category->getImage() ? $category->getImage() : array()),
243  (int) $category->getSequence(),
244  (int) $category->getId()
245  )
246  );
247  $this->updateLocaleFields($category);
248  return $returner;
249  }
250 
256  function resequenceCategories($contextId, $parentCategoryId = null) {
257  $params = array((int) $contextId);
258  if ($parentCategoryId) $params[] = (int) $parentCategoryId;
259  $result = $this->retrieve(
260  'SELECT category_id FROM categories WHERE context_id = ?' .
261  ($parentCategoryId?' AND parent_id = ?':''),
262  $params
263  );
264 
265  for ($i=1; !$result->EOF; $i++) {
266  list($categoryId) = $result->fields;
267  $this->update(
268  'UPDATE categories SET seq = ? WHERE category_id = ?',
269  array(
270  (int) $i,
271  (int) $categoryId
272  )
273  );
274 
275  $result->MoveNext();
276  }
277 
278  $result->Close();
279  }
280 
285  function deleteObject($category) {
286  return $this->deleteById(
287  $category->getId(),
288  $category->getContextId()
289  );
290  }
291 
297  function deleteById($categoryId, $contextId = null) {
298  $params = array((int) $categoryId);
299  if ($contextId) $params[] = (int) $contextId;
300 
301  $this->update(
302  'DELETE FROM categories
303  WHERE category_id = ?
304  ' . ($contextId?' AND context_id = ?':''),
305  $params
306  );
307 
308  // If the category was deleted (this validates context_id,
309  // if specified), delete any associated settings as well.
310  if ($this->getAffectedRows()) {
311  $this->update(
312  'DELETE FROM category_settings WHERE category_id = ?',
313  array((int) $categoryId)
314  );
315 
316  // remove any monograph assignments for this category.
317  $this->update(
318  'DELETE FROM publication_categories WHERE category_id = ?',
319  array((int) $categoryId)
320  );
321  }
322  }
323 
330  function deleteByContextId($contextId) {
331  $categories = $this->getByContextId($contextId);
332  while ($category = $categories->next()) {
333  $this->deleteObject($category, $contextId);
334  }
335  }
336 
343  public function insertPublicationAssignment($categoryId, $publicationId) {
344  $this->update(
345  'INSERT INTO publication_categories (category_id, publication_id)
346  VALUES (?, ?)',
347  array((int) $categoryId, (int) $publicationId)
348  );
349  }
350 
356  public function deletePublicationAssignments($publicationId) {
357  $this->update(
358  'DELETE FROM publication_categories WHERE publication_id = ?',
359  array((int) $publicationId)
360  );
361  }
362 
369  function getByContextId($contextId, $rangeInfo = null) {
370  // The strange ORDER BY clause is to return subcategories
371  // immediately after their parent category's entry.
372  $result = $this->retrieveRange(
373  'SELECT c.*
374  FROM categories c
375  LEFT JOIN categories pc ON (pc.category_id = c.parent_id)
376  WHERE c.context_id = ?
377  ORDER BY (COALESCE((pc.seq * 8192) + pc.category_id, 0) * 8192) + CASE WHEN pc.category_id IS NULL THEN 8192 * ((c.seq * 8192) + c.category_id) ELSE c.seq END',
378  array((int) $contextId)
379  );
380 
381  return new DAOResultFactory($result, $this, '_fromRow');
382  }
383 
389  function getCountByContextId($contextId) {
390  $result = $this->retrieve(
391  'SELECT COUNT(*)
392  FROM categories
393  WHERE context_id = ?',
394  (int) $contextId
395  );
396 
397  $returner = $result->fields[0];
398  $result->Close();
399  return $returner;
400  }
401 
409  function getByParentId($parentId, $contextId = null, $rangeInfo = null) {
410  $params = array((int) $parentId);
411  if ($contextId) $params[] = (int) $contextId;
412 
413  $result = $this->retrieveRange(
414  'SELECT *
415  FROM categories
416  WHERE parent_id = ?
417  ' . ($contextId?' AND context_id = ?':'') . '
418  ORDER BY seq',
419  $params
420  );
421  return new DAOResultFactory($result, $this, '_fromRow');
422  }
423 
428  function getInsertId() {
429  return $this->_getInsertId('categories', 'category_id');
430  }
431 }
432 
433 
CategoryDAO\getByParentId
getByParentId($parentId, $contextId=null, $rangeInfo=null)
Definition: CategoryDAO.inc.php:409
CategoryDAO\getByTitle
getByTitle($categoryTitle, $contextId, $locale=null)
Definition: CategoryDAO.inc.php:77
CategoryDAO\updateLocaleFields
updateLocaleFields($category)
Definition: CategoryDAO.inc.php:191
CategoryDAO\deleteByContextId
deleteByContextId($contextId)
Definition: CategoryDAO.inc.php:330
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
DAO\retrieveRange
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
Category
Describes basic Category properties.
Definition: Category.inc.php:17
CategoryDAO\getAdditionalFieldNames
getAdditionalFieldNames()
Definition: CategoryDAO.inc.php:178
DAO\getAffectedRows
getAffectedRows()
Definition: DAO.inc.php:264
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
CategoryDAO\getInsertId
getInsertId()
Definition: CategoryDAO.inc.php:428
CategoryDAO\getById
getById($categoryId, $contextId=null, $parentId=null)
Definition: CategoryDAO.inc.php:27
CategoryDAO\getLocaleFieldNames
getLocaleFieldNames()
Definition: CategoryDAO.inc.php:170
CategoryDAO\deleteObject
deleteObject($category)
Definition: CategoryDAO.inc.php:285
CategoryDAO\deletePublicationAssignments
deletePublicationAssignments($publicationId)
Definition: CategoryDAO.inc.php:356
CategoryDAO
Operations for retrieving and modifying Category objects.
Definition: CategoryDAO.inc.php:19
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
CategoryDAO\deleteById
deleteById($categoryId, $contextId=null)
Definition: CategoryDAO.inc.php:297
CategoryDAO\getByContextId
getByContextId($contextId, $rangeInfo=null)
Definition: CategoryDAO.inc.php:369
DAO\_getInsertId
_getInsertId($table='', $id='')
Definition: DAO.inc.php:255
CategoryDAO\updateObject
updateObject($category)
Definition: CategoryDAO.inc.php:229
DAO\getDataObjectSettings
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, $dataObject)
Definition: DAO.inc.php:582
CategoryDAO\getByPath
getByPath($path, $contextId)
Definition: CategoryDAO.inc.php:55
CategoryDAO\insertPublicationAssignment
insertPublicationAssignment($categoryId, $publicationId)
Definition: CategoryDAO.inc.php:343
CategoryDAO\newDataObject
newDataObject()
Definition: CategoryDAO.inc.php:140
CategoryDAO\categoryExistsByPath
categoryExistsByPath($path)
Definition: CategoryDAO.inc.php:126
DAO\updateDataObjectSettings
updateDataObjectSettings($tableName, $dataObject, $idArray)
Definition: DAO.inc.php:488
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
CategoryDAO\_fromRow
_fromRow($row)
Definition: CategoryDAO.inc.php:149
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
CategoryDAO\insertObject
insertObject($category)
Definition: CategoryDAO.inc.php:205
CategoryDAO\getCountByContextId
getCountByContextId($contextId)
Definition: CategoryDAO.inc.php:389
CategoryDAO\resequenceCategories
resequenceCategories($contextId, $parentCategoryId=null)
Definition: CategoryDAO.inc.php:256
CategoryDAO\getByPublicationId
getByPublicationId($publicationId)
Definition: CategoryDAO.inc.php:109