Setup to get sub page id for import
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 23 Jul 2015 13:23:35 +0000 (09:23 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 23 Jul 2015 13:23:35 +0000 (09:23 -0400)
get's all sub ids for the page given (if the number in numeric only)

controllers/Import.php

index 9629851..da3d4f0 100644 (file)
 
         private function _displayFile($data)
         {
-            $file_id = $this->_handleMediaFile($data['filename']);
+            if ($this->_options['toolbox_paragraphs_table'] == 'pages') {
+                $file_id = $this->_handleMediaFile($data['filename']);
+            } else {
+                $file_id = $this->_handleMediaFile(
+                    $data['filename'],
+                    '',
+                    0,
+                    $this->_options['site_url'] . 'uploads/'
+                );
+            }
+
             if (!$file_id) {
                 return false;
             }
                 if (!(($uploads = wp_upload_dir()) && false === $uploads['error'])) {
                     return new WP_Error('upload_error', $uploads['error']);
                 }
-                $filename = $this->_fetchRemoteImage($file, $uploads['path']);
+                $filename = $this->_fetchRemoteImage($file, $uploads['path'], $baseUrl);
                 $new_file = $uploads['path'] . '/' . $filename;
                 $url      = $uploads['url'] . '/' . $filename;
                 $return   = apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => wp_check_filetype($file, null)));
             $filename = wp_unique_filename($path, $file);
 
             $fp = fopen($path . '/' . $filename, 'w+');
-            $fileUrl = ($baseUrl) ? $baseUrl . $file : $this->_options['toolbox_image_url'] . $file;
+            $fileUrl = ($baseUrl) ? $baseUrl . '/' . $file : $this->_options['toolbox_image_url'] . $file;
             $ch = curl_init($fileUrl);
             curl_setopt($ch, CURLOPT_TIMEOUT, 50);
             curl_setopt($ch, CURLOPT_FILE, $fp);
-            //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
             $imgData = curl_exec($ch);
             $httpCode = curl_getinfo($ch);
             curl_close($ch);
             return $filename;
         }
 
+        private function _getSubPageIds($parent)
+        {
+            $this->_connect();
+            $pageIds = array();
+            $sql = "
+            SELECT id
+              FROM {$this->_options['toolbox_page_table']}
+             WHERE parent = :parent";
+            $stmt = $this->_dbh->prepare($sql);
+            $stmt->bindParam(':parent', $parent, PDO::PARAM_INT);
+            $stmt->execute();
+            while ($page = $stmt->fetch()) {
+                $pageIds[] = $page['id'];
+                $subPageIds = $this->_getSubPageIds($page['id']);
+                if (!empty($subPageIds)) {
+                    $pageIds = array_merge($pageIds, $subPageIds);
+                }
+            }
+            return $pageIds;
+        }
+
         private function _getWhereSql()
         {
             $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']})";
+                if (filter_var($this->_options['include_pages'], FILTER_VALIDATE_INT)) {
+                    $subPageIds = $this->_getSubPageIds($this->_options['include_pages']);
+                    if (!empty($subPageIds)) {
+                        $where[] = "(id = {$this->_options['include_pages']}"
+                        . " OR id IN (" . implode(',', $subPageIds) . ") )";
+                    }
+
+                } else {
+                    $where[] = "id IN ({$this->_options['include_pages']})";
+                }
             }
             if ($this->_options['exclude_pages']) {
                 $where[] = "id NOT IN ({$this->_options['exclude_pages']})
                 $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)
+                   WHERE bus_id = :pid
                 ORDER BY bus_id,pos";
                 $fileStmt = $this->_dbh->prepare($fileSql);
             }
                 $page['srcs'] = array();
                 $paraStmt->bindParam(':page', $page['id'], PDO::PARAM_INT);
                 $paraStmt->execute();
-                $paragraphs     = $paraStmt->fetchAll(PDO::FETCH_ASSOC);
+                $paragraphs     = $paraStmt->fetchAll();
             //echo '<pre>' . print_r($paragraphs, true) . '</pre>';
             //exit;
                 $primaryAlign   = 'right';
                         $page['images'][]     = $page['image'];
                         $page['pageContent'] .= $this->_displayImage($page, $primaryAlign);
                     }
-                    $page['pageContent'] = $page['description'];
+                    $page['pageContent'] .= $page['description'];
                 }
 
                 foreach ($paragraphs as $paragraph) {
                     }
                     $page['pageContent'] .= $paragraph['description'];
                     preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $paragraph['description'], $matches);
-            echo '<pre>' . print_r($matches, true) . '</pre>';
+            //echo '<pre>' . print_r($matches, true) . '</pre>';
 
                     for ($i = 0; $i < count($matches[0]); $i++) {
                         if ($matches[1][$i]) {
                             $page['srcs'][] = $imgName;
                         }
                     }
+                    //var_dump($paragraph['id']);
                     $fileStmt->bindParam(':pid', $paragraph['id'], PDO::PARAM_INT);
                     $fileStmt->execute();
                     while ($file = $fileStmt->fetch()) {
+            //echo '<pre>Files: ' . print_r($file, true) . '</pre>';
                         $page['files'][] = $file['filename'];
                         $page['pageContent'] .= $this->_displayFile($file);
                     }
                     ++$iterator;
                 }
+                //echo '<pre>Files: ' . print_r($page['files'], true) . '</pre>';
             }
             return $data;
         }
             global $wpdb;
             $this->_connect();
             echo '<p>Replace keywords</p>';
-            $sql = "
-              SELECT id,navigation_name,keyword
-                FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']}
-               WHERE keyword != ''
-            ORDER BY parent,pos";
+            if ($this->_options['toolbox_page_table'] == 'pages') {
+                $sql = "
+                  SELECT id,navigation_name,keyword
+                    FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']}
+                   WHERE keyword != ''
+                ORDER BY parent,pos";
+            } else {
+                $sql = "
+                  SELECT id,category as navigation_name,keyword
+                    FROM {$this->_options['toolbox_schema']}.{$this->_options['toolbox_page_table']}
+                   WHERE keyword != ''
+                ORDER BY parent,pos";
+            }
             $stmt = $this->_dbh->query($sql);
             while ($page = $stmt->fetch()) {
                 echo '<pre>' . print_r($page, true) .  '</pre>';
                 // find out which page (wp) this needs to be
                 $wpPost = get_page_by_title($page['navigation_name']);
+                if (!$wpPost) {
+                    continue;
+                }
                 //echo '<pre>' . print_r($wpPost, true) .  '</pre>';
                 echo '<pre>' . print_r(get_permalink($wpPost), true) .  '</pre>';
                 $replaceUrl = '<a href="'.get_permalink($wpPost->ID).'">' . $page['navigation_name'] . '</a>';
             case 3:
                 $this->_replaceUrls();
                 break;
+            case 4:
+                echo '<p>Test getting page sub ids</p>';
+                $subPageIds = $this->_getSubPageIds($this->_options['include_pages']);
+                echo '<pre>'.print_r($subPageIds, true).'</pre>';
+                break;
             }
 
             $this->_footer();