From 85dd8266c501e75c6dd417b7a8668c1d7870d637 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 23 Jul 2015 09:23:35 -0400 Subject: [PATCH] Setup to get sub page id for import get's all sub ids for the page given (if the number in numeric only) --- controllers/Import.php | 89 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 16 deletions(-) diff --git a/controllers/Import.php b/controllers/Import.php index 9629851..da3d4f0 100644 --- a/controllers/Import.php +++ b/controllers/Import.php @@ -276,7 +276,17 @@ 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; } @@ -305,7 +315,7 @@ 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))); @@ -364,11 +374,11 @@ $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); @@ -380,14 +390,42 @@ 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']}) @@ -443,7 +481,7 @@ $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); } @@ -454,7 +492,7 @@ $page['srcs'] = array(); $paraStmt->bindParam(':page', $page['id'], PDO::PARAM_INT); $paraStmt->execute(); - $paragraphs = $paraStmt->fetchAll(PDO::FETCH_ASSOC); + $paragraphs = $paraStmt->fetchAll(); //echo '
' . print_r($paragraphs, true) . '
'; //exit; $primaryAlign = 'right'; @@ -492,7 +530,7 @@ $page['images'][] = $page['image']; $page['pageContent'] .= $this->_displayImage($page, $primaryAlign); } - $page['pageContent'] = $page['description']; + $page['pageContent'] .= $page['description']; } foreach ($paragraphs as $paragraph) { @@ -538,7 +576,7 @@ } $page['pageContent'] .= $paragraph['description']; preg_match_all("/' . print_r($matches, true) . ''; + //echo '
' . print_r($matches, true) . '
'; for ($i = 0; $i < count($matches[0]); $i++) { if ($matches[1][$i]) { @@ -549,14 +587,17 @@ $page['srcs'][] = $imgName; } } + //var_dump($paragraph['id']); $fileStmt->bindParam(':pid', $paragraph['id'], PDO::PARAM_INT); $fileStmt->execute(); while ($file = $fileStmt->fetch()) { + //echo '
Files: ' . print_r($file, true) . '
'; $page['files'][] = $file['filename']; $page['pageContent'] .= $this->_displayFile($file); } ++$iterator; } + //echo '
Files: ' . print_r($page['files'], true) . '
'; } return $data; } @@ -660,16 +701,27 @@ global $wpdb; $this->_connect(); echo '

Replace keywords

'; - $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 '
' . print_r($page, true) .  '
'; // find out which page (wp) this needs to be $wpPost = get_page_by_title($page['navigation_name']); + if (!$wpPost) { + continue; + } //echo '
' . print_r($wpPost, true) .  '
'; echo '
' . print_r(get_permalink($wpPost), true) .  '
'; $replaceUrl = '' . $page['navigation_name'] . ''; @@ -808,6 +860,11 @@ case 3: $this->_replaceUrls(); break; + case 4: + echo '

Test getting page sub ids

'; + $subPageIds = $this->_getSubPageIds($this->_options['include_pages']); + echo '
'.print_r($subPageIds, true).'
'; + break; } $this->_footer(); -- 2.17.1