Hej allesammen!
Ved godt at i nok er ved at være trætte af de her indlæg og skal nok (prøve) at love at dette bliver et af de sidste, er bare nød til at have at vide om jeg har fat i det rigtige.
Har indtil videre ikke haft brug for en View class til filer, så har indtil videre bare lavet Model og Controller.
File_Model:
- <?php
- /**
- * Filehandler - model
- * @version 0.1
- */
- class FBFile_Model
- {
-
- /**
- * Handles to file
- */
- private $fileHandles = array();
-
-
- /**
- * Active file
- */
- private $activeFile = "";
-
- /**
- * Default file mode
- */
- private $defaultMode = "";
-
- /**
- * Construct
- * Synonym to open()
- */
- public function __construct($filemode = "")
- {
- $this->defaultMode = (empty($filemode)) ? "r+" : $filemode;
- }
-
- /**
- * Sets default open mode
- * @param String filemode
- */
- public function setDefaultMode($mode)
- {
- $this->defaultMode = $mode;
- }
-
- /**
- * Opens up a file
- * @param String filename
- * @param String mode
- * @param Bool Set active file
- */
- public function open($filename, $mode = "", $setActive = true)
- {
- $mode = (empty($mode)) ? $this->defaultMode : $mode;
- $this->fileHandles[$filename] = fopen($filename, $mode);
- if($setActive)
- $this->activeFile = $filename;
-
- return $this->fileHandles[$filename];
- }
-
- public function write($content, $filename_key = "")
- {
- if(!empty($filename_key))
- return fwrite($this->fileHandles[$filename_key], $content);
- else
- return fwrite($this->fileHandles[$this->activeFile], $content);
- }
-
- /**
- * Close a file
- * @param String filename-key
- *
- * ERROR IN FUNCTION
- */
- public function close($filename_key = "")
- {
- if(!empty($filename_key))
- return fclose($this->fileHandles[$filename_key]);
- else
- return fclose($this->fileHandles[$this->activeFile]);
- }
-
- /**
- * Returns the raw filedata
- * @param String filename-key
- */
- public function rawData($filename_key = "")
- {
- if(!empty($filename_key))
- return fread($this->fileHandles[$filename_key], filesize($filename_key));
- else
- return fread($this->fileHandles[$this->activeFile], filesize($this->activeFile));
- }
-
- /**
- * Adds a file to handle-array
- * @param Filehandle
- */
- public function addFile($filename, $fileHandle, $set_active = false)
- {
- $this->fileHandles[$filename] = $fileHandle;
- if($set_active)
- $this->activeFile = count($this->fileHandles)-1;
- }
-
- /**
- * Return a filehandle
- * @return Filehandle
- */
- public function getFileHandle($filename_key = "")
- {
- if(empty($filename_key))
- return $this->fileHandles[$this->activeFile];
- else
- return $this->fileHandles[$filename_key];
- }
-
- /**
- * Decontruct. Close all files
- * @return void
- */
- public function __destruct()
- {
- foreach($this->fileHandles as $fileHandle)
- fclose($fileHandle);
- }
-
- }
- ?>
DocX Controller:
- <?php
- /**
- * Filehandler for DocX - controller
- * @version 0.1
- */
- class FBFile_DocX
- {
- /**
- * The DocX File
- */
- private $docXFile;
-
- /**
- * The Model class
- */
- private $fileModel;
-
- /**
- * Should the DocX file be deleted
- */
- private $deleteDocX = false;
-
- /**
- * Contruct - clone of open()
- * @param String filename
- */
- public function __construct($deleteDocX = false)
- {
- $this->fileModel = new FBFile_Model();
-
- if($deleteDocX)
- $deleteDocX = true;
- }
-
- /**
- * Opens a file
- * @param String filename
- */
- public function open($filename)
- {
- if(!$this->deleteDocX)
- {
- $path_parts = pathinfo($filename);
- do
- {
- $newFilename = $path_parts["dirname"].$path_parts["filename"].mt_rand().".".$path_parts["extension"];
- } while(file_exists($newFilename));
- copy($filename, $newFilename);
- $file = $newFilename;
- }
- else
- $file = $filename;
-
- $zip = new ZipArchive();
- if(!$zip->open($file))
- {
- trigger_error("Couldn't open DocX file: " . $file, E_USER_ERROR);
- return false;
- }
- $this->docXFile = $zip->getFromName("word/document.xml");
- }
-
- /**
- * Returns raw text
- */
- public function rawText()
- {
- $aText = strip_tags($this->docXFile, "<w:p>");
- $aText = str_replace("</w:p>", "<br />", $aText);
- return strip_tags($aText, "<br>");
- }
- }
- ?>
Koden er ikke færdig...
PS: Hvis der er nogen der lige kan spotte fejlen i close() ville det være super, den siger at resource ikke er en valid stream... Men det er den?!