00001 <?php
00002
00003
00016 class SnapContentMediaAgentsheetsApplet extends SnapContent {
00017 private $hasUploaded = false;
00018
00026 protected function doDelete() {
00027 if(!$this->validate())
00028 return true;
00029
00030 $appDir= new SnapExternalFile($this->version, $this->get('appDir'));
00031 $appDir->delete();
00032
00033 return true;
00034 }
00035
00043 protected function doCopy() {
00044 $newCM = new SnapContentMediaAgentsheetsApplet($this->version);
00045
00046 $appDir = new SnapExternalFile($this->version, $this->get('appDir'));
00047
00048 $newCM->set('width', $this->get('width'));
00049 $newCM->set('height', $this->get('height'));
00050 $newCM->set('wsCols', $this->get('wsCols'));
00051 $newCM->set('wsRows', $this->get('wsRows'));
00052 $newCM->set('worksheet', $this->get('worksheet'));
00053 $newCM->set('additionalWorksheets', $this->get('additionalWorksheets'));
00054 $cp = $appDir->copy();
00055 if($cp) {
00056 $newCM->set('appDir', $cp->getArray());
00057
00058 if($this->get('zip')) {
00059 $zip = new SnapExternalFile($this->version, $this->get('zip'));
00060 $newCM->set('zip', $zip->copy()->getArray());
00061 $html = $this->doGetHTML(array('__noprefix' => true));
00062 $html = <<<END_HTML
00063 <html>
00064 <head>
00065 <title>Agentsheets Applet</title>
00066 </head>
00067 <body>
00068 <p>
00069 $html
00070 </p>
00071 </body>
00072 </html>
00073 END_HTML;
00074 $zipPath = $zip->getPath();
00075 file_put_contents(dirname($zipPath) . '/index.html', $html);
00076 system("cd " . dirname($zipPath) . "; zip -g $zipPath index.html >/dev/null 2>&1");
00077 }
00078 }
00079
00080 return $newCM;
00081 }
00082
00090 protected function validate() {
00091 $values = $this->getAll();
00092 if(!isset($values['width']) || !isset($values['height'])
00093 || !isset($values['appDir']) || !isset($values['wsRows']) || !isset($values['wsCols']) || !isset($values['worksheet']))
00094 return false;
00095 else
00096 return true;
00097 }
00098
00107 protected function doGetHTML($params) {
00108 if(!$this->validate())
00109 return false;
00110 if(isset($params['__noprefix']))
00111 $appletFolder = '';
00112 else
00113 $appletFolder = $this->getAppDirURL();
00114 $width = $this->get('width');
00115 $height = $this->get('height');
00116 $wsCols = $this->get('wsCols');
00117 $wsRows = $this->get('wsRows');
00118 $worksheet = $this->get('worksheet');
00119 $additionalWorksheets = $this->get('additionalWorksheets');
00120 if(is_array($additionalWorksheets) && count($additionalWorksheets)) {
00121 $wkshts = implode(':', $additionalWorksheets);
00122 $addl = <<<END_HTML
00123 <param name="AllWorksheets" value="$wkshts" />
00124 END_HTML;
00125 }
00126 else
00127 $addl = '';
00128
00129
00130 $html = <<<END_HTML
00131 <applet codebase="${appletFolder}applet" code="__mysimulation__.class" archive="uAgentsheets.jar" width="$width" height="$height" name="AgentSheets-Simulation" align="BOTTOM">
00132 <param name="ShowMessageBox" value="true" />
00133 <param name="AgentsChangeable" value="true" />
00134 <param name="AgentsSelectable" value="true" />
00135 <param name="ShowTools" value="true" />
00136 <param name="ShowDropDownMenu" value="true" />
00137 <param name="AgentsDraggable" value="true" />
00138 <param name="DefaultTool" value="ARROW_TOOL" />
00139 <param name="DefaultWorksheet" value="$worksheet" />
00140 <param name="WorksheetColumns" value="$wsCols" />
00141 <param name="AutoStart" value="false" />
00142 <param name="ShowGallery" value="true" />
00143 <param name="WorksheetTitle" value="Worksheet" />
00144 <param name="GalleryTitle" value="Gallery" />
00145 <param name="WorksheetRows" value="$wsRows" />
00146 <param name="ShowButtons" value="true" />
00147 <param name="SimulationProperties" value="Simulation-Properties.sip" />
00148 <param name="ShowSpeedControl" value="true" />
00149 $addl
00150 </applet>
00151 END_HTML;
00152 return $html;
00153 }
00186 public function uploadApplet($tmpFile, $fileName, $width, $height, $wsCols, $wsRows, $worksheet, $addlWorksheets = array()) {
00187 if(!is_readable($tmpFile)) {
00188 $this->setError("File '$tmpFile' does not exist "
00189 . "or is not accessible");
00190 return false;
00191 }
00192
00193 if(!ctype_digit((string) $height) || $height <= 0) {
00194 $this->setError("Invalid height: '$height'");
00195 return false;
00196 }
00197 if(!ctype_digit((string) $width) || $width <= 0) {
00198 $this->setError("Invalid width: '$width'");
00199 return false;
00200 }
00201
00202 if(!ctype_digit((string) $wsRows) || $height <= 0) {
00203 $this->setError("Invalid wsRows: '$wsRows'");
00204 return false;
00205 }
00206
00207 if(!ctype_digit((string) $wsCols) || $height <= 0) {
00208 $this->setError("Invalid wsCols: '$wsCols'");
00209 return false;
00210 }
00211
00212
00213 $zip = new SnapExternalFile($this->version);
00214 $zip->create($tmpFile, $fileName);
00215
00216 $appDir = $zip->unzip('applet');
00217 $appDir->setAcceptable(array("jpg", "jar", "txt", "ws", "js", "class", "png", "svg", "jpeg", "gif", "sip"));
00218 $appDir->weedFiles();
00219
00220 $this->set('width', $width);
00221 $this->set('height', $height);
00222 $this->set('wsCols', $wsCols);
00223 $this->set('wsRows', $wsRows);
00224 $this->set('worksheet', $worksheet);
00225 $this->set('additionalWorksheets', $addlWorksheets);
00226 $this->set('appDir', $appDir->getArray());
00227 $this->set('zip', $zip->getArray());
00228
00229 $html = $this->doGetHTML(array('__noprefix' => true));
00230 $html = <<<END_HTML
00231 <html>
00232 <head>
00233 <title>Agentsheets Applet</title>
00234 </head>
00235 <body>
00236 <p>
00237 $html
00238 </p>
00239 </body>
00240 </html>
00241 END_HTML;
00242 $zipPath = $zip->getPath();
00243 file_put_contents(dirname($zipPath) . '/index.html', $html);
00244 system("cd " . dirname($zipPath) . "; zip -g $zipPath index.html >/dev/null 2>&1");
00245
00246 return true;
00247 }
00248
00257 private function copyFrom($oldCM) {
00258 if($this->hasUploaded)
00259 return true;
00260
00261 $this->set('width', $oldCM->getWidth());
00262 $this->set('height', $oldCM->getHeight());
00263 $this->set('wsCols', $oldCM->getWsCols());
00264 $this->set('wsRows', $oldCM->getWsRows());
00265 $this->set('worksheet', $oldCM->getWorksheet());
00266 $this->set('additionalWorksheets', $oldCM->getAdditionalWorksheets());
00267
00268 $oldAppDir = new SnapExternalFile($this->version, $oldCM->get('appDir'));
00269 $newAppDir = $oldAppDir->copy();
00270 if(!$newAppDir) {
00271 $this->setError("Could not copy applet files");
00272 return false;
00273 }
00274
00275 $this->set('appDir', $newAppDir->getArray());
00276
00277 if(!$oldCM->get('zip')) {
00278
00279 $zipContainer = $newAppDir->getPath();
00280 $zipFile = $zipContainer . '/appletzip.zip';
00281 system("cd $zipContainer; zip -r appletzip.zip applet >/dev/null 2>&1");
00282 $newZip = new SnapExternalFile($this->version);
00283 $newZip->create($zipFile, 'appletzip.zip');
00284 unlink($zipFile);
00285 }
00286 else {
00287 $oldZip = new SnapExternalFile($this->version, $oldCM->get('zip'));
00288 $newZip = $oldZip->copy();
00289 if(!$newZip) {
00290 $newAppDir->delete();
00291 $this->setError('Could not copy zip file');
00292 return false;
00293 }
00294 }
00295
00296 $this->set('zip', $newZip->getArray());
00297 $html = $this->doGetHTML(array('__noprefix' => true));
00298 $html = <<<END_HTML
00299 <html>
00300 <head>
00301 <title>Agentsheets Applet</title>
00302 </head>
00303 <body>
00304 <p>
00305 $html
00306 </p>
00307 </body>
00308 </html>
00309 END_HTML;
00310 $zipPath = $newZip->getPath();
00311 file_put_contents(dirname($zipPath) . '/index.html', $html);
00312
00313 system("cd " . dirname($zipPath) . "; zip -g $zipPath index.html >/dev/null 2>&1");
00314
00315 $this->hasUploaded = true;
00316
00317 return true;
00318 }
00319
00329 public function setWidth($oldCM, $width) {
00330 if(!ctype_digit((string) $width) || $width <= 0) {
00331 $this->setError("Invalid width: '$width'");
00332 return false;
00333 }
00334 if(!$this->copyFrom($oldCM))
00335 return false;
00336 $this->set('width', $width);
00337 return true;
00338 }
00348 public function setHeight($oldCM, $height) {
00349 if(!ctype_digit((string) $height) || $height <= 0) {
00350 $this->setError("Invalid height: '$height'");
00351 return false;
00352 }
00353 if(!$this->copyFrom($oldCM))
00354 return false;
00355 $this->set('height', $height);
00356 return true;
00357 }
00367 public function setWsRows($oldCM, $wsRows) {
00368 if (!ctype_digit((string) $wsRows) || $wsRows <= 0) {
00369 $this->setError("Invalid wsRows: '$wsRows'");
00370 return false;
00371 }
00372 if (!$this->copyFrom($oldCM)) return false;
00373 $this->set('wsRows',$wsRows);
00374 return true;
00375 }
00385 public function setWsCols($oldCM, $wsCols) {
00386 if (!ctype_digit((string) $wsCols) || $wsCols <= 0) {
00387 $this->setError("Invalid wsCols: '$wsCols'");
00388 return false;
00389 }
00390 if (!$this->copyFrom($oldCM)) return false;
00391 $this->set('wsCols',$wsCols);
00392 return true;
00393 }
00404 public function setWorksheet($oldCM, $worksheet) {
00405 if(!is_string($worksheet) || $worksheet == "") {
00406 $this->setError("Invalid worksheet: '$worksheet'");
00407 return false;
00408 }
00409 if (!$this->copyFrom($oldCM)) return false;
00410 $this->set('worksheet', $worksheet);
00411 return true;
00412 }
00413
00423 public function setAdditionalWorksheets($oldCM, $addl) {
00424 if(!is_array($addl)) {
00425 $this->setError('Additional worksheets must be in an array');
00426 return false;
00427 }
00428 if(!$this->copyFrom($oldCM)) return false;
00429 $this->set('additionalWorksheets', $addl);
00430 return true;
00431 }
00432
00440 public function getWidth() {
00441 if(!$this->validate())
00442 return false;
00443
00444 return $this->get('width');
00445 }
00453 public function getHeight() {
00454 if(!$this->validate())
00455 return false;
00456
00457 return $this->get('height');
00458 }
00466 public function getWsCols() {
00467 if(!$this->validate()) return false;
00468 return $this->get('wsCols');
00469 }
00477 public function getWsRows() {
00478 if(!$this->validate()) return false;
00479 return $this->get('wsRows');
00480 }
00488 public function getWorksheet() {
00489 if(!$this->validate()) return false;
00490 return $this->get('worksheet');
00491 }
00492
00500 public function getAdditionalWorksheets() {
00501 if(!$this->validate()) return false;
00502 return $this->get('additionalWorksheets');
00503 }
00504
00512 public function getAppDirURL() {
00513 if(!$this->validate())
00514 return false;
00515 $file = new SnapExternalFile($this->version, $this->get('appDir'));
00516 $url = $file->getURL();
00517 return $url;
00518 }
00519
00527 public function getAppDirPath() {
00528 if(!$this->validate())
00529 return false;
00530 $file = new SnapExternalFile($this->version, $this->get('appDir'));
00531 $path = $file->getPath();
00532 return $path;
00533 }
00534
00542 public function getZipPath() {
00543 if(!$this->validate())
00544 return false;
00545 if(!$this->get('zip')) {
00546 $this->setError('This applet does not have a zip file associated with it; you will need to create a new version');
00547 return false;
00548 }
00549 $file = new SnapExternalFile($this->version, $this->get('zip'));
00550 return $file->getPath();
00551 }
00552
00560 public function getZipURL() {
00561 if(!$this->validate())
00562 return false;
00563 if(!$this->get('zip')) {
00564 $this->setError('This applet does not have a zip file associated with it; you will need to create a new version');
00565 return false;
00566 }
00567 $file = new SnapExternalFile($this->version, $this->get('zip'));
00568 return $file->getURL();
00569 }
00570
00578 public function getSize() {
00579 if(!$this->validate())
00580 return false;
00581 if(!$this->get('zip')) {
00582 $this->setError('This applet does not have a zip file associated with it; you will need to create a new version');
00583 return false;
00584 }
00585 $file = new SnapExternalFile($this->version, $this->get('zip'));
00586 return filesize($file->getPath());
00587 }
00588
00596 public function checkMedia() {
00597 if($this->get('appDir') == '')
00598 return true;
00599 if(!$this->validate())
00600 return false;
00601 $file = new SnapExternalFile($this->version, $this->get('appDir'));
00602 $path = $file->getPath();
00603 if(is_readable($path) && is_dir($path))
00604 return true;
00605 else
00606 return false;
00607 }
00608
00616 public function updateFileList() {
00617 if(!$this->validate())
00618 return 'no files to insert';
00619 SnapDBI::startTransaction();
00620 $file = new SnapExternalFile($this->version, $this->get('appDir'));
00621 $ret = $file->updateDB();
00622 if($ret)
00623 SnapDBI::commitTransaction();
00624 else {
00625 $ret = 'database failure';
00626 SnapDBI::cancelTransaction();
00627 }
00628 return $ret;
00629 }
00630 }
00631
00632 ?>