add import
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 5 Feb 2010 21:36:40 +0000 (21:36 +0000)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 5 Feb 2010 21:36:40 +0000 (21:36 +0000)
Toolkit/CKImages/database/Import.php [new file with mode: 0755]
Toolkit/CKImages/database/importHtImages.php [new file with mode: 0755]

diff --git a/Toolkit/CKImages/database/Import.php b/Toolkit/CKImages/database/Import.php
new file mode 100755 (executable)
index 0000000..ca5f7e9
--- /dev/null
@@ -0,0 +1,138 @@
+<?php
+class Toolkit_CKImages_database_Import
+{
+    // {{{ Class Properties
+    protected $dbh;
+    protected $pathToImages;
+    protected $is;
+    protected $urlPathToImages;
+
+    // }}}
+    // {{{ __construct()
+    function __construct(PDO $pdo)
+    {
+        $this->dbh             = $pdo;
+        $this->pathToImages    = BASE . 'images/ht_images/';
+        $this->urlPathToImages = 'http://www.troutcreek.com/images/ht_images/';
+        $this->is              = new Toolkit_Image_Server();
+    }
+    // }}}
+    // {{{ addFolderToDatabase()
+    function addFolderToDatabase($folder, $parent)
+    {
+        static $stmt;
+        if (!$stmt) {
+            $sql = "
+            INSERT INTO ckeditor_folders
+            (name, parent)
+            VALUES
+            (:name, :parent)
+            RETURNING id";
+            $stmt = $this->dbh->prepare($sql);
+        }
+        try {
+            $stmt->bindParam(":name",   $folder, PDO::PARAM_STR);
+            $stmt->bindParam(":parent", $parent, PDO::PARAM_STR);
+            $stmt->execute();
+            return $stmt->fetchColumn();
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }
+    // }}}
+    // {{{ addImageToDatabase()
+    function addImageToDatabase($file, $folder)
+    {
+        static $stmt;
+        if (!$stmt) {
+            $sql = "
+            INSERT INTO ckeditor_images
+            (file_name, name_on_disk, original_width, original_height, folder)
+            VALUES
+            (:file_name, :name_on_disk, :original_width, :original_height, :folder)";
+            $stmt = $this->dbh->prepare($sql);
+        }
+        try {
+            $stmt->bindParam(":file_name",       $file['file_name'],       PDO::PARAM_STR);
+            $stmt->bindParam(":name_on_disk",    $file['name_on_disk'],    PDO::PARAM_STR);
+            $stmt->bindParam(":original_width",  $file['original_width'],  PDO::PARAM_STR);
+            $stmt->bindParam(":original_height", $file['original_height'], PDO::PARAM_STR);
+            $stmt->bindParam(":folder",          $folder,                  PDO::PARAM_INT);
+            $stmt->execute();
+        } catch(PDOException $e) {
+            Toolkit_Common::handleError($e);
+        }
+    }// }}}
+    // {{{ processFilesArray
+    function processFilesArray($files, $parent = 1)
+    {
+        //echo '<pre>'.print_r($files, true).'</pre>';
+        foreach ($files as $folderName => $file) {
+            if (is_array($file)) {
+                $fKey = key($file);
+                $folderToAdd     = ereg_replace("/$", "", $fKey);
+        //echo '<pre>'.print_r($folderToAdd, true).'</pre>';
+                $folder          = $this->addFolderToDatabase($folderToAdd, $parent);
+        //echo '<pre>'.print_r($folder, true).'</pre>';
+        //var_dump($files[$folderName][$fKey]);
+       // echo '<pre>'.print_r($file[$folderName][$fKey], true).'</pre>';
+                $processedFile[] = $this->processFilesArray($files[$folderName][$fKey], $folder);
+            } else {
+                $processedFile[] = $this->sendToImageServer($file, $parent);
+            }
+        }
+        return $processedFile;
+    }
+    // }}}
+    // {{{ readImageDir()
+    function readImageDir($path = '')
+    {
+        $dirName = $this->pathToImages . ereg_replace("/$", "", $path);
+        //var_dump($dirName);echo '<br />';
+        $d = dir($dirName);
+        while (false !== ($entry = $d->read())) {
+            if (!ereg("^\.|^CVS", $entry)) {
+                $entryPath = $this->pathToImages . $path . $entry;
+                //var_dump($entryPath); echo '<br />';
+                if ($dirCheck = is_dir($entryPath)) {
+                    $files[$path][] = $this->readImageDir($path . $entry . '/');
+                } else if ($fileCheck = is_file($entryPath)) {
+                    $files[$path][] = $path . $entry;
+                }
+            }
+        }
+        return $files;
+    }
+    // }}}
+    //{{{ runImport()
+    function runImport()
+    {
+        $files = $this->readImageDir();
+        echo '<pre>'.print_r($files, true).'</pre>';
+        $processedFiles = $this->processFilesArray($files['']);
+        //echo '<pre>'.print_r($processedFiles, true).'</pre>';
+    }
+    //}}}
+    // {{{ sendToImageServer()
+    function sendToImageServer($file, $folder)
+    {
+        $file_name    = basename($file);
+        $size         = getImageSize($this->pathToImages . $file);
+        try {
+            $name_on_disk = $this->is->imageUpload(
+                $this->urlPathToImages . '/' . ereg_replace(' ', '%20', $file)
+            );
+        } catch (Exception $e) {
+            Toolkit_Common::handleError($e);
+        }
+        $image        = array(
+            'name_on_disk'    => $name_on_disk,
+            'file_name'       => $file_name,
+            'original_width'  => $size[0], 
+            'original_height' => $size[1], 
+        );
+        $this->addImageToDatabase($image, $folder);
+    }
+    // }}}
+}
+?>
diff --git a/Toolkit/CKImages/database/importHtImages.php b/Toolkit/CKImages/database/importHtImages.php
new file mode 100755 (executable)
index 0000000..4e709ce
--- /dev/null
@@ -0,0 +1,6 @@
+<?php
+require_once '../../../setup.phtml';
+$dbh = Toolkit_Database::getInstance();
+$import = new Toolkit_CKImages_database_Import($dbh);
+$import->runImport();
+?>