00001 <?php
00002
00013 class SnapContentMediaNetlogoApplet extends SnapContent {
00014 private $hasUploaded = false;
00018 protected function doDelete() {
00019 if(!$this->validate())
00020 return true;
00021
00022 $nlogo = new SnapExternalFile($this->version, $this->get('nlogo'));
00023 $nlogo->delete();
00024
00025 return true;
00026 }
00031 protected function doCopy() {
00032 $newCM = new SnapContentMediaNetlogoApplet($this->version);
00033
00034 $nlogo = new SnapExternalFile($this->version, $this->get('nlogo'));
00035
00036 $newCM->set('width', $this->get('width'));
00037 $newCM->set('height', $this->get('height'));
00038 $newCM->set('nlogo', $nlogo->copy()->getArray());
00039
00040 return $newCM;
00041 }
00042
00050 protected function validate() {
00051 $values = $this->getAll();
00052 if(!isset($values['width']) || !isset($values['height'])
00053 || !isset($values['nlogo']))
00054 return false;
00055 else
00056 return true;
00057 }
00058
00067 protected function doGetHTML($params) {
00068 if(!$this->validate())
00069 return false;
00070
00071 $nlogo = new SnapExternalFile($this->version, $this->get('nlogo'));
00072 $nlogourl = $nlogo->getURL();
00073 $toEmbedPath = Snap2::getMediaURLBase() . "/netlogo/NetLogoLite.jar";
00074 $width = $this->get('width');
00075 $height = $this->get('height');
00076 $html = <<<END_HTML
00077 <applet code="org.nlogo.window.Applet"
00078 archive="$toEmbedPath"
00079 width="$width" height="$height">
00080 <param name="DefaultModel"
00081 value="$nlogourl">
00082 </applet>
00083 END_HTML;
00084
00085 return $html;
00086 }
00113 public function uploadApplet($tmpFile, $fileName, $width, $height) {
00114 if(!is_readable($tmpFile)) {
00115 $this->setError("File '$tmpFile' does not exist "
00116 . "or is not accessible");
00117 return false;
00118 }
00119
00120 if(!ctype_digit((string) $height) || $height <= 0) {
00121 $this->setError("Invalid height: '$height'");
00122 return false;
00123 }
00124 if(!ctype_digit((string) $width) || $width <= 0) {
00125 $this->setError("Invalid width: '$width'");
00126 return false;
00127 }
00128
00129 $nlogo = new SnapExternalFile($this->version);
00130 $nlogo->create($tmpFile, $fileName);
00131
00132 $this->set('width', $width);
00133 $this->set('height', $height);
00134 $this->set('nlogo', $nlogo->getArray());
00135
00136 return true;
00137 }
00138
00147 private function copyFrom($oldCM) {
00148 if($this->hasUploaded)
00149 return true;
00150
00151 $oldNlogofile = new SnapExternalFile($this->version, $oldCM->get('nlogo'));
00152 $newNlogofile = $oldNlogofile->copy();
00153 if(!$newNlogofile) {
00154 $this->setError("Could not copy netlogo file");
00155 return false;
00156 }
00157
00158 $this->set('nlogo', $newNlogofile->getArray());
00159 $this->set('width', $oldCM->getWidth());
00160 $this->set('height', $oldCM->getHeight());
00161
00162 $this->hasUploaded = true;
00163
00164 return true;
00165 }
00166
00176 public function setWidth($oldCM, $width) {
00177 if(!ctype_digit((string) $width) || $width <= 0) {
00178 $this->setError("Invalid width: '$width'");
00179 return false;
00180 }
00181 if(!$this->copyFrom($oldCM))
00182 return false;
00183 $this->set('width', $width);
00184 return true;
00185 }
00186
00187
00198 public function setHeight($oldCM, $height) {
00199 if(!ctype_digit((string) $height) || $height <= 0) {
00200 $this->setError("Invalid height: '$height'");
00201 return false;
00202 }
00203 if(!$this->copyFrom($oldCM))
00204 return false;
00205 $this->set('height', $height);
00206 return true;
00207 }
00208
00216 public function getWidth() {
00217 if(!$this->validate())
00218 return false;
00219
00220 return $this->get('width');
00221 }
00222
00223
00224
00232 public function getHeight() {
00233 if(!$this->validate())
00234 return false;
00235
00236 return $this->get('height');
00237 }
00238
00239
00247 public function getNlogoURL() {
00248 if(!$this->validate())
00249 return false;
00250 $file = new SnapExternalFile($this->version, $this->get('nlogo'));
00251 $url = $file->getURL();
00252 return $url;
00253 }
00254
00262 public function getNlogoPath() {
00263 if(!$this->validate())
00264 return false;
00265 $file = new SnapExternalFile($this->version, $this->get('nlogo'));
00266 $path = $file->getPath();
00267 return $path;
00268 }
00269
00277 public function getSize() {
00278 if(!$this->validate())
00279 return false;
00280 $file = new SnapExternalFile($this->version, $this->get('nlogo'));
00281 return filesize($file->getPath());
00282 }
00283
00291 public function checkMedia() {
00292 if($this->get('nlogo') == '')
00293 return true;
00294 if(!$this->validate())
00295 return false;
00296 $nlogo = new SnapExternalFile($this->version, $this->get('nlogo'));
00297 $path = $nlogo->getPath();
00298 if(!is_readable($path) || !is_file($path))
00299 return false;
00300 else
00301 return true;
00302 }
00303
00311 public function updateFileList() {
00312 if(!$this->validate())
00313 return 'no files to insert';
00314 SnapDBI::startTransaction();
00315 $nlogo = new SnapExternalFile($this->version, $this->get('nlogo'));
00316 if(!$nlogo->updateDB()) {
00317 SnapDBI::cancelTransaction();
00318 return 'database failure';
00319 }
00320 else {
00321 SnapDBI::commitTransaction();
00322 return true;
00323 }
00324 }
00325 }
00326
00327 ?>