WIP: redo for older toolboxes
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 21 Jul 2015 21:05:35 +0000 (17:05 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 21 Jul 2015 21:05:35 +0000 (17:05 -0400)
Have to work on ht_images for src urls

controllers/Admin.php
controllers/Import.php

index 3bbf822..354072e 100644 (file)
@@ -157,6 +157,11 @@ class GlmWPImporter_Admin_Controller
                 'label' => 'Include Pages',
                 'type'  => 'text'
             ),
+            array(
+                'name'  => 'parent_page',
+                'label' => 'Parent Page',
+                'type'  => 'text'
+            ),
         );
 
         foreach ($fieldNames as $field) {
index 54e5641..9629851 100644 (file)
             } else {
                 echo '<p>All Set!</p>';
                 $this->_connect();
-                $sql = "SELECT * FROM toolbox.pages";
-                $data = $this->_dbh->query($sql)->fetchAll();
-                printf('<p>Found %d Pages</p>', count($data));
+                $WHERE = $this->_getWhereSql();
+                $sql = "
+                SELECT count(*)
+                  FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']}
+                 $WHERE";
+                echo '<pre>' . $sql . '</pre>';
+                $totalPages = $this->_dbh->query($sql)->fetchColumn();
+                printf('<p>Found %d Pages</p>', $totalPages);
                 echo '<p><a href="admin.php?import=toolbox&amp;step=2">Part 2</a></p>';
             }
         }
          */
         private function _displayImage($data, $alignment)
         {
-            $img_id  = $this->_handleMediaFile($data['image'], $data['caption']);
+            if (!isset($data['image'])) {
+                return false;
+            }
+            if (isset($data['caption'])) {
+                $img_id  = $this->_handleMediaFile($data['image'], $data['caption']);
+            } else if ($data['imagename']) {
+                $img_id  = $this->_handleMediaFile($data['image'], $data['imagename']);
+            } else {
+                $img_id  = $this->_handleMediaFile($data['image']);
+            }
             if (!$img_id) {
                 return false;
             }
             $image   = wp_get_attachment_image_src($img_id, 'medium');
             $img_url = $image[0];
-            if ($data['caption']) {
+            $content = '';
+            if (isset($data['caption']) && $data['caption']) {
                 $content .= '[caption id="attachment_'.$img_id.'" align="align'.$alignment.'"]'
                     .'<img class="align'.$alignment.' size-medium wp-image-' . $img_id . '" '
                     . ' src="' . $img_url . '"> '
                     . $data['caption'] . '[/caption]';
+            } else if (isset($data['imagename']) && $data['imagename']) {
+                $content .= '[caption id="attachment_'.$img_id.'" align="align'.$alignment.'"]'
+                    .'<img class="align'.$alignment.' size-medium wp-image-' . $img_id . '" '
+                    . ' src="' . $img_url . '"> '
+                    . $data['imagename'] . '[/caption]';
             } else {
                 $content .= '<img class="align'.$alignment.' size-medium wp-image-' . $img_id . '" '
                     . ' src="' . $img_url . '">';
                 if ($post_id) {
                     $post = get_post($post_id);
                 }
-                if (!(($uploads = wp_upload_dir($time)) && false === $uploads['error'])) {
+                if (!(($uploads = wp_upload_dir()) && false === $uploads['error'])) {
                     return new WP_Error('upload_error', $uploads['error']);
                 }
                 $filename = $this->_fetchRemoteImage($file, $uploads['path']);
             return $filename;
         }
 
-        /**
-         * _fetchAllPages
-         *
-         * Fetch all the toolbox pages
-         *
-         * @access private
-         * @return void
-         */
-        private function _fetchAllPages($limit = null, $offset = 0)
+        private function _getWhereSql()
         {
-            $this->_connect();
+            $WHERE = '';
             $where = array();
             if ($this->_options['include_pages']) {
-                $where[] = "id IN ({$this->_options['include_pages']})
-                    OR parent IN ({$this->_options['include_pages']}) ";
+                //$where[] = "id IN ({$this->_options['include_pages']})
+                    //OR parent IN ({$this->_options['include_pages']}) ";
+                $where[] = "id IN ({$this->_options['include_pages']})";
             }
             if ($this->_options['exclude_pages']) {
                 $where[] = "id NOT IN ({$this->_options['exclude_pages']})
             if (!empty($where)) {
                 $WHERE = ' WHERE ' . implode(' AND ', $where);
             }
+            return $WHERE;
+        }
+
+        /**
+         * _fetchAllPages
+         *
+         * Fetch all the toolbox pages
+         *
+         * @access private
+         * @return void
+         */
+        private function _fetchAllPages($limit = null, $offset = 0)
+        {
+            $this->_connect();
+            $WHERE = $this->_getWhereSql();
             $pageSql = "
               SELECT *
                 FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']}
             ORDER BY parent,pos";
             $pageSql .= " LIMIT $limit OFFSET $offset";
             $pageData = $this->_dbh->query($pageSql)->fetchAll(PDO::FETCH_ASSOC);
-            $paragraphSql = "
-              SELECT *
-                FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']}
-               WHERE page = :page
-            ORDER BY pos";
-            $paraStmt = $this->_dbh->prepare($paragraphSql);
-            $fileSql = "
-              SELECT *
-                FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_files_table']}
-               WHERE paragraph = :pid
-            ORDER BY pos";
-            $fileStmt = $this->_dbh->prepare($fileSql);
+            if ($this->_options['toolbox_page_table'] == 'pages') {
+                $paragraphSql = "
+                  SELECT *
+                    FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']}
+                   WHERE page = :page
+                ORDER BY pos";
+                $paraStmt = $this->_dbh->prepare($paragraphSql);
+                $fileSql = "
+                  SELECT *
+                    FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_files_table']}
+                   WHERE paragraph = :pid
+                ORDER BY pos";
+                $fileStmt = $this->_dbh->prepare($fileSql);
+            } else {
+                $paragraphSql = "
+                  SELECT b.*
+                    FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_paragraphs_table']} b
+                         LEFT OUTER JOIN bus_category_bus bcb ON (bcb.busid = b.id)
+                   WHERE bcb.catid = :page
+                ORDER BY bcb.pos";
+                $paraStmt = $this->_dbh->prepare($paragraphSql);
+                $fileSql = "
+                  SELECT *
+                    FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_files_table']}
+                   WHERE bus_id IN (SELECT bus_id FROM bus_category_bus WHERE catid = :pid)
+                ORDER BY bus_id,pos";
+                $fileStmt = $this->_dbh->prepare($fileSql);
+            }
             $data = $this->_dbh->query($pageSql)->fetchAll();
+            //echo '<pre>' . print_r($data, true) . '</pre>';
+            //exit;
             foreach ($data as &$page) {
                 $page['srcs'] = array();
                 $paraStmt->bindParam(':page', $page['id'], PDO::PARAM_INT);
                 $paraStmt->execute();
                 $paragraphs     = $paraStmt->fetchAll(PDO::FETCH_ASSOC);
+            //echo '<pre>' . print_r($paragraphs, true) . '</pre>';
+            //exit;
                 $primaryAlign   = 'right';
                 $secondaryAlign = 'right';
                 $alternateImg   = false;
                     $secondaryAlign = 'left';
                     break;
                 }
-                $iterator = 1;
                 $page['pageContent'] = '';
+                $iterator = 1;
+                if ($this->_options['toolbox_page_table'] == 'bus_category') {
+                    $page['pageContent'] .= '<h1>'.$page['category'].'</h1>';
+                    ++$iterator;
+                    if ($page['image']) {
+                        $page['images'][]     = $page['image'];
+                        $page['pageContent'] .= $this->_displayImage($page, $primaryAlign);
+                    }
+                    $page['pageContent'] = $page['description'];
+                }
+
                 foreach ($paragraphs as $paragraph) {
                     switch ($iterator) {
                     case 1:
-                        if ($paragraph['title']) {
+                        if (isset($paragraph['title']) && $paragraph['title']) {
                             $page['pageContent'] .= '<h1>'.$paragraph['title'].'</h1>';
                         }
-                        if ($paragraph['image']) {
+                        if (isset($paragraph['image']) && $paragraph['image']) {
                             $page['images'][]     = $paragraph['image'];
                             $page['pageContent'] .= $this->_displayImage($paragraph, $primaryAlign);
                         }
                         break;
                     case 2:
-                        if ($paragraph['title']) {
+                        if (isset($paragraph['name']) && $paragraph['name']) {
+                            $page['pageContent'] .= '<h2>'.$paragraph['name'].'</h2>';
+                        }
+                        if (isset($paragraph['title']) && $paragraph['title']) {
                             $page['pageContent'] .= '<h2>'.$paragraph['title'].'</h2>';
                         }
-                        if ($paragraph['image']) {
+                        if (isset($paragraph['image']) && $paragraph['image']) {
                             $page['images'][]     = $paragraph['image'];
                             $page['pageContent'] .= $this->_displayImage($paragraph, $secondaryAlign);
                         }
                         break;
                     default:
-                        if ($paragraph['title']) {
+                        if (isset($paragraph['name']) && $paragraph['name']) {
+                            $page['pageContent'] .= '<h2>'.$paragraph['name'].'</h2>';
+                        }
+                        if (isset($paragraph['title']) && $paragraph['title']) {
                             $page['pageContent'] .= '<h2>'.$paragraph['title'].'</h2>';
                         }
-                        if ($alternateImg && $page['template'] == 3) {
+                        if (isset($paragraph['template']) && $alternateImg && $page['template'] == 2) {
                             $align = ($iterator%2 == 0) ? 'left' : 'right';
                         } else {
                             $align = $secondaryAlign;
                         }
-                        if ($paragraph['image']) {
+                        if (isset($paragraph['image']) && $paragraph['image']) {
                             $page['images'][]     = $paragraph['image'];
                             $page['pageContent'] .= $this->_displayImage($paragraph, $align);
                         }
                     }
                     $page['pageContent'] .= $paragraph['description'];
                     preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $paragraph['description'], $matches);
+            echo '<pre>' . print_r($matches, true) . '</pre>';
+
                     for ($i = 0; $i < count($matches[0]); $i++) {
-                        $newSrc  = $this->_getDescriptionImage($matches[1][$i]);
-                        $parsUrl = parse_url($newSrc);
-                        $imgName = basename($parsUrl['path']);
-                        $page['pageContent'] = str_replace($matches[1][$i], $newSrc, $page['pageContent']);
-                        $page['srcs'][] = $imgName;
+                        if ($matches[1][$i]) {
+                            $newSrc  = $this->_getDescriptionImage($matches[1][$i]);
+                            $parsUrl = parse_url($newSrc);
+                            $imgName = basename($parsUrl['path']);
+                            $page['pageContent'] = str_replace($matches[1][$i], $newSrc, $page['pageContent']);
+                            $page['srcs'][] = $imgName;
+                        }
                     }
                     $fileStmt->bindParam(':pid', $paragraph['id'], PDO::PARAM_INT);
                     $fileStmt->execute();
         private function _readOptions()
         {
             echo '<pre>' . print_r($this->_options, true) . '</pre>';
+            echo '<pre>' . print_r(get_option(GLM_WP_IMPORT_POST_OPTION, array()), true) . '</pre>';
         }
 
         /**
             // grab all pages and build the post array
             $start = filter_var($_REQUEST['start'], FILTER_VALIDATE_INT);
             $pages = $this->_fetchAllPages(PAGES_PER_LOAD, $start);
+            //echo '<pre>' . print_r($pages, true) . '</pre>';
+            //exit;
             $numPagesImported = count($pages);
 
             foreach ($pages as $page) {
                 if ($page['parent']) {
 
                 }
+                $pageName = (isset($page['navigation_name']))
+                    ? $page['navigation_name']
+                    : $page['category'];
+                // if you don't find the parent page id then use the default given or 0
+                $parent = isset($this->_post[$page['parent']])
+                    ? $this->_post[$page['parent']]
+                    : (($this->_options['parent_page']) ? $this->_options['parent_page'] : 0);
                 $post = array(
                     'post_content'   => $page['pageContent'],
-                    'post_name'      => $page['navigation_name'],// slug ?
-                    'post_title'     => $page['navigation_name'],
+                    'post_name'      => $pageName,// slug ?
+                    'post_title'     => $pageName,
                     'post_status'    => 'publish',
                     'post_type'      => 'page',
                     'post_author'    => 'steve',
                     'ping_status'    => 'closed',
-                    'post_parent'    => $this->_post[$page['parent']],
+                    'post_parent'    => $parent,
                     'menu_order'     => $page['pos'],
                     'comment_status' => 'closed',
                 );