<?php require_once 'map.php';
if (! defined('ABSPATH') ) { exit;
}
global $wpdb;
require_once 'legalPages.php';
$lpObj = new legalPages();
$lpObj->lp_enqueue_editor();
$baseurl=esc_url(get_bloginfo('url'));
//$baseurl = $_SERVER['PHP_SELF'];
$page = isset($_REQUEST['page'])?sanitize_text_field($_REQUEST['page']):'';
$template = isset($_REQUEST['template'])?sanitize_text_field($_REQUEST['template']):'';
$lptype = isset($_REQUEST['lp-type'])?sanitize_text_field($_REQUEST['lp-type']):'';
$terms = file_get_contents(dirname(__FILE__) . '/templates/Terms.html');
$privacy = file_get_contents(dirname(__FILE__) . '/templates/privacy.html');
$privacyCalifornia = file_get_contents(dirname(__FILE__) . '/templates/privacyCalifornia.html');
$earnings = file_get_contents(dirname(__FILE__) . '/templates/earnings.html');
$disclaimer = file_get_contents(dirname(__FILE__) . '/templates/disclaimer.html');
?>
    <div style="width:1000px;float:left;">
        <h1 class="logo-hh"><img style="width: 142px;" src="<?php echo plugin_dir_url(__FILE__); ?>image/wpLegalEagle2.png"></h1> </div>
    <div style="clear:both;"></div>
    <div class="wrap">
        <?php



        if(!empty($_POST) && $_POST['lp-submit']=='Publish') :
                $title = $_POST['lp-title'];
                $content = $_POST['lp-content'];
                $post_args = array(
             'post_title' => apply_filters('the_title', $title),
             'post_content' => $content,
             'post_type' => 'page',
             'post_status' => 'publish',
             'post_author' => 1
                    );
                $pid = wp_insert_post($post_args);
                /* Adding custom field */
                update_post_meta($pid, 'is_legal', 'yes');

                $url = get_permalink($pid);
                    ?>
                    <div id="message">
                        <p><span class="label label-success myAlert"><?php _e('Page Successfully Created. You can view your page as a normal Page in Pages Menu.','wplegalpages');?> </span></p>
                        <p><a href="<?php echo get_admin_url(); ?>/post.php?post=<?php echo $pid;?>&action=edit"><?php _e('Edit','wplegalpages');?></a> | <a href="<?php echo esc_url($url); ?>"><?php _e('View','wplegalpages');?></a></p>
            </div>
            <?php
        endif;

        $general = get_option('wpgattack_general');
        $checked = 'checked="checked"'; $selected = 'selected="selected"';
?>
                <style type="text/css">
                    .clear {  clear: both;      }
                    #lp_generalid {padding: 5px 20px 20px 20px;  width: 800px; float: left;}
                    #lp_generalid_right {float: left;width: 280px;margin-left: 20px;margin-top: 20px; border-radius: 5px;  -moz-border-radius: 5px; -webkit-border-radius: 5px;padding-bottom: 10px; }
                    #lp_generalid p { font-size: 16px;  }
                    #lp_generalid input[type=text] {  width: 800px;  height: 30px;  color: #666;  font-size: 20px;  padding: 3px 4px; }
                    #lp_generalid input[type=submit] {width: 100px; height: 30px; color: #000; cursor: pointer; font-size: 20px; padding: 3px 4px;  }
                    #lp_generalid_right li {list-style: square; line-height: 20px; list-style-position: inside; padding-left: 10px;  }
                    #lp_generalid_right li span { font-size: 16px;   cursor: pointer; }
                    #lp_generalid p a { text-decoration: none; }
                    #content p { width: 800px; }
                    a {text-decoration: none; }
                </style>
                <?php
                $postTbl = $wpdb->prefix . "posts";
                $postmetaTbl = $wpdb->prefix . "postmeta";
                $countofPages = $wpdb->get_results(
                    "SELECT count(meta_id) as cntPages FROM $postTbl, $postmetaTbl
			WHERE $postTbl.ID = $postmetaTbl.post_id and $postTbl.post_status='publish'
			AND $postmetaTbl.meta_key =  'is_legal'"
                );
                if($countofPages[0]->cntPages<20) {
                                ?>
                                   <div class="postbox">
                                       <h3 class="hndle title-head" style="cursor:pointer; padding:7px 10px; font-size:20px;"> <?php _e('Create Page','wplegalpages');?> :</h3>
                                       <div class="row">
                                           <div class="col-md-9">
                                <div id="lp_generalid">
                                    <form>
                                        <p>
                                            <label class="field_title">Type of Site: </label>
                                            <select name="lp-type" class="form-control" onchange="window.location='<?php echo $_SERVER['PHP_SELF'];?>?page=<?php echo $_REQUEST['page'];?>&lp-type='+this.value">
                                                <option value="">All</option>
                                                <option value="1" <?php if($lptype==1) { echo $selected;
                                               }?>>Business Website</option>
                                                <option value="2" <?php if($lptype==2) { echo $selected;
                                               }?>>Business Website With Products</option>
                                                <option value="3" <?php if($lptype==3) { echo $selected;
                                               }?>>'Business Website With Products & Affiliate Program</option>
                                                <option value="4" <?php if($lptype==4) { echo $selected;
                                               }?>>Affiliate/Review Site</option>
                                                <option value="5" <?php if($lptype==5) { echo $selected;
                                               }?>>Niche Sites</option>
                                                <option value="6" <?php if($lptype==6) { echo $selected;
                                               }?>>Medical Niche Site</option>
                                                <option value="7" <?php if($lptype==7) { echo $selected;
                                               }?>>Amazon Site</option>
                                                <option value="8" <?php if($lptype==8) { echo $selected;
                                               }?>>Adsense Site</option>
                                                <option value="9" <?php if($lptype==9) { echo $selected;
                                               }?>>Ecommerce Site</option>
                                                <option value="10" <?php if($lptype==10) { echo $selected;
                                               }?>>ISP - Hosting Provider Site</option>
                                            </select>
                                        </p>
                                    </form>
                                    <p>&nbsp;&nbsp;</p>
                                    <form name="terms" method="post" enctype="multipart/form-data">
                                        <?php if(!empty($template)) {
                                            $row = $wpdb->get_row($wpdb->prepare('SELECT * from '.$lpObj->tablename.' where id=%d', $template));
                                        }?>
                                            <p>
                                                <input type="text" class="form-control myText" name="lp-title" id="lp-title" value="<?php echo isset($row->title)?$row->title:'Terms of Use';?>" /> </p>
                                            <p>
                                                <div id="poststuff">
                                                    <div id="<?php echo user_can_richedit() ? 'postdivrich' : 'postdiv'; ?>">
                                                        <?php
                                                        $output=add_map();
                                                        $latitude = '';
                                                        $longitude = '';
                                                        $facebook = '';
                                                        $google = '';
                                                        $twitter = '';
                                                        $linkedin = '';

                                                        if(!empty($output->results)) {
                                                                        $latitude = $output->results[0]->geometry->location->lat;
                                                               $longitude = $output->results[0]->geometry->location->lng;
                                                        }

                                                        $content = isset($row->content)?$row->content:$terms;
                                                        $lp_find = array("[Domain]","[Business Name]","[Phone]","[Street]","[City, State, Zip code]","[Country]","[Email]","[Address]","[Niche]");
                                                        $lp_general = get_option('lp_general');
                                                        $lp_map_find=array("[latitude]","[longitude]");            //Added by Rajashri L
                                                        $url=(plugins_url()."/wplegalpages/image");
                                                        $map_diamention=array("latitude"=>$latitude,"longitude"=>$longitude);
                                                        $content = str_replace($lp_find, $lp_general, stripslashes($content));
                                                        $content = str_replace($lp_map_find, $map_diamention, stripslashes($content));

                                                        $facebook_url = isset($lp_general['facebook-url']) ? $lp_general['facebook-url'] : '';

                                                        if($facebook_url) {

                                                                                $facebook=array("facebook"=>'<a style="width: 30px; height: 30px; display:inline-block" href="'. $facebook_url.'"><img src="'.$url.'/facebook.png"/></a>');
                                                        }
                                                        $lp_find_facebook=array("[facebook]");
                                                        $content = str_replace($lp_find_facebook, $facebook, stripslashes($content));

                                                        $google_url = isset($lp_general['google-url']) ? $lp_general['google-url'] : '';
                                                        if($google_url) {
                                                                               $google=array("google"=> '<a style="width: 30px; height: 30px; display:inline-block" href="'. $google_url.'"><img src="'.$url.'/google.png"/></a>');
                                                        }
                                                        $lp_find_google=array("[google]");
                                                        $content = str_replace($lp_find_google, $google, stripslashes($content));

                                                        $twitter_url = isset($lp_general['twitter-url']) ? $lp_general['twitter-url'] : '';
                                                        if($twitter_url) {
                                                                               $twitter=array("twitter"=>'<a style="width: 30px; height: 30px; display:inline-block" href="'.$twitter_url.'"><img src="'.$url.'/twitter.png"/></a>');
                                                        }
                                                        $lp_find_twitter=array("[twitter]");
                                                        $content = str_replace($lp_find_twitter, $twitter, stripslashes($content));

                                                        $linkedin_url = isset($lp_general['linkedin-url']) ? $lp_general['linkedin-url'] : '';
                                                        if($linkedin_url) {
                                                                               $linkedin=array("linkedin"=>'<a style="width: 30px; height: 30px; display:inline-block" href="'. $linkedin_url.'"><img src="'.$url.'/linkedin.png"/></a>');
                                                        }
                                                        $lp_find_linkedin=array("[linkedin]");
                                                        $content = str_replace($lp_find_linkedin, $linkedin, stripslashes($content));

                                                        wp_editor(stripslashes(html_entity_decode($content)), 'content'); ?> </div>
                                                                                  <script type="text/javascript">
                                                                                      function sp_content_save() {
                                                                                          var obj = document.getElementById('lp-content');
                                                                                          var content = document.getElementById('content');
                                                                                          tinyMCE.triggerSave(0, 1);
                                                                                          obj.value = content.value;
                                                                                      }
                                                                                  </script>
                                                                                  <textarea id="lp-content" name="lp-content" value="5" style="display:none" rows="10"></textarea>
                                                                              </div>
                                                                          </p>
                                                                          <p>
                                                                              <input type="submit" class="btn btn-primary mybtn" onclick="sp_content_save();" name="lp-submit" value="<?php _e('Publish','wplegalpages');?>" /> </p>
                                                                  </form>
                                                              </div>
                                                          </div>
                                                          <div class="col-md-2" style="margin-left:-60px;">
                                                              <div id="lp_generalid_right" class="postbox ">
                                                                  <h3 class="hndle" style="cursor:pointer; padding:7px 10px; font-size:20px;"> <?php _e('Choose Template','wplegalpages');?> </h3>
                                                                  <br/>
                                                                  <ul>
                                                                        <?php
                                                                        if(isset($_REQUEST['lp-type'])) {
                                                                            $result = $wpdb->get_results("select * from $lpObj->tablename where contentfor like '%".$_REQUEST['lp-type']."%'");
                                                                        } else {
                                                                            $result = $wpdb->get_results("select * from $lpObj->tablename");
                                                                        }

                                                                        foreach($result as $ras){
                                                                                                                ?>
                                                                                                   <li><span id="legalpages<?php echo $ras->id;?>"><a class="myLink" href="<?php echo esc_url($baseurl);?>/wp-admin/admin.php?page=<?php echo $page;?>&lp-type=<?php echo $lptype;?>&template=<?php echo $ras->id;?>"><?php echo $ras->title;?> &raquo;</a></span></li>
                                                                        <?php }?>
                                                   </ul>
                                               </div>
                                           </div>
                                       </div>
                                       <div class="clear"></div>
                                   </div>
                <?php } else { ?>
                        <div id="message" class="updated">
                            <p><?php _e('You are exceeding the limit of creating 20 legal pages.','wplegalpages');?></p>
                        </div>
                <?php }?>
