Advertisement
kervi

Untitled

Nov 17th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.08 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', TRUE);
  4. ini_set('display_startup_errors', TRUE);
  5. /**
  6.  * @file
  7.  * The PHP page that serves all page requests on a Drupal installation.
  8.  *
  9.  * The routines here dispatch control to the appropriate handler, which then
  10.  * prints the appropriate page.
  11.  *
  12.  * All Drupal code is released under the GNU General Public License.
  13.  * See COPYRIGHT.txt and LICENSE.txt.
  14.  */
  15.  
  16. /**
  17.  * Root directory of Drupal installation.
  18.  */
  19. define('DRUPAL_ROOT', getcwd());
  20.  
  21. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  22. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  23.  
  24.  
  25.  
  26. $out = array();
  27. $res = Database::getConnection('default', 'parser')
  28.   ->query("select  au.title as author," .
  29.     "b.id as id," .
  30.     "au.id as aid," .
  31.     "CONCAT_WS('--', b.category_650_1,b.category_650_2,b.category_653_1,b.category_653_2) as cat," .
  32.     "b.note as stitle," .
  33.     "bt.title_245 as title," .
  34.     "b.desc_260 as publisher," .
  35.     "b.desc_41 as language," .
  36.     "b.pages as pages," .
  37.     "b.is_show as is_show," .
  38.     "b.size as size," .
  39.     "b.k_price as k_price," .
  40.     "b.price as price," .
  41.     "b.y30_j_1 as s1," .
  42.     "b.y30_j_2 as s2," .
  43.     "b.y30_j_3 as s3," .
  44.     "b.y30_j_4 as s4," .
  45.     "b.y30_j_5 as s5," .
  46.     "b.desc_300_c as descsize," .
  47.     "b.desc_300 as descpages" .
  48.   " from book b" .
  49.   " left outer join author au on au.id = b.author_id" .
  50.   " left outer join book_title bt on bt.book_id = b.id" .
  51.   " order by b.id");
  52. foreach($res as $k=>$entity) {
  53.   $res_cat = Database::getConnection('default', 'parser')
  54.     ->query("select  c.title as title, c.id as cid" .
  55.     " from category c inner join book_category bc on bc.category_id = c.id where bc.book_id = :bc",
  56.       array(':bc' => $entity->id));
  57.   foreach($res_cat as $cat_entity) {
  58.     $entity->rubrics[] = $cat_entity;
  59.   }
  60.  
  61.   import_data($entity);
  62. }
  63.  
  64. function import_data($q){
  65.     if(!empty($q->title)) {
  66.       $title = $q->title;
  67.     } else {
  68.       $pos = mb_strpos($q->stitle, '"');
  69.       $rpos = mb_strrpos($q->stitle, '"');
  70.       $title = mb_substr($q->stitle, $pos+1, $rpos - $pos - 1);
  71.     }
  72.     $sku = db_select('commerce_product', 'c')
  73.       ->fields('c', array('product_id'))
  74.       ->condition('sku', $q->id, '=')
  75.       ->execute()->fetchField();
  76.     if ($sku){
  77.       $product = commerce_product_load_by_sku($sku);
  78.     } else {
  79.       $product = commerce_product_new('product');
  80.       $product->status = $q->is_show;
  81.       $product->uid = '1';
  82.       $product->created = $product->changed = time();
  83.       $product->is_new = TRUE;
  84.     }
  85.     $product->title = word_substr($title,255);
  86.     $product->sku = $q->id;
  87.     $product->created = $product->changed = time();
  88.  
  89.  
  90.  
  91.     $total_amount = 100 * variable_get('default_price',1);
  92.     $product->field_page_price['und'][0]['amount'] = $total_amount;
  93.     $product->field_page_price['und'][0]['currency_code'] = commerce_default_currency();
  94.  
  95.     $product->commerce_price['und'][0]['amount'] = $q->price > 1 ? $q->price * 100 : 100;
  96.     $product->commerce_price['und'][0]['currency_code'] = commerce_default_currency();
  97.  
  98.  
  99.     $product->field_pages['und'][0]['value'] = $q->pages;
  100.     $product->field_descpages['und'][0]['value'] = $q->descpages;
  101.  
  102.  
  103.     $product->field_size['und'][0]['value'] = $q->size;
  104.  
  105.     $product->field_descsize['und'][0]['value'] = $q->descsize;
  106.  
  107.     commerce_product_save($product);
  108.  
  109.     $nid = db_select('field_data_field_product', 'f')
  110.     ->fields('f', array('entity_id'))
  111.     ->condition('field_product_product_id', $product->product_id, '=')
  112.     ->condition('bundle', 'book_display', '=')
  113.     ->execute()->fetchField();
  114.  
  115.     if ($nid) {
  116.       $node = node_load($nid);
  117.     } else {
  118.       $node = new stdClass();
  119.       $node->type = 'book_display';
  120.       $node->body = array(LANGUAGE_NONE => array(0 => array('value' => $q->stitle)));
  121.       $node->uid = 1;
  122.       $node->field_product['und'][0]['product_id'] = $product->product_id;
  123.     }
  124.  
  125.     $node->title = word_substr($title,255);
  126.  
  127.     if(!empty($q->author)) {
  128.       //$vid_obj = taxonomy_vocabulary_machine_name_load('authors');
  129.       $exists = db_select('taxonomy_term_data', 't')->fields('t', array('tid'))
  130.         ->condition('name', $q->author, '=')
  131.         ->condition('vid', '2', '=')
  132.         ->execute()->fetchField();
  133.       if(!$exists) {
  134.         $term = new stdClass();
  135.         $term->name = $q->author;
  136.         $term->vid = '2';
  137.         $term->path['pathauto'] = FALSE;
  138.         $term->path = array('alias' => 'author/show/id/' . $q->aid, 'pathauto' => 0);
  139.         taxonomy_term_save($term);
  140.         /*$np = array('source' => 'taxonomy/term/' . $term->tid, 'alias' => 'author/show/id/' . $q->aid);
  141.         path_save($np);
  142.         */
  143.         $exists = $term->tid;
  144.       }
  145.       $node->field_author['und'][0]['tid'] = $exists;
  146.     }
  147.  
  148.     if(!empty($q->cat)) {
  149.       //$vid_obj = taxonomy_vocabulary_machine_name_load('tags');
  150.       $cat_arr = explode('--', $q->cat);
  151.       $exist_tids = array();
  152.       foreach($cat_arr as $name) {
  153.         $name = multibyte_trim($name);
  154.         $exists = db_select('taxonomy_term_data', 't')->fields('t', array('tid'))
  155.           ->condition('name', drupal_substr($name,0,254), '=')
  156.           ->condition('vid', '1', '=')
  157.           ->execute()->fetchField();
  158.  
  159.         if(!$exists) {
  160.           $term = new stdClass();
  161.           $term->name = drupal_substr($name,0,254);
  162.           $term->vid = '1';
  163.           $term->path['pathauto'] = TRUE;
  164.           taxonomy_term_save($term);
  165.           $exists = $term->tid;
  166.         }
  167.         $exist_tids[] = $exists;
  168.       }
  169.       foreach($exist_tids as $num_tid => $tid) {
  170.         $node->field_keywords['und'][$num_tid]['tid'] = $tid;
  171.       }
  172.     }
  173.  
  174.  
  175.     if(count($q->rubrics)) {
  176.       //$vid_obj = taxonomy_vocabulary_machine_name_load('categories');
  177.       $exist_tids = array();
  178.       foreach($q->rubrics as $r) {
  179.         $exists = db_select('taxonomy_term_data', 't')->fields('t', array('tid'))
  180.           ->condition('name', $r->title, '=')
  181.           ->condition('vid', '3', '=')
  182.           ->execute()->fetchField();
  183.         if(!$exists) {
  184.           $term = new stdClass();
  185.           $term->name = $r->title;
  186.           $term->vid = '3';
  187.           $term->path = array('alias' => 'book_category/list/category_id/' . $r->cid, 'pathauto' => 0);
  188.           taxonomy_term_save($term);
  189.           $exists = $term->tid;
  190.           /*
  191.           $np = array('source' => 'taxonomy/term/' . $term->tid, 'alias' => 'book_category/list/category_id/' . $r->cid);
  192.           path_save($np);
  193.           */
  194.         }
  195.         $exist_tids[] = $exists;
  196.       }
  197.       foreach($exist_tids as $num => $tid) {
  198.         $node->field_category['und'][$num_tid]['tid'] = $tid;
  199.       }
  200.     }
  201.  
  202.     $node->field_publisher['und'][0]['value'] = $q->publisher;
  203.  
  204.     $node->field_language['und'][0]['value'] = $q->language;
  205.  
  206.     $node->path = array('alias' => 'book/show/id/' . $q->id, 'pathauto' => 0);
  207.     node_save($node);
  208.     /*
  209.     $np = array('source' => 'node/' . $node->nid, 'alias' => 'book/show/id/' . $q->id);
  210.     path_save($np);
  211.     */
  212.     print 'done: '.$q->id.PHP_EOL;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement