00001 <?php
00002
00015 class SnapContentMediaDocument extends SnapContent {
00016 public static $DOC_TYPES = array(
00017 'doc' => 'Microsoft Word 97/2000/XP/2003 Document',
00018 'docx' => 'Micrsoft Word 2007/2008 Document',
00019 'pdf' => 'Adobe PDF Document',
00020 'txt' => 'Text File'
00021 );
00022
00027 protected function getSuffix() {
00028 if(!$this->validate())
00029 return false;
00030
00031 return $this->get('suffix');
00032 }
00033
00041 protected function doDelete() {
00042 if(!$this->validate())
00043 return true;
00044
00045 $doc = new SnapExternalFile($this->version, $this->get('doc'));
00046 $doc->delete();
00047
00048 return true;
00049 }
00050
00058 protected function doCopy() {
00059 $newCM = new SnapContentMediaDocument($this->version);
00060 $doc = new SnapExternalFile($this->version, $this->get('doc'));
00061 $copy = $doc->copy();
00062 if(!$copy) {
00063 $this->setError('Failed to copy: ' . $doc->getLastError());
00064 return null;
00065 }
00066 $newCM->set('suffix', $this->get('suffix'));
00067 $newCM->set('doc', $copy->getArray());
00068 return $newCM;
00069 }
00070
00078 protected function validate() {
00079 $values = $this->getAll();
00080 if(!isset($values['doc']) || !isset($values['suffix']))
00081 return false;
00082 else
00083 return true;
00084 }
00085
00094 protected function doGetHTML($params) {
00095 if(!$this->validate())
00096 return false;
00097
00098 $doc = new SnapExternalFile($this->version, $this->get('doc'));
00099 $class = isset($params['class']) ? " class=\"$params[class]\"" : "";
00100 $style = isset($params['style']) ? " style=\"$params[style]\"" : "";
00101
00102 if(isset($params['embed']) && $params['embed'] == true) {
00103 $width = isset($params['width']) ? " width=\"$params[width]\"" : "";
00104 $height = isset($params['height']) ? " height=\"$params[height]\"" : "";
00105 return "<iframe src=\"" . $doc->getURL() . "\"$width$height$class$style></iframe>";
00106 }
00107 else {
00108 $title = isset($params['title']) ? $params['title'] : 'Download Document';
00109 return "<a$class$style href=\"" . $this->getLinkURL() . "\">$title</a>";
00110 }
00111 }
00112
00119 public function uploadDocument($tmpFile, $fileName) {
00120 if(!is_readable($tmpFile)) {
00121 $this->setError("File '$tmpFile' does not exist "
00122 . "or is not accessible");
00123 return false;
00124 }
00125
00126 $doc = new SnapExternalFile($this->version);
00127 $doc->create($tmpFile, $fileName);
00128 $this->set('doc', $doc->getArray());
00129
00130 $suffix = substr(strrchr($fileName, '.'), 1);
00131 if(!isset(self::$DOC_TYPES[$suffix])) {
00132 $this->setError("Invalid file type");
00133 return false;
00134 }
00135 $this->set('suffix', $suffix);
00136
00137 return true;
00138 }
00139
00147 public function getURL() {
00148 if(!$this->validate())
00149 return false;
00150 $file = new SnapExternalFile($this->version, $this->get('doc'));
00151 return $file->getURL();
00152 }
00153
00161 public function getPath() {
00162 if(!$this->validate())
00163 return false;
00164 $file = new SnapExternalFile($this->version, $this->get('doc'));
00165 return $file->getPath();
00166 }
00167
00175 public function getDocumentSuffix() {
00176 if(!$this->validate())
00177 return false;
00178 return $this->get('suffix');
00179 }
00180
00188 public function getDocumentType() {
00189 $suffix = $this->getSuffix();
00190 if(!isset(self::$DOC_TYPES[$suffix]))
00191 return "Invalid";
00192 else
00193 return self::$DOC_TYPES[$suffix];
00194 }
00195
00203 public function getSize() {
00204 if(!$this->validate())
00205 return false;
00206 $file = new SnapExternalFile($this->version, $this->get('doc'));
00207 return filesize($file->getPath());
00208 }
00209
00215 public function checkMedia() {
00216 if($this->get('doc') == '')
00217 return true;
00218 if(!$this->validate())
00219 return false;
00220 $file = new SnapExternalFile($this->version, $this->get('doc'));
00221 $path = $file->getPath();
00222 if(is_readable($path) && is_file($path))
00223 return true;
00224 else
00225 return false;
00226 }
00227
00235 public function updateFileList() {
00236 if(!$this->validate())
00237 return 'no files to insert';
00238 SnapDBI::startTransaction();
00239 $file = new SnapExternalFile($this->version, $this->get('doc'));
00240 $ret = $file->updateDB();
00241 if($ret)
00242 SnapDBI::commitTransaction();
00243 else {
00244 $ret = 'database failure';
00245 SnapDBI::cancelTransaction();
00246 }
00247 return $ret;
00248 }
00249
00250 }
00251
00252 ?>