1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
48: class ArticleObject extends AlphaDAO {
49: 50: 51: 52: 53: 54:
55: protected $title;
56:
57: 58: 59: 60: 61: 62:
63: protected $section;
64:
65: 66: 67: 68: 69: 70:
71: protected $description;
72:
73: 74: 75: 76: 77: 78:
79: protected $bodyOnload;
80:
81: 82: 83: 84: 85: 86:
87: protected ;
88:
89: 90: 91: 92: 93: 94:
95: protected $content;
96:
97: 98: 99: 100: 101: 102:
103: protected $author;
104:
105: 106: 107: 108: 109: 110:
111: protected $published;
112:
113: 114: 115: 116: 117: 118:
119: protected ;
120:
121: 122: 123: 124: 125: 126:
127: protected $votes;
128:
129: 130: 131: 132: 133: 134:
135: protected $tags;
136:
137: 138: 139: 140: 141: 142:
143: protected $taggedAttributes = array('title', 'description', 'content');
144:
145: 146: 147: 148: 149: 150:
151: private $filePath;
152:
153: 154: 155: 156: 157: 158:
159: protected $dataLabels = array("OID"=>"Article ID#","title"=>"Title","section"=>"Site Section","description"=>"Description","bodyOnload"=>"Body onload Javascript","content"=>"Content","headerContent"=>"HTML Header Content","author"=>"Author","created_ts"=>"Date Added","updated_ts"=>"Date of last Update","published"=>"Published","URL"=>"URL","printURL"=>"Printer version URL","comments"=>"Comments","votes"=>"Votes","tags"=>"Tags");
160:
161: 162: 163: 164: 165: 166:
167: const TABLE_NAME = 'Article';
168:
169: 170: 171: 172: 173: 174:
175: protected $URL;
176:
177: 178: 179: 180: 181: 182:
183: protected $printURL;
184:
185: 186: 187: 188: 189: 190:
191: private static $logger = null;
192:
193: 194: 195: 196: 197:
198: public function __construct() {
199: self::$logger = new Logger('ArticleObject');
200:
201:
202: parent::__construct();
203:
204: $this->title = new String();
205: $this->title->setHelper('Please provide a title for the article.');
206: $this->title->setSize(100);
207: $this->title->setRule("/\w+/");
208:
209: $this->section = new DEnum('ArticleObject::section');
210:
211: $this->description = new String();
212: $this->description->setHelper('Please provide a brief description of the article.');
213: $this->description->setSize(200);
214: $this->description->setRule("/\w+/");
215: $this->bodyOnload = new String();
216: $this->content = new Text();
217: $this->headerContent = new Text();
218: $this->author = new String();
219: $this->author->setHelper('Please state the name of the author of this article');
220: $this->author->setSize(70);
221: $this->author->setRule("/\w+/");
222: $this->published = new Boolean(0);
223:
224: $this->comments = new Relation();
225: $this->markTransient('comments');
226: $this->comments->setValue($this->OID);
227: $this->comments->setRelatedClass('ArticleCommentObject');
228: $this->comments->setRelatedClassField('articleOID');
229: $this->comments->setRelatedClassDisplayField('content');
230: $this->comments->setRelationType('ONE-TO-MANY');
231:
232: $this->votes = new Relation();
233: $this->markTransient('votes');
234: $this->votes->setValue($this->OID);
235: $this->votes->setRelatedClass('ArticleVoteObject');
236: $this->votes->setRelatedClassField('articleOID');
237: $this->votes->setRelatedClassDisplayField('score');
238: $this->votes->setRelationType('ONE-TO-MANY');
239:
240: $this->tags = new Relation();
241: $this->markTransient('tags');
242: $this->tags->setRelatedClass('TagObject');
243: $this->tags->setRelatedClassField('taggedOID');
244: $this->tags->setRelatedClassDisplayField('content');
245: $this->tags->setRelationType('ONE-TO-MANY');
246: $this->tags->setTaggedClass(get_class($this));
247: $this->tags->setValue($this->OID);
248:
249: $this->URL = '';
250: $this->printURL = '';
251:
252: $this->markTransient('URL');
253: $this->markTransient('printURL');
254:
255:
256: $this->markUnique('title');
257:
258: $this->markTransient('filePath');
259: $this->markTransient('taggedAttributes');
260: }
261:
262: 263: 264: 265: 266: 267:
268: protected function after_save_callback() {
269: if($this->getVersion() == 1 && $this->tags instanceof Relation) {
270:
271: $this->tags->setValue($this->OID);
272:
273: foreach($this->taggedAttributes as $tagged) {
274: $tags = TagObject::tokenize($this->get($tagged), get_class($this), $this->getOID());
275: foreach($tags as $tag) {
276: try {
277: $tag->save();
278: }catch(ValidationException $e){
279: 280: 281: 282:
283: }
284: }
285: }
286: }
287: }
288:
289: 290: 291: 292: 293:
294: protected function after_loadByAttribute_callback() {
295: $this->after_load_callback();
296: }
297:
298: 299: 300: 301: 302:
303: protected function after_load_callback() {
304: global $config;
305: global $front;
306:
307:
308: if($config->get('app.use.mod.rewrite')) {
309:
310: if(isset($front) && $front->hasAlias('ViewArticleTitle')) {
311: $alias = $front->getControllerAlias('ViewArticleTitle');
312:
313: $this->URL = $config->get('app.url').$alias.'/'.str_replace(' ', '-', $this->title->getValue());
314: }else{
315: $this->URL = $config->get('app.url').'ViewArticleTitle/title/'.str_replace(' ', $config->get('cms.url.title.separator'), $this->title->getValue());
316: }
317: }else{
318: $this->URL = $config->get('app.url').'alpha/controller/ViewArticle.php?oid='.$this->getID();
319: }
320:
321:
322: if($config->get('app.use.mod.rewrite')) {
323:
324: if(isset($front) && $front->hasAlias('ViewArticlePrint')) {
325: $alias = $front->getControllerAlias('ViewArticlePrint');
326:
327: $this->printURL = $config->get('app.url').$alias.'/'.str_replace(' ', '-', $this->title->getValue());
328: }else{
329: $this->printURL = $config->get('app.url').'ViewArticlePrint/title/'.str_replace(' ', $config->get('cms.url.title.separator'), $this->title->getValue());
330: }
331: }else{
332: $this->printURL = $config->get('app.url').'alpha/controller/ViewArticlePrint.php?title='.$this->title->getValue();
333: }
334:
335: $this->comments->setValue($this->OID);
336: $this->votes->setValue($this->OID);
337: }
338:
339: 340: 341: 342: 343: 344: 345: 346: 347: 348:
349: public function loadRecentWithLimit($limit, $excludeID = ''){
350:
351: if($excludeID != '') {
352: $denum = new DEnum('ArticleObject::section');
353: $excludeID = $denum->getOptionID($excludeID);
354: }
355:
356: $sqlQuery = "SELECT OID FROM ".$this->getTableName()." WHERE published='1' AND section!='$excludeID' ORDER BY created_ts DESC LIMIT 0, $limit;";
357:
358: $result = $this->query($sqlQuery);
359:
360: $OIDs = array();
361:
362: foreach($result as $row)
363: array_push($OIDs, $row['OID']);
364:
365: return $OIDs;
366: }
367:
368: 369: 370: 371: 372: 373:
374: public function getAttachmentsLocation() {
375: global $config;
376:
377: return $config->get('app.file.store.dir').'attachments/article_'.$this->getID();
378: }
379:
380: 381: 382: 383: 384: 385:
386: public function getAttachmentsURL() {
387: global $config;
388:
389: return $config->get('app.url').'attachments/article_'.$this->getID();
390: }
391:
392: 393: 394: 395: 396: 397:
398: public function getAttachmentSecureURL($filename) {
399: global $config;
400:
401: return FrontController::generateSecureURL('act=ViewAttachment&dir='.$this->getAttachmentsLocation().'&filename='.$filename);
402: }
403:
404: 405: 406: 407: 408: 409:
410: public function createAttachmentsFolder() {
411:
412: try{
413: mkdir($this->getAttachmentsLocation());
414: }catch (Exception $e) {
415: throw new AlphaException('Unable to create the folder ['.$this->getAttachmentsLocation().'] for the article.');
416: }
417:
418:
419: try{
420: chmod($this->getAttachmentsLocation(), 0777);
421: }catch (Exception $e) {
422: throw new AlphaException('Unable to set write permissions on the folder ['.$this->getAttachmentsLocation().'].');
423: }
424: }
425:
426: 427: 428: 429: 430: 431:
432: public function getArticleScore() {
433: $votes = $this->getArticleVotes();
434:
435: $score = 0;
436: $total_score = 0;
437: $vote_count = count($votes);
438:
439: for($i = 0; $i < $vote_count; $i++){
440: $total_score += $votes[$i]->get('score');
441: }
442:
443: if($vote_count > 0)
444: $score = $total_score/$vote_count;
445:
446: return sprintf("%01.2f", $score);
447: }
448:
449: 450: 451: 452: 453: 454:
455: public function getArticleVotes() {
456: $votes = $this->votes->getRelatedObjects();
457:
458: return $votes;
459: }
460:
461: 462: 463: 464: 465: 466: 467:
468: public function checkUserVoted() {
469:
470: if (!isset($_SESSION['currentUser']))
471: return true;
472:
473: $userID = $_SESSION['currentUser']->getID();
474:
475: $vote = new ArticleVoteObject();
476:
477: $sqlQuery = "SELECT COUNT(*) AS usersVote FROM ".$vote->getTableName()." WHERE articleOID='".$this->OID."' AND personOID='".$userID."';";
478:
479: $result = $this->query($sqlQuery);
480:
481: if(!isset($result[0])) {
482: throw new AlphaException('Failed to check if the current user voted for the article ['.$this->OID.'], query ['.$sqlQuery.']');
483: return false;
484: }
485:
486: $row = $result[0];
487:
488: if($row['usersVote'] == "0")
489: return false;
490: else
491: return true;
492: }
493:
494: 495: 496: 497: 498: 499:
500: public function getArticleComments() {
501: $comments = $this->comments->getRelatedObjects();
502:
503: return $comments;
504: }
505:
506: 507: 508: 509: 510: 511: 512:
513: public function loadContentFromFile($filePath) {
514: try{
515: $this->content->setValue(file_get_contents($filePath));
516: $this->filePath = $filePath;
517: }catch (Exception $e) {
518: throw new FileNotFoundException($e->getMessage());
519: }
520: }
521:
522: 523: 524: 525: 526: 527:
528: public function isLoadedFromFile() {
529: return ($this->filePath == '' ? false: true);
530: }
531:
532: 533: 534: 535: 536: 537: 538: 539:
540: public function getContentFileDate() {
541: if($this->filePath != '') {
542: try{
543: return date("Y-m-d H:i:s", filemtime($this->filePath));
544: }catch (Exception $e) {
545: throw new FileNotFoundException($e->getMessage());
546: }
547: }else{
548: throw new FileNotFoundException('Error trying to access an article content file when none is set!');
549: }
550: }
551: }
552:
553: ?>