--- /dev/null
+<?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);
+ }
+ // }}}
+}
+?>