00001 <?php
00002
00016
00017
00018
00019 import('article.ArticleGalley');
00020 import('article.ArticleHTMLGalley');
00021
00022 class ArticleGalleyDAO extends DAO {
00024 var $articleFileDao;
00025
00029 function ArticleGalleyDAO() {
00030 parent::DAO();
00031 $this->articleFileDao = &DAORegistry::getDAO('ArticleFileDAO');
00032 }
00033
00040 function &getGalley($galleyId, $articleId = null) {
00041 $params = array($galleyId);
00042 if ($articleId !== null) $params[] = (int) $articleId;
00043 $result = &$this->retrieve(
00044 'SELECT g.*,
00045 a.file_name, a.original_file_name, a.file_type, a.file_size, a.status, a.date_uploaded, a.date_modified
00046 FROM article_galleys g
00047 LEFT JOIN article_files a ON (g.file_id = a.file_id)
00048 WHERE g.galley_id = ?' .
00049 ($articleId !== null?' AND g.article_id = ?':''),
00050 $params
00051 );
00052
00053 $returner = null;
00054 if ($result->RecordCount() != 0) {
00055 $returner = &$this->_returnGalleyFromRow($result->GetRowAssoc(false));
00056 } else {
00057 HookRegistry::call('ArticleGalleyDAO::getNewGalley', array(&$galleyId, &$articleId, &$returner));
00058 }
00059
00060 $result->Close();
00061 unset($result);
00062
00063 return $returner;
00064 }
00065
00073 function publicGalleyIdExists($publicGalleyId, $galleyId) {
00074 $result = &$this->retrieve(
00075 'SELECT COUNT(*) FROM article_galleys WHERE public_galley_id = ? AND galley_id <> ?', array($publicGalleyId, $galleyId)
00076 );
00077 $returner = $result->fields[0] ? true : false;
00078
00079 $result->Close();
00080 unset($result);
00081
00082 return $returner;
00083 }
00084
00091 function &getGalleyByPublicGalleyId($publicGalleyId, $articleId) {
00092 $result = &$this->retrieve(
00093 'SELECT g.*,
00094 a.file_name, a.original_file_name, a.file_type, a.file_size, a.status, a.date_uploaded, a.date_modified
00095 FROM article_galleys g
00096 LEFT JOIN article_files a ON (g.file_id = a.file_id)
00097 WHERE g.public_galley_id = ? AND
00098 g.article_id = ?',
00099 array($publicGalleyId, (int) $articleId)
00100 );
00101
00102 $returner = null;
00103 if ($result->RecordCount() != 0) {
00104 $returner = &$this->_returnGalleyFromRow($result->GetRowAssoc(false));
00105 } else {
00106 HookRegistry::call('ArticleGalleyDAO::getNewGalley', array(&$galleyId, &$articleId, &$returner));
00107 }
00108
00109 $result->Close();
00110 unset($result);
00111
00112 return $returner;
00113 }
00114
00120 function &getGalleysByArticle($articleId) {
00121 $galleys = array();
00122
00123 $result = &$this->retrieve(
00124 'SELECT g.*,
00125 a.file_name, a.original_file_name, a.file_type, a.file_size, a.status, a.date_uploaded, a.date_modified
00126 FROM article_galleys g
00127 LEFT JOIN article_files a ON (g.file_id = a.file_id)
00128 WHERE g.article_id = ? ORDER BY g.seq',
00129 $articleId
00130 );
00131
00132 while (!$result->EOF) {
00133 $galleys[] = &$this->_returnGalleyFromRow($result->GetRowAssoc(false));
00134 $result->moveNext();
00135 }
00136
00137 $result->Close();
00138 unset($result);
00139
00140 HookRegistry::call('ArticleGalleyDAO::getGalleysByArticle', array(&$galleys, &$articleId));
00141
00142 return $galleys;
00143 }
00144
00152 function &getGalleyByBestGalleyId($galleyId, $articleId) {
00153 if ($galleyId != '') $galley =& $this->getGalleyByPublicGalleyId($galleyId, $articleId);
00154 if (!isset($galley)) $galley =& $this->getGalley((int) $galleyId, $articleId);
00155 return $galley;
00156 }
00157
00163 function &_returnGalleyFromRow(&$row) {
00164 if ($row['html_galley']) {
00165 $galley = &new ArticleHTMLGalley();
00166
00167
00168 $galley->setStyleFileId($row['style_file_id']);
00169 if ($row['style_file_id']) {
00170 $galley->setStyleFile($this->articleFileDao->getArticleFile($row['style_file_id']));
00171 }
00172
00173
00174 $images = &$this->getGalleyImages($row['galley_id']);
00175 $galley->setImageFiles($images);
00176
00177 } else {
00178 $galley = &new ArticleGalley();
00179 }
00180 $galley->setGalleyId($row['galley_id']);
00181 $galley->setPublicGalleyId($row['public_galley_id']);
00182 $galley->setArticleId($row['article_id']);
00183 $galley->setLocale($row['locale']);
00184 $galley->setFileId($row['file_id']);
00185 $galley->setLabel($row['label']);
00186 $galley->setSequence($row['seq']);
00187 $galley->setViews($row['views']);
00188
00189
00190 $galley->setFileName($row['file_name']);
00191 $galley->setOriginalFileName($row['original_file_name']);
00192 $galley->setFileType($row['file_type']);
00193 $galley->setFileSize($row['file_size']);
00194 $galley->setStatus($row['status']);
00195 $galley->setDateModified($this->datetimeFromDB($row['date_modified']));
00196 $galley->setDateUploaded($this->datetimeFromDB($row['date_uploaded']));
00197
00198 HookRegistry::call('ArticleGalleyDAO::_returnGalleyFromRow', array(&$galley, &$row));
00199
00200 return $galley;
00201 }
00202
00207 function insertGalley(&$galley) {
00208 $this->update(
00209 'INSERT INTO article_galleys
00210 (public_galley_id, article_id, file_id, label, locale, html_galley, style_file_id, seq)
00211 VALUES
00212 (?, ?, ?, ?, ?, ?, ?, ?)',
00213 array(
00214 $galley->getPublicGalleyId(),
00215 $galley->getArticleId(),
00216 $galley->getFileId(),
00217 $galley->getLabel(),
00218 $galley->getLocale(),
00219 (int)$galley->isHTMLGalley(),
00220 $galley->isHTMLGalley() ? $galley->getStyleFileId() : null,
00221 $galley->getSequence() == null ? $this->getNextGalleySequence($galley->getArticleID()) : $galley->getSequence()
00222 )
00223 );
00224 $galley->setGalleyId($this->getInsertGalleyId());
00225
00226 HookRegistry::call('ArticleGalleyDAO::insertNewGalley', array(&$galley, $galley->getGalleyId()));
00227
00228 return $galley->getGalleyId();
00229 }
00230
00235 function updateGalley(&$galley) {
00236 return $this->update(
00237 'UPDATE article_galleys
00238 SET
00239 public_galley_id = ?,
00240 file_id = ?,
00241 label = ?,
00242 locale = ?,
00243 html_galley = ?,
00244 style_file_id = ?,
00245 seq = ?
00246 WHERE galley_id = ?',
00247 array(
00248 $galley->getPublicGalleyId(),
00249 $galley->getFileId(),
00250 $galley->getLabel(),
00251 $galley->getLocale(),
00252 (int)$galley->isHTMLGalley(),
00253 $galley->isHTMLGalley() ? $galley->getStyleFileId() : null,
00254 $galley->getSequence(),
00255 $galley->getGalleyId()
00256 )
00257 );
00258 }
00259
00264 function deleteGalley(&$galley) {
00265 return $this->deleteGalleyById($galley->getGalleyId());
00266 }
00267
00273 function deleteGalleyById($galleyId, $articleId = null) {
00274
00275 HookRegistry::call('ArticleGalleyDAO::deleteGalleyById', array(&$galleyId, &$articleId));
00276
00277 $this->deleteImagesByGalley($galleyId);
00278 if (isset($articleId)) {
00279 return $this->update(
00280 'DELETE FROM article_galleys WHERE galley_id = ? AND article_id = ?',
00281 array($galleyId, $articleId)
00282 );
00283
00284 } else {
00285 return $this->update(
00286 'DELETE FROM article_galleys WHERE galley_id = ?', $galleyId
00287 );
00288 }
00289 }
00290
00296 function deleteGalleysByArticle($articleId) {
00297 $galleys = &$this->getGalleysByArticle($articleId);
00298 foreach ($galleys as $galley) {
00299 $this->deleteGalleyById($galley->getGalleyId(), $articleId);
00300 }
00301 }
00302
00309 function galleyExistsByFileId($articleId, $fileId) {
00310 $result = &$this->retrieve(
00311 'SELECT COUNT(*) FROM article_galleys
00312 WHERE article_id = ? AND file_id = ?',
00313 array($articleId, $fileId)
00314 );
00315
00316 $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
00317
00318 $result->Close();
00319 unset($result);
00320
00321 return $returner;
00322 }
00323
00328 function incrementViews($galleyId) {
00329 if ( !HookRegistry::call('ArticleGalleyDAO::incrementGalleyViews', array(&$galleyId)) ) {
00330 return $this->update(
00331 'UPDATE article_galleys SET views = views + 1 WHERE galley_id = ?',
00332 $galleyId
00333 );
00334 } else return false;
00335 }
00336
00341 function resequenceGalleys($articleId) {
00342 $result = &$this->retrieve(
00343 'SELECT galley_id FROM article_galleys WHERE article_id = ? ORDER BY seq',
00344 $articleId
00345 );
00346
00347 for ($i=1; !$result->EOF; $i++) {
00348 list($galleyId) = $result->fields;
00349 $this->update(
00350 'UPDATE article_galleys SET seq = ? WHERE galley_id = ?',
00351 array($i, $galleyId)
00352 );
00353 $result->moveNext();
00354 }
00355
00356 $result->close();
00357 unset($result);
00358 }
00359
00365 function getNextGalleySequence($articleId) {
00366 $result = &$this->retrieve(
00367 'SELECT MAX(seq) + 1 FROM article_galleys WHERE article_id = ?',
00368 $articleId
00369 );
00370 $returner = floor($result->fields[0]);
00371
00372 $result->Close();
00373 unset($result);
00374
00375 return $returner;
00376 }
00377
00382 function getInsertGalleyId() {
00383 return $this->getInsertId('article_galleys', 'galley_id');
00384 }
00385
00386
00387
00388
00389
00390
00396 function &getGalleyImages($galleyId) {
00397 $images = array();
00398
00399 $result = &$this->retrieve(
00400 'SELECT a.* FROM article_html_galley_images i, article_files a
00401 WHERE i.file_id = a.file_id AND i.galley_id = ?',
00402 $galleyId
00403 );
00404
00405 while (!$result->EOF) {
00406 $images[] = &$this->articleFileDao->_returnArticleFileFromRow($result->GetRowAssoc(false));
00407 $result->MoveNext();
00408 }
00409
00410 $result->Close();
00411 unset($result);
00412
00413 return $images;
00414 }
00415
00421 function insertGalleyImage($galleyId, $fileId) {
00422 return $this->update(
00423 'INSERT INTO article_html_galley_images
00424 (galley_id, file_id)
00425 VALUES
00426 (?, ?)',
00427 array($galleyId, $fileId)
00428 );
00429 }
00430
00436 function deleteGalleyImage($galleyId, $fileId) {
00437 return $this->update(
00438 'DELETE FROM article_html_galley_images
00439 WHERE galley_id = ? AND file_id = ?',
00440 array($galleyId, $fileId)
00441 );
00442 }
00443
00448 function deleteImagesByGalley($galleyId) {
00449 return $this->update(
00450 'DELETE FROM article_html_galley_images WHERE galley_id = ?',
00451 $galleyId
00452 );
00453 }
00454 }
00455
00456 ?>