Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL);
- ini_set('display_errors', TRUE);
- ini_set('display_startup_errors', TRUE);
- /**
- * @file
- * The PHP page that serves all page requests on a Drupal installation.
- *
- * The routines here dispatch control to the appropriate handler, which then
- * prints the appropriate page.
- *
- * All Drupal code is released under the GNU General Public License.
- * See COPYRIGHT.txt and LICENSE.txt.
- */
- /**
- * Root directory of Drupal installation.
- */
- define('DRUPAL_ROOT', getcwd());
- require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
- drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
- $out = array();
- $res = Database::getConnection('default', 'parser')
- ->query("select au.title as author," .
- "b.id as id," .
- "au.id as aid," .
- "CONCAT_WS('--', b.category_650_1,b.category_650_2,b.category_653_1,b.category_653_2) as cat," .
- "b.note as stitle," .
- "bt.title_245 as title," .
- "b.desc_260 as publisher," .
- "b.desc_41 as language," .
- "b.pages as pages," .
- "b.is_show as is_show," .
- "b.size as size," .
- "b.k_price as k_price," .
- "b.price as price," .
- "b.y30_j_1 as s1," .
- "b.y30_j_2 as s2," .
- "b.y30_j_3 as s3," .
- "b.y30_j_4 as s4," .
- "b.y30_j_5 as s5," .
- "b.desc_300_c as descsize," .
- "b.desc_300 as descpages" .
- " from book b" .
- " left outer join author au on au.id = b.author_id" .
- " left outer join book_title bt on bt.book_id = b.id" .
- " order by b.id");
- foreach($res as $k=>$entity) {
- $res_cat = Database::getConnection('default', 'parser')
- ->query("select c.title as title, c.id as cid" .
- " from category c inner join book_category bc on bc.category_id = c.id where bc.book_id = :bc",
- array(':bc' => $entity->id));
- foreach($res_cat as $cat_entity) {
- $entity->rubrics[] = $cat_entity;
- }
- import_data($entity);
- }
- function import_data($q){
- if(!empty($q->title)) {
- $title = $q->title;
- } else {
- $pos = mb_strpos($q->stitle, '"');
- $rpos = mb_strrpos($q->stitle, '"');
- $title = mb_substr($q->stitle, $pos+1, $rpos - $pos - 1);
- }
- $sku = db_select('commerce_product', 'c')
- ->fields('c', array('product_id'))
- ->condition('sku', $q->id, '=')
- ->execute()->fetchField();
- if ($sku){
- $product = commerce_product_load_by_sku($sku);
- } else {
- $product = commerce_product_new('product');
- $product->status = $q->is_show;
- $product->uid = '1';
- $product->created = $product->changed = time();
- $product->is_new = TRUE;
- }
- $product->title = word_substr($title,255);
- $product->sku = $q->id;
- $product->created = $product->changed = time();
- $total_amount = 100 * variable_get('default_price',1);
- $product->field_page_price['und'][0]['amount'] = $total_amount;
- $product->field_page_price['und'][0]['currency_code'] = commerce_default_currency();
- $product->commerce_price['und'][0]['amount'] = $q->price > 1 ? $q->price * 100 : 100;
- $product->commerce_price['und'][0]['currency_code'] = commerce_default_currency();
- $product->field_pages['und'][0]['value'] = $q->pages;
- $product->field_descpages['und'][0]['value'] = $q->descpages;
- $product->field_size['und'][0]['value'] = $q->size;
- $product->field_descsize['und'][0]['value'] = $q->descsize;
- commerce_product_save($product);
- $nid = db_select('field_data_field_product', 'f')
- ->fields('f', array('entity_id'))
- ->condition('field_product_product_id', $product->product_id, '=')
- ->condition('bundle', 'book_display', '=')
- ->execute()->fetchField();
- if ($nid) {
- $node = node_load($nid);
- } else {
- $node = new stdClass();
- $node->type = 'book_display';
- $node->body = array(LANGUAGE_NONE => array(0 => array('value' => $q->stitle)));
- $node->uid = 1;
- $node->field_product['und'][0]['product_id'] = $product->product_id;
- }
- $node->title = word_substr($title,255);
- if(!empty($q->author)) {
- //$vid_obj = taxonomy_vocabulary_machine_name_load('authors');
- $exists = db_select('taxonomy_term_data', 't')->fields('t', array('tid'))
- ->condition('name', $q->author, '=')
- ->condition('vid', '2', '=')
- ->execute()->fetchField();
- if(!$exists) {
- $term = new stdClass();
- $term->name = $q->author;
- $term->vid = '2';
- $term->path['pathauto'] = FALSE;
- $term->path = array('alias' => 'author/show/id/' . $q->aid, 'pathauto' => 0);
- taxonomy_term_save($term);
- /*$np = array('source' => 'taxonomy/term/' . $term->tid, 'alias' => 'author/show/id/' . $q->aid);
- path_save($np);
- */
- $exists = $term->tid;
- }
- $node->field_author['und'][0]['tid'] = $exists;
- }
- if(!empty($q->cat)) {
- //$vid_obj = taxonomy_vocabulary_machine_name_load('tags');
- $cat_arr = explode('--', $q->cat);
- $exist_tids = array();
- foreach($cat_arr as $name) {
- $name = multibyte_trim($name);
- $exists = db_select('taxonomy_term_data', 't')->fields('t', array('tid'))
- ->condition('name', drupal_substr($name,0,254), '=')
- ->condition('vid', '1', '=')
- ->execute()->fetchField();
- if(!$exists) {
- $term = new stdClass();
- $term->name = drupal_substr($name,0,254);
- $term->vid = '1';
- $term->path['pathauto'] = TRUE;
- taxonomy_term_save($term);
- $exists = $term->tid;
- }
- $exist_tids[] = $exists;
- }
- foreach($exist_tids as $num_tid => $tid) {
- $node->field_keywords['und'][$num_tid]['tid'] = $tid;
- }
- }
- if(count($q->rubrics)) {
- //$vid_obj = taxonomy_vocabulary_machine_name_load('categories');
- $exist_tids = array();
- foreach($q->rubrics as $r) {
- $exists = db_select('taxonomy_term_data', 't')->fields('t', array('tid'))
- ->condition('name', $r->title, '=')
- ->condition('vid', '3', '=')
- ->execute()->fetchField();
- if(!$exists) {
- $term = new stdClass();
- $term->name = $r->title;
- $term->vid = '3';
- $term->path = array('alias' => 'book_category/list/category_id/' . $r->cid, 'pathauto' => 0);
- taxonomy_term_save($term);
- $exists = $term->tid;
- /*
- $np = array('source' => 'taxonomy/term/' . $term->tid, 'alias' => 'book_category/list/category_id/' . $r->cid);
- path_save($np);
- */
- }
- $exist_tids[] = $exists;
- }
- foreach($exist_tids as $num => $tid) {
- $node->field_category['und'][$num_tid]['tid'] = $tid;
- }
- }
- $node->field_publisher['und'][0]['value'] = $q->publisher;
- $node->field_language['und'][0]['value'] = $q->language;
- $node->path = array('alias' => 'book/show/id/' . $q->id, 'pathauto' => 0);
- node_save($node);
- /*
- $np = array('source' => 'node/' . $node->nid, 'alias' => 'book/show/id/' . $q->id);
- path_save($np);
- */
- print 'done: '.$q->id.PHP_EOL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement