00001 <?php
00002
00020 class TSDBook extends TSDObject {
00021 public static $ATTRIBUTES = array(
00022 'id' => false,
00023 'title' => 'edit',
00024 'gradeOrCourse' => 'edit',
00025 'coverImage' => false
00026 );
00027
00037 public function __construct($id = '') {
00038 parent::__construct('TSDBook', self::$ATTRIBUTES, 'id', $id);
00039 }
00040
00051 public function isValid() {
00052 return (
00053 ($this->id == '' || ctype_digit((string) $this->id)) &&
00054 (is_string($this->title) && $this->title != '') &&
00055 ($this->gradeOrCourse == '' || is_string($this->gradeOrCourse)) &&
00056 ($this->coverImage === null || ctype_digit((string) $this->coverImage))
00057 );
00058 }
00059
00075 public static function getList($constraints = array(), $limit = array(), $order = array(), $cnt = false) {
00076 return self::getListImpl('TSDBook', 'TSDBook', array_keys(self::$ATTRIBUTES), $constraints,
00077 array(), $limit, $order, $cnt);
00078 }
00079
00099 public function listChildren($constraints = array(), $limit = array(), $order = array(), $cnt = false) {
00100 return self::getListImpl('TSDChapter', 'TSDChapter', array_keys(TSDChapter::$ATTRIBUTES), $constraints,
00101 array('bookId' => $this->id), $limit, $order, $cnt);
00102 }
00103
00118 public function listValidChildren($constraints = array(), $limit = array(), $order = array(), $cnt = false) {
00119 $ljoin = 'LEFT JOIN TSDChapter ON TSDChapter.bookId = Book.id
00120 LEFT JOIN TSDSection ON TSDSection.chapterId = Chapter.id';
00121 return self::getListImpl('TSDChapter', 'TSDChapter', null, $constraints,
00122 array('bookId' => $this->id, 'IS NOT NULL', 'TSDSection.id' => 0),
00123 $limit, $order, $cnt, $ljoin);
00124 }
00125
00136 public function setCoverImage($snapPath) {
00137 if(!$this->checkInit('Cannot set cover image')) return null;
00138 if(!TSD::can('edit')) {
00139 $this->setError('Permission denied');
00140 return false;
00141 }
00142
00143 $img = SnapResource::lookup($snapPath);
00144
00145 if(!$img) {
00146 $this->setError('Could not find image with path \'' . $snapPath . '\'');
00147 return false;
00148 }
00149
00150 $this->setAttr('coverImage', $img->getId());
00151 return true;
00152 }
00153
00166 public function uploadCoverImage($tmpFile, $fileName = '') {
00167 if(!$this->checkInit('Cannot set cover image')) return null;
00168 if(!TSD::can('edit')) {
00169 $this->setError('Permission denied');
00170 return false;
00171 }
00172
00173 Snap2::reinit('admin');
00174
00175 if($fileName == '')
00176 $fileName = basename($tmpFile);
00177
00178 $basePath = SConfig::getDefault('tsd.snapPath');
00179 $imgPath = SConfig::getDefault('tsd.imageDir');
00180 $path = $basePath . $imgPath . '/';
00181 $imgDir = SnapDirectory::lookup($path);
00182 if(!$imgDir)
00183 $imgDir = SnapDirectory::lookup($basePath)->createDirectory($imgPath, "Textbook Images");
00184
00185 $sn = substr(preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->gradeOrCourse . $this->title), 0, 64);
00186 $img = SnapResource::lookup($path . $sn);
00187 if(!$img)
00188 $img = $imgDir->createResource($sn, SnapResource::TYPE_MEDIA_IMAGE, $this->title);
00189 if(!$img) {
00190 $this->setError('Could not create image resource: ' . $imgDir->getLastError());
00191 return false;
00192 }
00193
00194 $v = $img->createVersion();
00195 $cm = new SnapContentMediaImage($v);
00196
00197 if(!$cm->uploadImage($tmpFile, $fileName, array('thumbSize' => array('width' => 159.0, 'height' => 100.0),
00198 'thumbCropScale' => true, 'medium_resize' => 'standard', 'display_resize' => 'standard')))
00199 {
00200 $cm->delete();
00201 $v->defunct();
00202 $v->destroy();
00203 $this->setError('Could not upload image: ' . $cm->getLastError());
00204 return false;
00205 }
00206
00207 if(!$v->update($cm)) {
00208 $cm->delete();
00209 $v->defunct();
00210 $v->destroy();
00211 $this->setError('Could not update image: ' . $v->getLastError());
00212 return false;
00213 }
00214
00215 $v->submit();
00216 $v->approveForDev();
00217
00218 $this->setAttr('coverImage', $img->getId());
00219
00220 return true;
00221 }
00222
00231 public function addChapter($name) {
00232 if(!$this->checkInit('Cannot add chapter')) return null;
00233 if(!TSD::can('edit')) {
00234 $this->setError('Permission denied');
00235 return false;
00236 }
00237
00238 $chapter = new TSDChapter();
00239 $chapter->setAttr('name', $name);
00240 $chapter->setAttr('bookId', $this->id);
00241 if(!$chapter->commit())
00242 return null;
00243
00244 return $chapter;
00245 }
00246
00256 public function deleteChapterByName($name) {
00257 if(!$this->checkInit('Cannot delete chapter(s)')) return null;
00258 if(!TSD::can('edit')) {
00259 $this->setError('Permission denied');
00260 return false;
00261 }
00262
00263 $chapters = $this->listChildren(array('name' => $name));
00264 foreach($chapters as $c)
00265 if(!$c->delete())
00266 return false;
00267 return true;
00268 }
00269
00277 public function getParent() {
00278 return null;
00279 }
00280
00290 public function getIdentifier() {
00291 return $this->title . ' ' . $this->gradeOrCourse;
00292 }
00293
00301 public function delete() {
00302 $imgID = $this->coverImage;
00303 if(parent::delete()) {
00304 Snap2::reinit('admin');
00305 $img = SnapResource::retrieve($imgID);
00306 if(!$img->delete()) {
00307 foreach($img->getError() as $err)
00308 $this->setError($err);
00309 return false;
00310 }
00311 return true;
00312 }
00313 else
00314 return false;
00315 }
00316 }
00317
00318 ?>