From dac6865e7bc0efffb9743f924e3fa0ff3c8f1372 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 5 Feb 2010 21:36:40 +0000 Subject: [PATCH] add import --- Toolkit/CKImages/database/Import.php | 138 +++++++++++++++++++ Toolkit/CKImages/database/importHtImages.php | 6 + 2 files changed, 144 insertions(+) create mode 100755 Toolkit/CKImages/database/Import.php create mode 100755 Toolkit/CKImages/database/importHtImages.php diff --git a/Toolkit/CKImages/database/Import.php b/Toolkit/CKImages/database/Import.php new file mode 100755 index 0000000..ca5f7e9 --- /dev/null +++ b/Toolkit/CKImages/database/Import.php @@ -0,0 +1,138 @@ +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 '
'.print_r($files, true).'
'; + foreach ($files as $folderName => $file) { + if (is_array($file)) { + $fKey = key($file); + $folderToAdd = ereg_replace("/$", "", $fKey); + //echo '
'.print_r($folderToAdd, true).'
'; + $folder = $this->addFolderToDatabase($folderToAdd, $parent); + //echo '
'.print_r($folder, true).'
'; + //var_dump($files[$folderName][$fKey]); + // echo '
'.print_r($file[$folderName][$fKey], true).'
'; + $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 '
'; + $d = dir($dirName); + while (false !== ($entry = $d->read())) { + if (!ereg("^\.|^CVS", $entry)) { + $entryPath = $this->pathToImages . $path . $entry; + //var_dump($entryPath); echo '
'; + 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 '
'.print_r($files, true).'
'; + $processedFiles = $this->processFilesArray($files['']); + //echo '
'.print_r($processedFiles, true).'
'; + } + //}}} + // {{{ 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 index 0000000..4e709ce --- /dev/null +++ b/Toolkit/CKImages/database/importHtImages.php @@ -0,0 +1,6 @@ +runImport(); +?> -- 2.17.1