00001 <?php
00002
00020 abstract class SnapContent extends SObject {
00021 private $raw = "";
00022 private $inSync = true;
00023 private $content = array();
00024 private $readOnly = false;
00025 private $notrans = false;
00026
00031 protected $version = null;
00032
00033 private $editorClass = '';
00034 private $previewClass = '';
00035 private $translatorClass = '';
00036 private $suffix = '';
00037 private $name = '';
00038
00039 public static $CONTENT_MODULES;
00040
00041 private static $init = false;
00042
00051 public function __construct($version, $raw = false) {
00052 parent::__construct();
00053
00054 if(isset($this->NEED_TRANSLATION))
00055 $notrans = !$this->NEED_TRANSLATION;
00056 else
00057 $notrans = false;
00058
00059 if($raw !== false) {
00060 if($notrans)
00061 $this->content = $raw;
00062 else
00063 $this->content = json_decode($raw, true);
00064 $this->raw = $raw;
00065 $this->readOnly = true;
00066 }
00067
00068 $this->notrans = $notrans;
00069 $this->version = $version;
00070
00071 if($version) {
00072 $type = $version->getResource()->getContentType();
00073 $this->editorClass = self::$CONTENT_MODULES[$type]['editor'];
00074 $this->previewClass = self::$CONTENT_MODULES[$type]['preview'];
00075 $this->translatorClass = self::$CONTENT_MODULES[$type]['translator'];
00076 $this->suffix = self::$CONTENT_MODULES[$type]['suffix'];
00077 $this->name = self::$CONTENT_MODULES[$type]['name'];
00078 }
00079 }
00080
00081
00082
00083
00084
00095 public static function getContentModule($version, $new = false) {
00096 $type = $version->getResource()->getContentType();
00097 $cl = self::$CONTENT_MODULES[$type]['contentModule'];
00098 if($new)
00099 return new $cl($version);
00100 else
00101 return new $cl($version, $version->getContent());
00102 }
00103
00109 public static function getContentModuleClass($version) {
00110 $type = $version->getResource()->getContentType();
00111 return self::$CONTENT_MODULES[$type]['contentModule'];
00112 }
00113
00119 public static function isValidType($type) {
00120 return isset(self::$CONTENT_MODULES[$type]);
00121 }
00122
00131 public static function create($version) {
00132 $type = $version->getResource()->getContentType();
00133 $cl = self::$CONTENT_MODULES[$type]['contentModule'];
00134 return new $cl($version);
00135 }
00136
00146 public static function getInitialValue($type) {
00147 $cl = SnapContent::$CONTENT_MODULES[$type]['contentModule'];
00148 if(method_exists($cl, 'initialValue')) {
00149 $content = call_user_func(array($cl, 'initialValue'));
00150 }
00151 else
00152 $content = "";
00153 return $content;
00154 }
00155
00162 public function getEditor($redirectTo = "", $showPreview = true) {
00163 $cl = $this->editorClass;
00164 return new $cl($this->version, $redirectTo, $showPreview);
00165 }
00166
00174 public function getEditorClass() {
00175 return $this->editorClass;
00176 }
00177
00182 public function getPreview($mode = TKSnapPreview::MODE_DEFAULT) {
00183 $cl = $this->previewClass;
00184 return new $cl($this->version, $mode);
00185 }
00186
00194 public function getPreviewClass() {
00195 return $this->previewClass;
00196 }
00197
00203 public function getTranslator() {
00204 return $this->translatorClass;
00205 }
00206
00211 public function getName() {
00212 return $this->name;
00213 }
00214
00220 public static function getContentName($type) {
00221 if(!isset(self::$CONTENT_MODULES[$type]))
00222 return "???";
00223 else
00224 return self::$CONTENT_MODULES[$type]['name'];
00225 }
00226
00236 public static function getTypeFromSuffix($suffix) {
00237 foreach(self::$CONTENT_MODULES as $type => $cminfo) {
00238 if(in_array($suffix, $cminfo['suffixes']))
00239 return $type;
00240 }
00241
00242 return false;
00243 }
00244
00252 public function getRaw() {
00253 if(!$this->inSync) {
00254 if($this->notrans)
00255 $this->raw = $this->content;
00256 else
00257 $this->raw = json_encode($this->content);
00258 }
00259 $this->inSync = true;
00260 return $this->raw;
00261 }
00262
00270 public function copy() {
00271 $cp = $this->doCopy();
00272 $cp->readOnly = true;
00273 return $cp;
00274 }
00275
00288 public function delete() {
00289 return $this->doDelete();
00290 }
00291
00300 public function getLinkURL($download = false) {
00301 if($download)
00302 $dn = 'download';
00303 else
00304 $dn = 'content';
00305 if($this->suffix === false)
00306 $this->suffix = $this->getSuffix();
00307 $id = $this->version->getId();
00308 $active = $this->version->getResource()->getActiveVersion();
00309 if(!$active || $active->getId() != $id)
00310 return Snap2::getMediaURLBase() . "/$dn" . $this->version->getCanonicalPath()
00311 . "." . $this->suffix;
00312 else
00313 return Snap2::getMediaURLBase() . "/$dn" . $this->version->getResource()->getCanonicalPath()
00314 . "." . $this->suffix;
00315 }
00316
00324 public function getContentSuffix() {
00325 if($this->suffix === false)
00326 $this->suffix = $this->getSuffix();
00327 return $this->suffix;
00328 }
00329
00339 public function getHTML($params = array()) {
00340 return $this->doGetHTML($params);
00341 }
00342
00352 public function isValid() {
00353 return $this->validate();
00354 }
00355
00356
00357
00358
00359
00370 protected function set($field, $value) {
00371 if($this->readOnly) {
00372 $this->setError("Cannot set field '$field' on read-only content module");
00373 return false;
00374 }
00375 $this->content[$field] = $value;
00376 $this->inSync = false;
00377 return true;
00378 }
00379
00389 protected function setAll($values) {
00390 if($this->readOnly) {
00391 $this->setError("Cannot set fields on read-only content module");
00392 return false;
00393 }
00394 $this->content = $values;
00395 $this->inSync = false;
00396 return true;
00397 }
00398
00405 protected function get($field) {
00406 if(!isset($this->content[$field])) {
00407 return null;
00408 }
00409 return $this->content[$field];
00410 }
00411
00417 protected function getAll() {
00418 return $this->content;
00419 }
00420
00421
00422
00423
00424
00433 protected function doCopy() {
00434 $cl = get_class($this);
00435 $cm = new $cl($this->version, $this->getRaw());
00436 $cm->readOnly = true;
00437 return $cm;
00438 }
00439
00446 protected function doDelete() {
00447 return true;
00448 }
00449
00459 protected function getSuffix() {
00460 return "bin";
00461 }
00462
00472 protected abstract function doGetHTML($params);
00473
00482 protected abstract function validate();
00483
00492 public abstract function checkMedia();
00493 public abstract function updateFileList();
00494 }
00495
00496 ?>