• Main Page
  • Modules
  • Classes
  • Files
  • File List

classes/press/CategoryDAO.inc.php

00001 <?php
00002 
00017 import ('classes.press.Category');
00018 
00019 class CategoryDAO extends DAO {
00023    function CategoryDAO() {
00024       parent::DAO();
00025    }
00026 
00034    function &getById($categoryId, $pressId = null, $parentId = null) {
00035       $params = array((int) $categoryId);
00036       if ($pressId) $params[] = (int) $pressId;
00037       if ($parentId) $params[] = (int) $parentId;
00038 
00039       $result =& $this->retrieve(
00040          'SELECT  *
00041          FROM  categories
00042          WHERE category_id = ?
00043          ' . ($pressId?' AND press_id = ?':'') . '
00044          ' . ($parentId?' AND parent_id = ?':''),
00045          $params
00046       );
00047 
00048       $returner = null;
00049       if ($result->RecordCount() != 0) {
00050          $returner =& $this->_fromRow($result->GetRowAssoc(false));
00051       }
00052 
00053       $result->Close();
00054       unset($result);
00055 
00056       return $returner;
00057    }
00058 
00065    function &getByPath($path, $pressId) {
00066       $returner = null;
00067       $result =& $this->retrieve(
00068          'SELECT * FROM categories WHERE path = ? AND press_id = ?',
00069          array((string) $path, (int) $pressId)
00070       );
00071 
00072       if ($result->RecordCount() != 0) {
00073          $returner =& $this->_fromRow($result->GetRowAssoc(false));
00074       }
00075 
00076       $result->Close();
00077       unset($result);
00078 
00079       return $returner;
00080    }
00081 
00089    function &getByTitle($categoryTitle, $pressId, $locale = null) {
00090       $params = array('title', $categoryTitle, (int) $pressId);
00091       if ($locale) $params[] = $locale;
00092 
00093       $result =& $this->retrieve(
00094          'SELECT  a.*
00095          FROM  categories a,
00096             category_settings l
00097          WHERE l.category_id = a.category_id AND
00098             l.setting_name = ? AND
00099             l.setting_value = ?
00100             AND a.press_id = ?
00101             ' . ($locale?' AND locale = ?':''),
00102          $params
00103       );
00104 
00105       $returner = null;
00106       if ($result->RecordCount() != 0) {
00107          $returner =& $this->_fromRow($result->GetRowAssoc(false));
00108       }
00109 
00110       $result->Close();
00111       unset($result);
00112 
00113       return $returner;
00114    }
00115 
00121    function categoryExistsByPath($path) {
00122       $result =& $this->retrieve(
00123          'SELECT COUNT(*) FROM categories WHERE path = ?', $path
00124       );
00125       $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
00126 
00127       $result->Close();
00128       unset($result);
00129 
00130       return $returner;
00131    }
00132 
00137    function newDataObject() {
00138       return new Category();
00139    }
00140 
00146    function _fromRow(&$row) {
00147       $category = $this->newDataObject();
00148 
00149       $category->setId($row['category_id']);
00150       $category->setPressId($row['press_id']);
00151       $category->setParentId($row['parent_id']);
00152       $category->setPath($row['path']);
00153       $category->setImage(unserialize($row['image']));
00154 
00155       $this->getDataObjectSettings('category_settings', 'category_id', $row['category_id'], $category);
00156 
00157       HookRegistry::call('CategoryDAO::_fromRow', array(&$category, &$row));
00158 
00159       return $category;
00160    }
00161 
00166    function getLocaleFieldNames() {
00167       return array('title', 'description');
00168    }
00169 
00174    function updateLocaleFields(&$category) {
00175       $this->updateDataObjectSettings(
00176          'category_settings', $category,
00177          array(
00178             'category_id' => $category->getId()
00179          )
00180       );
00181    }
00182 
00188    function insertObject(&$category) {
00189       $this->update(
00190          'INSERT INTO categories
00191             (press_id, parent_id, path, image)
00192             VALUES
00193             (?, ?, ?, ?)',
00194          array(
00195             (int) $category->getPressId(),
00196             (int) $category->getParentId(),
00197             $category->getPath(),
00198             serialize($category->getImage() ? $category->getImage() : array()),
00199          )
00200       );
00201 
00202       $category->setId($this->getInsertCategoryId());
00203       $this->updateLocaleFields($category);
00204       return $category->getId();
00205    }
00206 
00211    function updateObject($category) {
00212       $returner = $this->update(
00213          'UPDATE  categories
00214          SET   press_id = ?,
00215             parent_id = ?,
00216             path = ?,
00217             image = ?
00218          WHERE category_id = ?',
00219          array(
00220             (int) $category->getPressId(),
00221             (int) $category->getParentId(),
00222             $category->getPath(),
00223             serialize($category->getImage() ? $category->getImage() : array()),
00224             (int) $category->getId()
00225          )
00226       );
00227       $this->updateLocaleFields($category);
00228       return $returner;
00229    }
00230 
00235    function deleteObject(&$category) {
00236       return $this->deleteById(
00237          $category->getId(),
00238          $category->getPressId()
00239       );
00240    }
00241 
00247    function deleteById($categoryId, $pressId = null) {
00248       $params = array((int) $categoryId);
00249       if ($pressId) $params[] = (int) $pressId;
00250 
00251       $this->update(
00252          'DELETE FROM categories
00253          WHERE category_id = ?
00254             ' . ($pressId?' AND press_id = ?':''),
00255          $params
00256       );
00257 
00258       // If the category was deleted (this validates press_id,
00259       // if specified), delete any associated settings as well.
00260       if ($this->getAffectedRows()) {
00261          $this->update(
00262             'DELETE FROM category_settings WHERE category_id = ?',
00263             array((int) $categoryId)
00264          );
00265 
00266          // remove any monograph assignments for this category.
00267          $this->update(
00268             'DELETE FROM monograph_categories WHERE category_id = ?',
00269             array((int) $categoryId)
00270          );
00271       }
00272    }
00273 
00280    function deleteByPressId($pressId) {
00281       $categories =& $this->getByPressId($pressId);
00282       while ($category =& $categories->next()) {
00283          $this->deleteObject($category, $pressId);
00284          unset($category);
00285       }
00286    }
00287 
00292    function &getByPressId($pressId, $rangeInfo = null) {
00293       // The strange ORDER BY clause is to return subcategories
00294       // immediately after their parent category's entry.
00295       $result =& $this->retrieveRange(
00296          'SELECT  *
00297          FROM  categories
00298          WHERE press_id = ?
00299          ORDER BY CASE WHEN parent_id = 0 THEN category_id * 2 ELSE (parent_id * 2) + 1 END ASC',
00300          array((int) $pressId)
00301       );
00302 
00303       $returner = new DAOResultFactory($result, $this, '_fromRow');
00304       return $returner;
00305    }
00306 
00311    function &getCountByPressId($pressId) {
00312       $result =& $this->retrieve(
00313          'SELECT  COUNT(*)
00314          FROM  categories
00315          WHERE press_id = ?',
00316          (int) $pressId
00317       );
00318 
00319       $returner = $result->fields[0];
00320 
00321       $result->Close();
00322       unset($result);
00323       return $returner;
00324    }
00325 
00330    function &getByParentId($parentId, $pressId = null, $rangeInfo = null) {
00331       $params = array((int) $parentId);
00332       if ($pressId) $params[] = (int) $pressId;
00333 
00334       $result =& $this->retrieveRange(
00335          'SELECT  *
00336          FROM  categories
00337          WHERE parent_id = ?
00338          ' . ($pressId?' AND press_id = ?':''),
00339          $params
00340       );
00341 
00342       $returner = new DAOResultFactory($result, $this, '_fromRow');
00343       return $returner;
00344    }
00345 
00350    function getInsertCategoryId() {
00351       return $this->getInsertId('categories', 'category_id');
00352    }
00353 }
00354 
00355 ?>

Generated on Mon Sep 17 2012 13:58:55 for Open Monograph Press by  doxygen 1.7.1