<?php
$_temp['Start SIDE Articles'] = microtime(true);
if(!function_exists("t")) {
	function t($val) {
		return $val;
	}
}

if(!isset($lang2iso) || $lang2iso == "") {
	$lang2iso = "en";
}

$show_admin = false;
if(isset($_SESSION['user_id']) && intval($_SESSION['user_id']) > 0) {
	$show_admin = true;
}
//echo '<pre>$module_params = ', var_export($module_params, true), '</pre>';

ob_start();

$is_sales_view = false;
if(isset($_SESSION['user_id']) && intval($_SESSION['user_id']) > 0) {
	$is_sales_view = true;
	//$admin_category_link = '<a class="user-action-inline" href="/admin/index.php?plugin=contents&amp;action=article_categ&amp;cat_id='.$middle_parent_id.'" target="_blank">e</a>';
}



$sides_article_list = "module";
if(isset($category_side_parent_id) && is_array($category_side_parent_id)) {
    $side_parent_id = $category_side_parent_id;
    $sides_article_list = "category";
    unset($category_side_parent_id);
}


if(isset($side_parent_id) && is_array($side_parent_id) && count($side_parent_id) >0) {
	
	if(isset($_REQUEST['debug_sd'])) {
		echo '<pre>$side_parent_id = ', var_export($side_parent_id, true), '</pre>';
		echo '<pre>$module_params = ', var_export($module_params, true), '</pre>';
	}
	
	include_once("$general_path" . "functions.php");
    $sc_title = '';
	if(!isset($module_params['sides_article_list_title']) || trim($module_params['sides_article_list_title']) == "") {
		$sides_article_list_title = "";
	}
	else {
		$sc_title = stripslashes($module_params['sides_article_list_title']);
        unset($sides_article_list_title);
	}
	
	$start_url = $_SERVER['HTTP_HOST'];
	if(str_ireplace("www.", "", $start_url) != str_ireplace("www.", "", $ultra_domainname)) {
		$start_url =  $_SERVER['HTTP_HOST'] . "/" . $ultra_addonname;
	}
	$start_url = $protocol . $start_url . "/";

	$sc_links = array();
	
	if(count($side_parent_id) == 1) {
		$cart_id = $side_parent_id[0];
		
		$q = "SELECT * FROM main_article_categ WHERE cat_id='$cart_id' and catdom_id='$domain_id' and active='yes';";
		//echo "<br>$q";
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) == 1) {
			$row = mysql_fetch_assoc($r);
			$sc_title = ($sc_title == "") ? stripslashes($row["title_$lang2iso"]) : $sc_title;
			$sc_link_title = stripslashes($row["link_title_$lang2iso"]);
			$sc_href_struct = return_friendly_link("main_article_categ", $cart_id, "_self", "");
			$sc_href = $start_url . 'articles/' . $sc_href_struct['link'];
			$sc_content_type = (trim($row['content_type'] != "") && $row['content_type'] == "news" ) ? "news" : "article";
			if(trim($row['url']) != "") {
				$sc_href = trim($row['url']);
				
				if(strpos('http', $sc_href) !== false) {
					
				}
				else {
					$sc_href = $start_url . $sc_href;
				}
				
			}
			
			$admin_category_link = '<a class="user-action-inline" href="/admin/index.php?plugin=contents&amp;action=article_categ&amp;cat_id='.$cart_id.'" target="_blank">e</a>';
			
			// Category Links
			$qc = "SELECT * FROM main_article_categ WHERE parent_id='$cart_id' and catdom_id='$domain_id' and active='yes' order by order_seq ASC;";
            //echo "<br>$qc";
			$rc = @mysql_query($qc);
			if($rc && mysql_num_rows($rc) > 0) {
				while ($rowc = mysql_fetch_assoc($rc)) {
					$side_cat_id = $rowc['cat_id'];
					$side_title = stripslashes($rowc["title_$lang2iso"]);
					$side_link_title = stripslashes($rowc["link_title_$lang2iso"]);
					$_blank = $rowc['_blank'] == "yes" ? "_blank" : "_self";
					$side_href = return_friendly_link("main_article_categ", $side_cat_id, "$_blank", "");
					$side_url = fix_url(trim($rowc['url']));
					if($side_url != "") {
						$sc_links [] = array(
							"title"	=> $side_link_title,
							"link"	=> array(
								"link"	=> $protocol . $side_url,
								"target"	=> $_blank
							),
							"cat_id"	=> $side_cat_id
						);
					}
					else {
						if($side_href['link'] != "") {
							$side_href['link'] = $start_url . 'articles/' . $side_href['link'];
						}
						else {
							$side_href['link'] = "#";
						}
						
						$sc_links [] = array(
							"title"	=> $side_link_title,
							"link"	=> $side_href,
							"cat_id"	=> $side_cat_id
						);
					}
				}
			}
			
			// Article Links
			if($sc_content_type == "article") {
				$qc = "SELECT * FROM main_article WHERE parent_id='$cart_id' and catdom_id='$domain_id' and active='yes' order by order_seq ASC;";
			}
			elseif ($sc_content_type == "news") {
				$qc = "SELECT * FROM main_article WHERE parent_id='$cart_id' and catdom_id='$domain_id' and active='yes' order by created DESC;";
			}

            //echo "<br>$qc";
			$rc = @mysql_query($qc);
			if($rc && mysql_num_rows($rc) > 0) {
				while ($rowc = mysql_fetch_assoc($rc)) {
					$side_article_id = $rowc['article_id'];
					$nofollow = intval($rowc['nofollow']) == 1 ? 'rel="nofollow"' : '';
					$side_title = stripslashes($rowc["title_$lang2iso"]);
					$side_link_title = stripslashes($rowc["link_title_$lang2iso"]);
					$_blank = $rowc['_blank'] == "yes" ? "_blank" : "_self";
					$side_href = return_friendly_link("main_article", $side_article_id, "$_blank", "");
					//echo '<pre>', var_export($side_href, true), '</pre>';
					$side_url = trim($rowc['url']) != "" ? trim($rowc['url']) : '';
					if($side_url != "") {
						$sc_links [] = array(
							"title"	=> $side_link_title,
							"link"	=> array(
								"link"	=> (substr($side_url, 0, 4) == 'http') ? $side_url : $start_url. $side_url,
								"target"	=> $_blank
							),
							"article_id" => $side_article_id,
							"nofollow"	=> $nofollow
						);
					}
					else {
						if($side_href['link'] != "") {
							$side_href['link'] = $start_url . 'article/' . $side_href['link'];
						}
						else {
							$side_href['link'] = "#";
						}
						
						$sc_links [] = array(
							"title"	=> $side_link_title,
							"link"	=> $side_href,
							"article_id" => $side_article_id,
							"nofollow"	=> $nofollow
						);
					}
				}
			}
		}
		
	}
	else {
		$sc_href = '';
		foreach ($side_parent_id as $cart_id) {
			$q = "SELECT * FROM main_article_categ WHERE cat_id='$cart_id' and catdom_id='$domain_id' and active='yes';";
            //echo "<br>$q";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) == 1) {
				$row = mysql_fetch_assoc($r);
				$side_title = stripslashes($row["title_$lang2iso"]);
				$side_link_title = stripslashes($row["link_title_$lang2iso"]);
				$_blank = $row['_blank'] == "yes" ? "_blank" : "_self";
				$side_href = return_friendly_link("main_article_categ", $cart_id, $_blank, "");
				$side_url = trim($row['url']);
				if($side_url != "") {
					$sc_links [] = array(
						"title"	=> $side_link_title,
						"link"	=> array(
								"link"	=> (substr($side_url, 0, 4) == 'http') ? $side_url : $start_url. $side_url,
								"target"	=> $_blank
							),
						"cat_id"	=> $cart_id
					);
				}
				else {
					if($side_href['link'] != "") {
						$side_href['link'] = $start_url . 'articles/' . $side_href['link'];
					}
					else {
						$side_href['link'] = "#";
					}
					
					$sc_links [] = array(
						"title"	=> $side_link_title,
						"link"	=> $side_href,
						"cat_id"	=> $cart_id
					);
				}
			}
		}
	}
	
	?>
    <section class="article-category">
        <div class="box box-danger">
            <?php
            if($sc_title == "" || (isset($module_params['sides_article_list_title_none']) && intval($module_params['sides_article_list_title_none']) == 1) ) {
                // Show nothing
            }
            else {
                ?>
                <div class="box-header with-border text-left">
                    <i class="fa fa-bookmark fa-fw text-danger pull-left"></i>
                    <?php
                    if(isset($sc_href) && trim($sc_href) != "" && isset($sides_article_list_title) && $sides_article_list_title == "") {
                        echo '<a target="_self" href="'.$sc_href.'" class="artcateg_title_link">' . t($sc_title) . '</a>';
                    }
                    else {
                        echo '<b>'.t($sc_title).'</b>';
                    }
                    ?>

                    <?php
                    if($is_sales_view && isset($admin_category_link)) {
                        echo $admin_category_link;
                    }
                    ?>

                </div>
                <?php
            }
            ?>
            <div class="box-body no-padding text-left">
                <?php
                if(isset($_REQUEST['debug'])) {
                    echo '<pre>', print_r($sc_links), '</pre>';
                }
                if(isset($sc_links) && count($sc_links) > 0) {
                    if(count($side_parent_id) == 1) {
                        if(intval($sides_article_list_max) > 0) {
                            $max_links = count($sc_links);
                            if($max_links > $sides_article_list_max) $max_links = $sides_article_list_max;

                            //echo '<pre>', var_export($sc_links, true), '</pre>';

                            echo '<ul class="list-unstyled list-links">';

                            $link_cnt = 1;
                            foreach($sc_links as $sc_index => $sc_data) {
                                if($link_cnt <= $max_links) {
                                    $sc_nofollow = '';
                                    if (isset($sc_data['nofollow'])) {
                                        $sc_nofollow = $sc_data['nofollow'];
                                    }
                                    ?>
                                    <li>
                                        <a class="list-links-item"
                                           target="<?php echo $sc_data['link']['target']; ?>" <?php echo $sc_nofollow; ?>
                                           href="<?php echo $sc_data['link']['link']; ?>"><i
                                                class="fa fa-chevron-right text-info"></i> <?php echo $sc_data['title']; ?>
                                        </a>
                                        <?php
                                        if ($is_sales_view) {
                                            if (isset($sc_data['article_id'])) {
                                                $admin_article_link = '<a class="user-action-inline" href="/admin/index.php?plugin=contents&amp;action=article&amp;article_id=' . $sc_data['article_id'] . '" target="_blank">e</a>';
                                                echo $admin_article_link;
                                            } elseif (isset($sc_data['cat_id'])) {
                                                $admin_subcategory_link = '<a class="user-action-inline" href="/admin/index.php?plugin=contents&amp;action=article_categ&amp;cat_id=' . $sc_data['cat_id'] . '" target="_blank">e</a>';
                                                echo $admin_subcategory_link;
                                            }
                                        }
                                        ?>
                                    </li>
                                    <?php
                                }
                                $link_cnt ++;
                            }

                            echo '</ul>';

                        }
                        else {
                            echo '<ul class="list-unstyled list-links">';
                            foreach ($sc_links as $i=>$sc_data) {
                                ?>
                                <li>
                                    <a class="list-links-item" target="<?php echo $sc_data['link']['target'];?>" href="<?php echo $sc_data['link']['link'];?>"><i class="fa fa-angle-double-right"></i> <?php echo $sc_data['title'];?></a>
                                    <?php
                                    if($is_sales_view) {
                                        if(isset($sc_data['article_id'])) {
                                            $admin_article_link = '<a class="user-action-inline" href="/admin/index.php?plugin=contents&amp;action=article&amp;article_id='.$sc_data['article_id'].'" target="_blank">e</a>';
                                            echo $admin_article_link;
                                        }
                                        elseif(isset($sc_data['cat_id'])) {
                                            $admin_subcategory_link = '<a class="user-action-inline" href="/admin/index.php?plugin=contents&amp;action=article_categ&amp;cat_id='.$sc_data['cat_id'].'" target="_blank">e</a>';
                                            echo $admin_subcategory_link;
                                        }
                                    }
                                    ?>
                                </li>
                                <?php
                            }

                            echo '</ul>';
                        }
                    }
                    else {

                        echo '<ul class="list-unstyled list-links">';
                        foreach ($sc_links as $i=>$sc_data) {
                            ?>
                            <li>
                                <a class="list-links-item" target="<?php echo $sc_data['link']['target'];?>" href="<?php echo $sc_data['link']['link'];?>"><i class="fa fa-angle-double-right"></i> <?php echo $sc_data['title'];?></a>
                                <?php
                                if($is_sales_view) {
                                    if(isset($sc_data['article_id'])) {
                                        $admin_article_link = '<a class="user-action-inline" href="/admin/index.php?plugin=contents&amp;action=article&amp;article_id='.$sc_data['article_id'].'" target="_blank">e</a>';
                                        echo $admin_article_link;
                                    }
                                    elseif(isset($sc_data['cat_id'])) {
                                        $admin_subcategory_link = '<a class="user-action-inline" href="/admin/index.php?plugin=contents&amp;action=article_categ&amp;cat_id='.$sc_data['cat_id'].'" target="_blank">e</a>';
                                        echo $admin_subcategory_link;
                                    }
                                }
                                ?>
                            </li>
                            <?php
                        }
                        echo '</ul>';
                    }
                }
                ?>
            </div>
        </div>
	</section>
	<?php
	unset($side_parent_id);
    if(isset($module_params)) unset($module_params);

    unset($sides_article_list_title);
    unset($sides_article_list_title_none);
    unset($sides_article_list_max);

}

$categs_list = ob_get_contents();
ob_end_clean();

echo $categs_list;
$_temp['End SIDE Articles'] = microtime(true);
?>