<?php
function wp_pinterest_to_wp_create_table_all(){
	
	global $wpdb;

	if (function_exists('is_multisite') && is_multisite()) {
		// check if it is a network activation - if so, run the activation function for each blog id
		if (isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) {
			$old_blog = $wpdb->blogid;
			// Get all blog ids
			$blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
			foreach ($blogids as $blog_id) {
				switch_to_blog($blog_id);
				wp_pinterest_to_wp_create_table_alb();
			}
			switch_to_blog($old_blog);
			return;
		}
	}
	
	wp_pinterest_to_wp_create_table_alb();

}

function wp_pinterest_to_wp_create_table_alb()
{

	global $wpdb;
	$prefix=$wpdb->prefix ;

	//comments table
	if(!wp_pinterest_to_wp_exists_table("{$prefix}pinterest_to_wp_log")){
		
		 
		$querys="SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";

CREATE TABLE IF NOT EXISTS `wp_pinterest_to_wp_boards` (
  `board_gallery` text CHARACTER SET utf8 NOT NULL,
  `board_id` text CHARACTER SET utf8 NOT NULL,
  `board_link` text CHARACTER SET utf8 NOT NULL,
  `board_img` text CHARACTER SET utf8 NOT NULL,
  `board_name` text CHARACTER SET utf8 NOT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

CREATE TABLE IF NOT EXISTS `wp_pinterest_to_wp_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `action` varchar(50) NOT NULL,
  `data` text NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `camp` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=313 ;

CREATE TABLE IF NOT EXISTS `wp_pinterest_to_wp_pins` (
  `id` int(11) NOT NULL,
  `pin_id` text CHARACTER SET utf8 NOT NULL,
  `pin_board` text CHARACTER SET utf8 NOT NULL,
  `pin_gallery` text CHARACTER SET utf8 NOT NULL COMMENT 'pid of the custom post',
  `pin_img` text CHARACTER SET utf8 NOT NULL,
  `pin_text` text CHARACTER SET utf8 NOT NULL,
  `pin_img_big` text CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
		";
			 

		//executing quiries
		$que=explode(';',$querys);
			
		foreach($que  as $query){
			if(trim($query)!=''){
				$wpdb->query($query);
			}
		}
	}//if not exist table
	
	 
	
}

function wp_pinterest_to_wp_exists_table($table)

{
	global $wpdb;
	$rows = $wpdb->get_row('show tables like "'.$table.'"', ARRAY_N);

	return (count($rows)>0);
}
