<?php
/**
 * Filename: _widgets.php
 * Created : EzLinkTools.Com <marcus@blogprofitnetwork.com> (https://EzLinkTools.Com)
 * Copyright 2020 - EzLinkTools.Com <marcus@blogprofitnetwork.com> (https://EzLinkTools.Com)
 */
defined('_IN_MAIN_MODULE') or die ('ACCESS DENIED');

class buttonVoodooWidgets extends WP_Widget {
	function init() {
		register_widget('buttonVoodooWidgets');
	}

	function __construct() {
		$options 	= array(
			'classname'		=> 'buttonVoodooWidgets',
			'description'	=> BUTTONVOODOO_NO .' is an awesome widget'
		);

		$controls	= array(
			'width'		=> 350,
			'height'	=> 350,
			'id_base'	=> BUTTONVOODOO_CODE .'-widgets'
		);

		parent::__construct(BUTTONVOODOO_CODE .'-widgets', BUTTONVOODOO_NAME, $options, $controls);
	}

	function update($new, $old) {
		$instance = array();
		return $instance;
	}

	function form($instance) {
		echo '<p><a href="'. admin_url('admin.php?page='. BUTTONVOODOO_CODE) .'">Click me</a> and browse the groups to configure your widget.</p>';
	}

	function widget($args, $instance) {
		global 	$buttonVoodooDatabase,
				$post;

		$id = $post->ID;

		$groups = $buttonVoodooDatabase->getEnabledGroups();
		if (0 != count($groups)) {
			echo $before_widget;
				echo $before_title;
				echo $after_title;


				foreach ($groups as $group) {
					$groupid		= $group['groupid'];
					$groupOptions	= get_option($buttonVoodooDatabase->tableGroupOptions .'_'. $groupid);

					$homepage		= $group['homepage'];
					$pages			= $group['pages'];
					$pagesid		= $group['pagesid'];
					$posts			= $group['posts'];
					$postsid		= $group['postsid'];

					//* homepage
					if (is_home() || is_front_page() || is_sticky()) {
						if ($homepage == BUTTONVOODOO_ENABLED) {
							$this->contents($groupid);
						}
					}


					//* pages
					if (is_page()) {
						if ($pages == BUTTONVOODOO_ENABLED) {
							if (0 != strlen($pagesid)) {
								$explodePagesId = explode(',', trim(str_replace(', ', ',', $pagesid)));
								if (in_array($id, $explodePagesId)) {
									$this->contents($groupid);
								}
							}
							else {
								$this->contents($groupid);
							}
						}
					}


					//* posts
					if (is_single()) {
						if ($posts == BUTTONVOODOO_ENABLED) {
							if (0 != strlen($postsid)) {
								$explodePostId = explode(',', trim(str_replace(', ', ',', $postsid)));
								if (in_array($id, $explodePostId)) {
									$this->contents($groupid);
								}
							}
							else {
								$this->contents($groupid);
							}
						}
					}

				}

			echo $after_widget;

		}
	}

	function contents($groupid) {
		global $buttonVoodooDatabase;

		$sizes 			= get_option($buttonVoodooDatabase->tableSizeOptions);

		$group			= $buttonVoodooDatabase->getEnabledGroupById($groupid);
		$groupOptions	= get_option($buttonVoodooDatabase->tableGroupOptions .'_'. $groupid);
		if (0 != count($group)) {
			$buttonVoodooDatabase->addGroupImpression($groupid);

			$buttons = $buttonVoodooDatabase->getEnabledButtonsByGroupId($groupid);
			if (0 != count($buttons)) {
				$sizeExplode 	= explode(',', trim(str_replace(' ','', $groupOptions['button']['size'])));
				$sizeWidth		= $sizeExplode[0];
				$sizeHeight		= $sizeExplode[1];

				$textDecoration	= ($groupOptions['font']['underline'] == BUTTONVOODOO_YES) ? 'text-decoration:underline!important;' : '';
				$textBold		= ($groupOptions['font']['bold'] == BUTTONVOODOO_YES) ? 'font-weight:bold!important;' : 'font-weight:normal!important;';
				$textColor		= 'color:'. $groupOptions['button']['color'] .'!important;';
				?>

				<style type="text/css">
					.holder-widget-button-voodoo-v2 {
						width: 100% !important;
						font-family: <?php echo $groupOptions['font']['family']; ?> !important;
						font-size: <?php echo $groupOptions['font']['size']; ?>px !important;
					}

					.holder-widget-button-voodoo-v2 a {
					<?php echo $textBold . $textDecoration . $textColor; ?>
					}

					.holder-widget-button-voodoo-v2 a .button-table {
						width: 100% !important;
						display: table !important;

						background: <?php echo $groupOptions['button']['bg']; ?> !important;

					<?php
					if ($groupOptions['button']['type'] == 'round') {
						echo 'border-radius:35px!important; -moz-border-radius:35px!important; -webkit-border-radius:35px!important;';
					}
					else {
						echo 'border-radius:0px!important; -moz-border-radius:0px!important; -webkit-border-radius:0px!important;';
					}
					?>
					}

					.holder-widget-button-voodoo-v2 a .button-table-row {
						display: table-row !important;
					}

					.holder-widget-button-voodoo-v2 a .button-table-heading {
						display: table-header-group !important;
					}

					.holder-widget-button-voodoo-v2 a .button-table-head,
					.holder-widget-button-voodoo-v2 a .button-table-cell {
						display: table-cell !important;
						vertical-align: middle !important;
					}

					.holder-widget-button-voodoo-v2 a .button-table-body {
						display: table-row-group !important;
					}

					.holder-widget-button-voodoo-v2 a .button-table-foot {
						display: table-footer-group !important;
					}

					.holder-widget-button-voodoo-v2 img {
						vertical-align: middle !important;
						text-decoration: none !important;

						width: <?php echo $sizeHeight - 20; ?>px !important;
						height: <?php echo $sizeHeight - 20; ?>px !important;
					}
				</style>

				<?php
				foreach ($buttons as $button) {
					$buttonid		= $button['buttonid'];
					$buttonOptions	= get_option($buttonVoodooDatabase->tableButtonOptions .'_'. $buttonid);

					if ($buttonOptions['icon']['type'] == 'round') {
						$iconType = 'border-radius:35px!important; -moz-border-radius:35px!important; -webkit-border-radius:35px!important;';
					}
					else {
						$iconType = 'border-radius:0px!important; -moz-border-radius:0px!important; -webkit-border-radius:0px!important;';
					}


					$overrideColor 	= ($buttonOptions['button']['color']['override'] == BUTTONVOODOO_YES) ? 'color:'. $buttonOptions['button']['color']['color'] .'!important;' : '';
					$overrideBg		= ($buttonOptions['button']['bg']['override'] == BUTTONVOODOO_YES) ? 'background:'. $buttonOptions['button']['bg']['color'] .'!important;' : '';
					?>


					<div class="holder-widget-button-voodoo-v2">
						<a href="<?php echo get_bloginfo('home') .'/?'. BUTTONVOODOO_CODE .'='. $buttonid; ?>" target="_blank" style="<?php echo $overrideColor; ?>">
							<div class="button-table" style="<?php echo $overrideBg; ?>">
								<div class="button-table-body">
									<div class="button-table-row">

										<?php
										if ($buttonOptions['icon']['enable'] == BUTTONVOODOO_YES) {
											if ($buttonOptions['icon']['location'] == 'left') {
												?>

												<div class="button-table-cell" style="width:<?php echo $sizeHeight -20; ?>px!important; ?> text-align:left!important;">
													<?php if (0 != strlen($button['icon'])) {
														echo '<img src="'. $button['icon'] .'" alt="" style="'. $iconType .'" />';
													} ?>
												</div>
												<div class="button-table-cell" style="padding-right:20px!important; text-align:left!important;"><?php echo $button['title']; ?></div>

												<?php
											}
											else {
												?>

												<div class="button-table-cell" style="padding-left:20px!important; text-align:left!important;"><?php echo $button['title']; ?></div>
												<div class="button-table-cell" style="width:<?php echo $sizeHeight -20; ?>px!important; ?> text-align:left!important;">
													<?php if (0 != strlen($button['icon'])) {
														echo '<img src="'. $button['icon'] .'" alt="" style="'. $iconType .'" />';
													} ?>
												</div>

												<?php
											}
										}
										else {
											?>

											<div class="button-table-cell" style="padding-left:20px!important; padding-right:20px!important; text-align:left!important;"><?php echo $button['title']; ?></div>

											<?php
										}
										?>

									</div>
								</div>
							</div>
						</a>
						<div style="height:10px!important;"></div>
					</div>


					<?php
				}
			}
			
		}
	}
}