00001 <?php
00002
00016 class TSDChapter extends TSDObject {
00017 public static $ATTRIBUTES = array(
00018 'id' => false,
00019 'name' => 'edit',
00020 'bookId' => false
00021 );
00022
00032 public function __construct($id = '') {
00033 parent::__construct('TSDChapter', self::$ATTRIBUTES, 'id', $id);
00034 }
00035
00045 public function isValid() {
00046 return (
00047 ($this->id == '' || ctype_digit((string) $this->id)) &&
00048 (is_string($this->name) && $this->name != '') &&
00049 (ctype_digit((string) $this->bookId) && $this->bookId > 0)
00050 );
00051 }
00052
00068 public static function getList($constraints = array(), $limit = array(), $order = array(), $cnt = false) {
00069 return self::getListImpl('TSDChapter', 'TSDChapter', array_keys(self::$ATTRIBUTES), $constraints,
00070 array(), $limit, $order, $cnt);
00071 }
00072
00094 public function listChildren($constraints = array(), $limit = array(), $order = array(), $cnt = false, $withAlCnt = false) {
00095 if($withAlCnt) {
00096 if(SConfig::getDefault('common.serverClass') == 'dev')
00097 $v = 'DEV';
00098 else
00099 $v = 'LIVE';
00100 $keys = array_merge(array_keys(TSDSection::$ATTRIBUTES),
00101 array('(SELECT COUNT(*) FROM TSDTextbookAlignment WHERE sectionId = id AND version=\''
00102 . $v . '\') AS numAlignments'));
00103 return self::getListImpl('TSDSection', 'TSDSection', $keys,
00104 $constraints, array('chapterId' => $this->id), $limit, $order, $cnt);
00105 }
00106 else
00107 return self::getListImpl('TSDSection', 'TSDSection', array_keys(TSDSection::$ATTRIBUTES), $constraints,
00108 array('chapterId' => $this->id), $limit, $order, $cnt);
00109 }
00110
00122 public function listValidChildren($constraints = array(), $limit = array(), $order = array(), $cnt = false) {
00123 return null;
00124 }
00125
00131 public function getParent() {
00132 if(!$this->checkInit('Retrieve book')) return null;
00133
00134 $book = new TSDBook($this->bookId);
00135 if(!$book->isValid())
00136 return null;
00137 else
00138 return $book;
00139 }
00140
00149 public function getIdentifier() {
00150 return $this->name;
00151 }
00152
00162 public function addSection($name, $description = '') {
00163 if(!$this->checkInit('Cannot add section')) return null;
00164 if(!TSD::can('edit')) {
00165 $this->setError('Permission denied');
00166 return false;
00167 }
00168
00169 $section = new TSDSection();
00170 $section->setAttr('name', $name);
00171 $section->setAttr('description', $description);
00172 $section->setAttr('chapterId', $this->id);
00173 if(!$section->commit())
00174 return null;
00175
00176 return $section;
00177 }
00178
00188 public function deleteSectionByName($name) {
00189 if(!$this->checkInit('Cannot delete section(s)')) return null;
00190 if(!TSD::can('edit')) {
00191 $this->setError('Permission denied');
00192 return false;
00193 }
00194
00195 $sections = $this->listChildren(array('name' => $name));
00196 foreach($sections as $s)
00197 if(!$s->delete())
00198 return false;
00199 return true;
00200 }
00201 }
00202
00203 ?>