00001 <?
00002
00003 require_once("lib/db.inc.php");
00004
00013 abstract class BaseObject extends MyModule
00014 {
00019 public $tableName;
00020
00024 private $dirtyFields = array();
00025
00029 protected $data = array();
00030
00034 public $creator;
00035
00039 public $comments;
00040
00044 public $commentsEnabled = false;
00045
00049 public $useLatLng = false;
00050
00054 public $useObjectCaching = false;
00055
00059 public $useTags = false;
00060
00064 public $tags;
00065
00069 public $hasAuthor = true;
00070
00074 private $internalId;
00075
00079 public static $objectCacheLife = 3000000;
00080
00084 public $fullTextFields = array();
00085
00091 public $likeFields = array();
00092
00101 public function __construct($data, $tableName)
00102 {
00103
00104 parent::__construct();
00105
00106
00107 $this->tableName = $tableName;
00108
00109
00110 $this->checkTable();
00111
00112
00113 $this->load($data);
00114
00115
00116 if ($this->useLatLng)
00117 {
00118 $this->fullTextFields[] = 'city';
00119 $this->fullTextFields[] = 'state';
00120 }
00121 }
00122
00126 public function __destruct()
00127 {
00128 }
00129
00134 public function __toString()
00135 {
00136 return $this->getName();
00137 }
00138
00144 public function __get($name)
00145 {
00146 if ($name == 'id')
00147 return $this->internalId;
00148 else if ($this->hasField($name))
00149 return $this->data[$name];
00150 else
00151 return $this->$name;
00152 }
00153
00159 public function __set($name, $value)
00160 {
00161
00162 if ($name == 'id')
00163 {
00164 if ($value != $this->internalId)
00165 {
00166
00167 $this->internalId = $value;
00168 $this->__construct($value);
00169 }
00170 }
00171
00172 else if ($this->hasField($name))
00173 {
00174
00175 if ($this->data[$name] != $value || !isset($this->data['name']))
00176 {
00177
00178
00179
00180
00181
00182
00183 $this->dirtyFields[$name] = (string)$name;
00184 $this->data[$name] = $value;
00185 }
00186 }
00187
00188 else
00189 $this->$name = $value;
00190 }
00191
00197 protected function initEditPage($title = null, $data = null)
00198 {
00199
00200 if ($data === null)
00201 $this->id = $this->params('id');
00202 else
00203 $this->data = $data;
00204
00205
00206 if ($title === null && $this->id)
00207 $this->pageTitle = 'Edit ' . ucfirst(strtolower($this->english)) . ' - ' . $this->__toString();
00208 else if ($this->pagetitle === null && !$this->id)
00209 $this->pageTitle = 'Add New ' . ucfirst(strtolower($this->english));
00210 else
00211 $this->pageTitle = $title;
00212
00213 if (!$this->canEdit())
00214 throw new PageError("You do not have access to edit this $this->english.");
00215
00216 if ($this->useTags)
00217 $this->tagFactory();
00218 }
00219
00224 protected function initDeletePage($title = null)
00225 {
00226
00227 $this->id = $this->params('id');
00228
00229
00230 if ($title === null)
00231 $title = 'Delete ' . ucfirst(strtolower($this->english));
00232 $this->pageTitle = $title;
00233
00234 if (!$this->canDelete())
00235 throw new PageError("You are not authorized to delete this $this->english.");
00236 }
00237
00242 protected function initViewPage($title = null)
00243 {
00244
00245 $this->id = $this->params('id');
00246
00247
00248 if ($title === null)
00249 $this->pageTitle = $this->getName();
00250 else
00251 $this->pageTitle = $title;
00252
00253 if (!$this->canView())
00254 throw new PageError("You do not have permission to view this $this->english.");
00255
00256 if ($this->commentsEnabled)
00257 {
00258 $this->needsJs('lib/js/comment.js');
00259 $this->needsJs('lib/js/prototype-1.5.0.js');
00260 }
00261 }
00262
00266 public function initJSONPage()
00267 {
00268 require_once("lib/json.inc.php");
00269
00270 $this->initViewPage();
00271
00272 $this->setTemplate(new BlankTemplate());
00273 }
00274
00278 public function drawJSONPage()
00279 {
00280 echo JSON::encode($this->getPublicData());
00281 }
00282
00286 public function initPHPPage()
00287 {
00288 $this->initViewPage();
00289
00290 $this->setTemplate(new BlankTemplate());
00291 }
00292
00296 public function drawPHPPage()
00297 {
00298 echo serialize($this->getPublicData());
00299 }
00300
00304 public function initXMLPage()
00305 {
00306 $this->initViewPage();
00307
00308 $this->setTemplate(new XMLTemplate());
00309 }
00310
00314 public function drawXMLPage()
00315 {
00316 $options = array(
00317 "indent" => "\t",
00318 "linebreak" => "\n",
00319 "typeHints" => false,
00320 "addDecl" => false,
00321 "rootName" => get_class($this)
00322 );
00323
00324 $serial = new XML_Serializer($options);
00325
00326
00327 $data = $this->getPublicData($this->params());
00328 if ($serial->serialize($data))
00329 echo $serial->getSerializedData();
00330 }
00331
00338 public function getPublicData()
00339 {
00340 global $me;
00341
00342 $data = array();
00343
00344 if ($this->id)
00345 {
00346 $data['id'] = $this->id;
00347
00348 if ($this->hasAuthor && $this->creator instanceOf BaseUser)
00349 {
00350 $data['user_id'] = $this->user_id;
00351 $data['username'] = $this->creator->getName();
00352 }
00353 if ($this->hasField('content_id'))
00354 $data['content_id'] = $this->content_id;
00355 if ($this->hasField('parent_id'))
00356 $data['parent_id'] = $this->parent_id;
00357 if ($this->hasField('add_date'))
00358 $data['add_date'] = $me->formatDateTime($this->add_date);
00359 if ($this->hasField('edit_date'))
00360 $data['edit_date'] = $me->formatDateTime($this->edit_date);
00361 if ($this->useLatLng)
00362 {
00363 $data['lat'] = $this->lat;
00364 $data['lon'] = $this->lon;
00365 }
00366
00367 if ($this->hasField('comment_count'))
00368 $data['comment_count'] = $this->comment_count;
00369
00370 if ($this->canView())
00371 $data['viewLink'] = $this->getUrl(".view?id=$this->id", null, true);
00372
00373 if ($this->canEdit())
00374 $data['editLink'] = $this->getUrl(".edit?id=$this->id", null, true);
00375
00376 if ($this->canDelete())
00377 $data['deleteLink'] = $this->getUrl(".delete?id=$this->id", null, true);
00378 }
00379 else
00380 $data['error'] = "That $this->english doesn't exist.";
00381
00382 return $data;
00383 }
00384
00390 public function getRssItem()
00391 {
00392 $item = new FeedItem();
00393 $item->title = 'Error';
00394 $item->description = 'You must define ' . get_class($this) . "::getRssItem() to enable rss support. Generally all you have to set are title and description.";
00395 $item->link = $this->getUrl(".view?id=$this->id", null, true);
00396 $item->date = strtotime($this->edit_date);
00397 $item->source = "http://" . Config::get('site_hostname');
00398 $item->descriptionHtmlSyndicated = true;
00399
00400
00401 if ($this->hasAuthor)
00402 {
00403 $item->author = $this->creator->getName();
00404 }
00405
00406 return $item;
00407 }
00408
00417 public function load($data, $deep = true)
00418 {
00419
00420 if (is_array($data))
00421 $this->setDataFromCache($data, $deep);
00422
00423 else if ($data)
00424 $this->loadData($data, $deep);
00425 }
00426
00433 protected function loadData($id, $deep = true)
00434 {
00435
00436 if ($id)
00437 {
00438
00439 $this->internalId = $id;
00440
00441
00442 if (!$this->loadCacheData($deep))
00443 {
00444 $this->lookupData($deep);
00445
00446 if ($deep && $this->useObjectCaching)
00447 $this->setCache();
00448 }
00449 }
00450
00451
00452 if ($this->commentsEnabled)
00453 $this->threadFactory();
00454
00455
00456 if ($this->useTags)
00457 $this->tagFactory();
00458 }
00459
00465 protected function lookupData($deep = true)
00466 {
00467
00468 $data = $this->getData(true);
00469 if (count($data) && is_array($data))
00470 foreach ($data AS $key => $val)
00471 if ($key != 'id')
00472 $this->data[$key] = $val;
00473
00474
00475 if ($deep)
00476 {
00477 if ($this->hasAuthor)
00478 {
00479 $this->creator = new User();
00480 $this->creator->load($this->user_id, false);
00481 }
00482 }
00483 }
00484
00490 protected function loadCacheData($deep = true)
00491 {
00492
00493 if ($this->useObjectCaching)
00494 {
00495
00496 $data = $this->getCache();
00497 if ($data)
00498 {
00499
00500 $this->load($data, $deep);
00501 return true;
00502 }
00503 }
00504
00505 return false;
00506 }
00507
00513 public function threadFactory()
00514 {
00515 $threadClass = get_class($this) . "Thread";
00516
00517 if (class_exists($threadClass))
00518 $this->comments = new $threadClass($this->id);
00519 }
00520
00524 public function tagFactory()
00525 {
00526 $tagClass = get_class($this) . "Tags";
00527
00528 if (class_exists($tagClass))
00529 $this->tags = new $tagClass($this->id);
00530 }
00531
00535 public function addPrivacyField()
00536 {
00537 }
00538
00544 public function save()
00545 {
00546
00547 if ($this->isDirty())
00548 $this->clean();
00549
00550
00551 $this->lookupCommentCount();
00552
00553
00554 $data = $this->saveData();
00555
00556
00557 if ($this->useObjectCaching)
00558 $this->deleteCache();
00559
00560
00561 self::deleteCachedPage('/' . strtolower(get_class($this)) . "/view/id/$this->id");
00562
00563 return $data;
00564 }
00565
00571 public function lookupCommentCount()
00572 {
00573
00574 if ($this->commentsEnabled && $this->hasField('comment_count') && $this->comments instanceOf BaseThread)
00575 $this->comment_count = $this->comments->searchCount(array(
00576 'content_id' => $this->id
00577 ), false);
00578
00579 return $this->comment_count;
00580 }
00581
00585 public function clean()
00586 {
00587 foreach ($this->dirtyFields AS $field)
00588 $this->cleanField($field);
00589 }
00590
00596 public function cleanField($field)
00597 {
00598
00599 }
00600
00604 public function isDirty()
00605 {
00606 return (bool)count($this->dirtyFields);
00607 }
00608
00614 public function delete()
00615 {
00616
00617 if ($this->commentsEnabled && $this->comments instanceOf BaseThread)
00618 $this->comments->delete();
00619
00620
00621 if ($this->useTags && $this->tags instanceOf BaseTags)
00622 $this->tags->delete();
00623
00624
00625 if ($this->useObjectCaching)
00626 $this->deleteCache();
00627
00628
00629 self::deleteCachedPage('/' + strtolower(get_class($this)) . "/view/id/$this->id");
00630
00631 return $this->deleteDb();
00632 }
00633
00637 public function drawEditPage()
00638 {
00639 $this->editPageLogic();
00640 }
00641
00647 function lookupLatLon()
00648 {
00649 $geo = new Geocode();
00650
00651
00652 $csz['city'] = $this->city;
00653 $csz['state'] = $this->state;
00654 $csz['zip'] = $this->zip;
00655 $csz['street'] = $this->street;
00656
00657
00658 $result = $geo->search($csz);
00659 if ($result)
00660 {
00661 $this->lat = $result['lat'];
00662 $this->lon = $result['lon'];
00663
00664 if (!$this->city)
00665 $this->city = $result['city'];
00666 if (!$this->state)
00667 $this->state = $result['state'];
00668 if (!$this->zip)
00669 $this->zip = $result['zip'];
00670 }
00671 }
00672
00679 public function drawViewPage($ignore = array())
00680 {
00681
00682 echo "<table width=\"100%\">\n";
00683 if (count($this->data))
00684 {
00685 foreach ($this->data AS $key => $val)
00686 {
00687 if (!in_array($key, $ignore))
00688 echo "\t<tr>\n\t\t<td>$key</td>\n\t\t<td>$val</td>\n\t</tr>\n";
00689 }
00690 }
00691 echo "</table>\n";
00692 }
00693
00702 public function drawDeletePage($preText = null, $button = null, $postText = null, $isUrl = true)
00703 {
00704 if (!isset($_POST['submit']))
00705 $this->deleteForm($preText, $button);
00706 else
00707 $this->deletePost($postText, $isUrl);
00708 }
00709
00714 function canEdit()
00715 {
00716 global $me;
00717
00718
00719 if (($this->user_id == $me->id && $me->id) || $me->isAdmin())
00720 return true;
00721
00722 else if (!$this->id && $me->id)
00723 return true;
00724 else
00725 return false;
00726 }
00727
00732 function canDelete()
00733 {
00734 global $me;
00735
00736 if (($this->user_id == $me->id && $me->id) || $me->isAdmin() && $this->id)
00737 return true;
00738 else
00739 return false;
00740 }
00741
00745 function drawHeaderRow()
00746 {
00747
00748 }
00749
00758 function getName($link = false, $text = '')
00759 {
00760 $text = trim($text);
00761 if (!$text)
00762 $text = '???';
00763
00764 if ($link)
00765 return $this->getLink(".view?id=$this->id", $text);
00766 return $text;
00767 }
00768
00773 function drawRow()
00774 {
00775 echo "<h3>" . $this->getName(true) . "</h3>";
00776 echo "<p>By: ";
00777 echo $this->creator->getName(true) . "<br/>";
00778 echo Time::formatRelative($this->add_date) . "<br/>";
00779
00780 if ($this->commentsEnabled)
00781 echo $this->getCommentsLink($this->id);
00782 "</p>\n";
00783 }
00784
00792 public function getAutoCompleteRow()
00793 {
00794 return "<li>" . $this->getName() . "</li>";
00795 }
00796
00805 public function getCommentsLink()
00806 {
00807 return $this->getLink(".view?id=$this->id#comments", Util::pluralize('comment', $this->comment_count, true));
00808 }
00809
00813 function drawHeaderLine()
00814 {
00815 }
00816
00821 function drawLine()
00822 {
00823 }
00824
00828 function drawPostedOwner()
00829 {
00830 global $me;
00831
00832 echo "<h3>Created: " . Util::formatTimeRelative($this->add_date) . " by " . $this->creator->getName(true) . "</h3>\n";
00833 if ($this->add_date != $this->edit_date)
00834 echo "<h4>Last updated: " . Util::formatTimeRelative($this->edit_date) . "</h4>\n";
00835 }
00836
00842 public function getPagesXml()
00843 {
00844 $xml = parent::getPagesXml();
00845
00846 $xml .= $this->getEditPageXml();
00847 $xml .= $this->getViewPageXml();
00848 $xml .= $this->getDeletePageXml();
00849 $xml .= $this->getJSONPageXml();
00850 $xml .= $this->getPHPPageXml();
00851 $xml .= $this->getXMLPageXml();
00852
00853 return $xml;
00854 }
00855
00861 protected function getViewPageXml()
00862 {
00863 return <<<XML
00864 <page name="view" type="content" desc="view a content">
00865 <param name="id" desc="id of the content" required="1" type="int"/>
00866 </page>
00867 XML;
00868 }
00869
00875 protected function getDeletePageXml()
00876 {
00877 return <<<XML
00878 <page name="delete" type="content" desc="delete an object">
00879 <param name="id" required="1" desc="the id of the module" type="int"/>
00880 </page>
00881 XML;
00882 }
00883
00889 protected function getJSONPageXml()
00890 {
00891 return <<<XML
00892 <page name="json" desc="fetch the json representation of this object">
00893 <param name="id" desc="id of the content" required="1" type="int"/>
00894 </page>
00895 XML;
00896 }
00897
00903 protected function getPHPPageXml()
00904 {
00905 return <<<XML
00906 <page name="php" desc="fetch the php serialize() representation of this object">
00907 <param name="id" desc="id of the content" required="1" type="int"/>
00908 </page>
00909 XML;
00910 }
00911
00917 protected function getXMLPageXml()
00918 {
00919 return <<<XML
00920 <page name="xml" desc="fetch the XML representation of this object">
00921 <param name="id" desc="id of the content" required="1" type="int"/>
00922 </page>
00923 XML;
00924 }
00925
00931 protected function getEditPageXml()
00932 {
00933 $var = "\n\t<page name=\"edit\" desc=\"create or edit a(n) $this->english\">\n";
00934 $var .= "\t\t<param name=\"id\" desc=\"the id of the $this->english. if not supplied, a new record will be created.\" type=\"int\"/>\n";
00935 $var .= $this->getEditPageParamXml();
00936 $var .= "\n\t</page>\n";
00937
00938 return $var;
00939 }
00940
00946 protected function getEditPageParamXml()
00947 {
00948 $var = '';
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959 return $var;
00960 }
00961
00966 public function canView()
00967 {
00968
00969 if (!$this->id)
00970 return false;
00971 return true;
00972 }
00973
00980 protected function getData($useDb = true)
00981 {
00982 if ($useDb)
00983 return $this->getDbData();
00984 else
00985 {
00986 $data = $this->data;
00987 $data['id'] = $this->id;
00988
00989 return $data;
00990 }
00991 }
00992
01000
01001 public function setData($data, $ignore = null)
01002 {
01003
01004 if (is_array($data))
01005 {
01006 if (!is_array($ignore))
01007 $ignore = array($ignore);
01008
01009
01010 foreach ($data AS $key => $val)
01011 {
01012
01013 if (in_array($key, $ignore))
01014 continue;
01015
01016
01017 if ($key === 'id')
01018 $this->internalId = (int)$val;
01019 else if ($this->hasField($key))
01020 $this->data[$key] = $val;
01021 }
01022
01023 return true;
01024 }
01025 else
01026 return false;
01027
01028 }
01029
01035 protected function saveData()
01036 {
01037 return $this->saveDb();
01038 }
01039
01045 private function getDbData()
01046 {
01047
01048 if ($this->id)
01049 {
01050 $result = dbFetchAssoc(dbQuery("
01051 SELECT *
01052 FROM $this->tableName
01053 WHERE id = '$this->id'
01054 "));
01055
01056 if (is_array($result))
01057 return $result;
01058 }
01059
01060 return false;
01061 }
01062
01069 private function saveDb()
01070 {
01071
01072 $fields = array();
01073 $columns = $this->getDbFields();
01074
01075
01076 foreach ($this->dirtyFields AS $key)
01077 {
01078
01079 if ($columns[$key] && isset($this->data[$key]) && $key != 'id')
01080 {
01081 $val = $this->data[$key];
01082
01083
01084 $val = str_replace("\\\\", "\\", $val);
01085 $val = str_replace("\'", "'", $val);
01086 $val = str_replace("\\\"", "\"", $val);
01087
01088
01089 $fields[] = "`$key` = '" . addslashes($val) . "'";
01090 }
01091 }
01092
01093
01094 if (count($fields))
01095 {
01096
01097 $sqlFields = implode(",\n", $fields) . "\n";
01098
01099
01100 if ($this->id)
01101 {
01102 $sql = "UPDATE $this->tableName SET\n";
01103 $sql .= $sqlFields;
01104 $sql .= "WHERE id = '$this->id'\n";
01105 $sql .= "LIMIT 1";
01106
01107 dbExecute($sql);
01108 }
01109
01110 else
01111 {
01112 $sql = "INSERT INTO $this->tableName SET\n";
01113 $sql .= $sqlFields;
01114
01115 $this->id = dbExecute($sql, true);
01116 }
01117 }
01118 }
01119
01125 private function deleteDb()
01126 {
01127
01128 if ($this->id)
01129 {
01130 dbExecute("
01131 DELETE FROM $this->tableName
01132 WHERE id = '$this->id'
01133 ");
01134
01135 return true;
01136 }
01137 return false;
01138 }
01139
01146 protected function editFormInit()
01147 {
01148
01149 $form = $this->editFormCreate();
01150
01151
01152 $this->editFormAddFields($form);
01153
01154 return $form;
01155 }
01156
01162 protected function editFormCreate()
01163 {
01164
01165 $form = new Form();
01166 $form->action = $this->getUrl(".edit?id=$this->id");
01167
01168 return $form;
01169 }
01170
01176 protected function editFormAddFields($form)
01177 {
01178 }
01179
01183 protected function editPageLogic()
01184 {
01185
01186 $form = $this->editFormInit();
01187
01188
01189 if ($form->isSubmitted())
01190 {
01191
01192 $this->editFormValidate($form);
01193
01194
01195 if (!$form->hasError())
01196 {
01197
01198 $this->editPagePostSaveSync($form);
01199 $this->editPagePostSuccess($form);
01200 }
01201 }
01202
01203 else
01204 $this->editFormLoad($form);
01205
01206
01207 if ($form->needsDrawn())
01208 $this->editFormDraw($form);
01209 }
01210
01216 protected function editFormValidate($form)
01217 {
01218
01219 $form->validate();
01220 }
01221
01227 protected function editFormLoad($form)
01228 {
01229 if (!$form->isSubmitted())
01230 $form->setData($this->data);
01231 }
01232
01239 public function loadDirtyData($data)
01240 {
01241
01242 $columns = $this->getDbFields();
01243 foreach ($columns AS $key => $val)
01244 {
01245
01246 if (array_key_exists($key, $data))
01247 $this->$key = $data[$key];
01248 }
01249 }
01250
01257 public function getDbFields()
01258 {
01259 return BaseJumper::getDbTable($this->tableName);
01260 }
01261
01267 protected function editPagePostSaveSync($form)
01268 {
01269 global $me;
01270
01271
01272 $columns = $this->getDbFields();
01273 foreach ($columns AS $key => $data)
01274 {
01275 if (!$this->id && $key == 'add_date')
01276 $this->$key = date('Y-m-d H:i:s');
01277 else if ($key == 'edit_date')
01278 $this->$key = date('Y-m-d H:i:s');
01279 else if ($key == 'user_id' && !$this->id)
01280 $this->$key = $me->id;
01281 else if ($form->hasField($key))
01282 $this->$key = $form->getData($key);
01283 }
01284
01285
01286 if ($form->hasField('citystzip') && $this->hasField('city') && $this->hasField('state') && $this->hasField('zip'))
01287 {
01288 $csz = $form->getData('citystzip');
01289 $this->city = $csz['city'];
01290 $this->state = $csz['state'];
01291 $this->zip = $csz['zip'];
01292 }
01293
01294
01295 if ($this->useLatLng)
01296 $this->lookupLatLon();
01297
01298
01299 $this->save();
01300
01301
01302 if ($this->useTags)
01303 $this->tags->updateTagsForm($form);
01304 }
01305
01310 protected function editPagePostSuccess($form)
01311 {
01312 Util::redirect($this->getUrl(".view?id=$this->id"));
01313 }
01314
01320 protected function editFormDraw($form)
01321 {
01322 $form->drawAll();
01323 }
01324
01331 protected function deleteForm($body = null, $button = null)
01332 {
01333
01334 if ($body === null)
01335 $body = "Are you sure you want to delete this $this->english?";
01336
01337 if ($button === null)
01338 $button = 'Delete ' . ucfirst($this->english);
01339
01340 echo "<form method=\"POST\" action=\"$_SERVER[REQUEST_URI]\">\n";
01341 echo "<input type=\"hidden\" name=\"submit\" value=\"1\">\n";
01342 echo "<table>";
01343 echo "<tr><td>$body</td></tr>\n";
01344 echo "<tr><td align=\"center\"><input type=\"submit\" value=\"$button\"></td></tr>\n";
01345 echo "</table>";
01346 echo "</form>";
01347 }
01348
01355 protected function deletePost($body = null, $isUrl = true)
01356 {
01357 $this->delete();
01358
01359 if ($isUrl)
01360 {
01361 if ($body === null)
01362 Util::redirect($this->baseUrl);
01363 else
01364 Util::redirect($body);
01365 }
01366 else
01367 echo $body;
01368 }
01369
01373 public function checkTable($skip = array())
01374 {
01375 if (!BaseJumper::dbHasTable($this->tableName))
01376 $this->addStatus(
01377 "Oops, table <b>$this->tableName</b> doesn't exist. You may
01378 want to visit the " . $this->getLink(".createtable", "create
01379 table") . " page to create it.<br/>Until then, the <b>" .
01380 get_class($this) . "</b> module may not work correctly."
01381 );
01382 }
01383
01390 public function isAdmin()
01391 {
01392 global $me;
01393 return $me->isAdmin();
01394 }
01395
01403 public function hasField($key)
01404 {
01405 if ($this->tableName)
01406 return BaseJumper::tableHasField($this->tableName, $key);
01407
01408 return false;
01409 }
01410
01416 public function getCreateTableSql()
01417 {
01418 $fields = implode(",\n", $this->getCreateFieldsArray());
01419 $indexes = implode(",\n", $this->getCreateIndexesArray());
01420
01421
01422 if (count($this->fullTextFields))
01423 $indexes .= ",\n FULLTEXT(" . implode(",", $this->fullTextFields) . ")";
01424
01425 return "CREATE TABLE IF NOT EXISTS $this->tableName\n(\n$fields,\n$indexes\n);";
01426 }
01427
01434 public function getCreateFieldsArray()
01435 {
01436 $fields = array(
01437 "id" => "id INT(11) not null auto_increment",
01438 "add_date" => "add_date datetime not null",
01439 "edit_date" => "edit_date datetime not null"
01440 );
01441
01442 if ($this->hasAuthor)
01443 $fields['user_id'] = "user_id INT(11) default 0 not null";
01444
01445 if ($this->useLatLng)
01446 {
01447 $fields['lat'] = "lat double default 0 not null";
01448 $fields['lon'] = "lon double default 0 not null";
01449 $fields['street'] = "street varchar(255) default '' not null";
01450 $fields['city'] = "city varchar(128) default '' not null";
01451 $fields['state'] = "state char(2) default '' not null";
01452 $fields['zip'] = "zip char(5) default '' not null";
01453 }
01454
01455 if ($this->commentsEnabled)
01456 $fields['comment_count'] = "comment_count INT(11) default 0 not null";
01457
01458 return $fields;
01459 }
01460
01467 public function getCreateIndexesArray()
01468 {
01469 $fields = array();
01470 $fields['id'] = "PRIMARY KEY (id)";
01471 $fields['add_date'] = "KEY (add_date)";
01472 $fields['edit_date'] = "KEY (edit_date)";
01473
01474 if ($this->hasAuthor)
01475 $fields['user_id'] = "KEY (user_id)";
01476
01477 if ($this->useLatLng)
01478 $fields['latlon'] = "KEY (lat, lon)";
01479
01480 return $fields;
01481 }
01482
01488 public function getCacheKey($id = null)
01489 {
01490 if ($id === null)
01491 $id = $this->id;
01492
01493 return "BaseJumper:object:" . get_class($this) . ":" . $id;
01494 }
01495
01504 protected function getDataToCache($deep = true)
01505 {
01506 $data = array();
01507
01508
01509 $data['id'] = $this->id;
01510 $data['data'] = $this->data;
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526 if ($deep)
01527 {
01528 if ($this->hasAuthor && $this->creator instanceOf BaseUser)
01529 $data['creator'] = $this->creator->getDataToCache(false);
01530 }
01531
01532 return $data;
01533 }
01534
01543 protected function setDataFromCache($data, $deep = true)
01544 {
01545 if (is_array($data))
01546 {
01547
01548 $this->internalId = $data['id'];
01549
01550
01551 $this->data = $data['data'];
01552
01553
01554
01555
01556
01557
01558
01559
01560
01561
01562
01563 if ($deep)
01564 {
01565 if ($this->hasAuthor)
01566 {
01567 $this->creator = new User();
01568 $this->creator->load($data['creator'], false);
01569 }
01570 }
01571 }
01572 }
01573
01577 public function getCache()
01578 {
01579 return CacheBot::get($this->getCacheKey(), self::$objectCacheLife);
01580 }
01581
01585 public function setCache()
01586 {
01587 return CacheBot::set($this->getCacheKey(), $this->getDataToCache(), self::$objectCacheLife);
01588 }
01589
01593 public function deleteCache()
01594 {
01595 return CacheBot::delete($this->getCacheKey());
01596 }
01597 }
01598 ?>