<?php
/*
Plugin Name: Irontec - Categorize
Plugin URI: http://www.irontec.com
Description: Categorize items
Version: 1.0
Author: Irontec
Author URI: http://www.irontec.com
*/

// Define plugin version and plugin url
define( 'IRON_CATEGORIZE_VERSION', '1.0' );
define( 'IRON_CATEGORIZE_URL', WP_PLUGIN_URL . '/irontec-categorize' );

// Add the plugin Administration Menu
add_action('admin_menu', 'iron_categorize_create_menu');

// Administration Menu
function iron_categorize_create_menu() {
	//create new top-level menu
	add_menu_page('Iron Categorize', 'Iron Categorize', 'manage_options', 'iron-categorize-handle', 'iron_categorize_front_page', plugins_url('/css/images/irontec.gif', __FILE__));
}

// Front Page
function iron_categorize_front_page () { ?>

	<?php
	// TABBER
	wp_deregister_script( 'tabber' );
	wp_register_script( 'tabber', get_stylesheet_directory_uri() .'/_inc/tabber/tabber.js' );
	wp_print_scripts( 'tabber' );
	wp_deregister_style('tabber-css');
	wp_enqueue_style('tabber-css', get_stylesheet_directory_uri() . '/_inc/tabber/tabber.css');
	wp_print_styles('tabber-css');

	// Some styling
	echo '<style>';
	echo
	'
	.status_color_fam { background: #fff3e4; }
	.status_color_ref { background: #eaedd8; }
	.status_color_sin { background: #e0f0f6; }
	.status_color_tra { background: #ede2e8; }
	';
	echo '</style>';
	?>

	<div class="ratingadmindiv wrap">
	<img style="float: left; margin: 13px;" src="<?php echo IRON_TUNNELS_URL; ?>/images/logo_irontec_2.0.png" />
	<h2>Categorize Front Page</h2>

	<?php
	$type = $_GET['type'];
	if (!$type) $type='familiares';
	?>

	<div class="tabber">

		<div class="tabbertab <?php if ($_GET['section']=='groups') echo 'tabbertabdefault'; ?>">
		<h2>Grupos</h2>

		<p>
		<?php if ($type == 'familiares') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=familiares&section=groups">familiares</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=familiares&section=groups">familiares</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'sintomaticos') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=sintomaticos&section=groups">sintomaticos</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=sintomaticos&section=groups">sintomaticos</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'reflexivos') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=reflexivos&section=groups">reflexivos</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=reflexivos&section=groups">reflexivos</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'transgresores') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=transgresores&section=groups">transgresores</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=transgresores&section=groups">transgresores</a>
		<?php endif; ?>
		</p>

		<?php
		global $wpdb;
		$table = $wpdb->prefix . 'bp_groups';
		$table2 = $wpdb->prefix . 'bp_groups_groupmeta';

		// Grupos
		$res_groups = $wpdb->get_results ('SELECT g.id, g.name, gm.meta_value FROM '. $table .' g
		INNER JOIN '.$table2.' gm ON g.id = gm.group_id AND gm.meta_key="ironestado" AND gm.meta_value="'. $type .'"
		ORDER BY g.name');

		$ngroups = count($res_groups);
		if ($ngroups>0) { ?>

			<p><b>NUM.: </b><?php echo $ngroups; ?></p>

			<table>
				<tr>
					<td align="left" style="width: 20px; padding: 4px; background: #d0d0d0;"><b>Id</b></td>
					<td align="left" style="width: 200px; padding: 4px; background: #d0d0d0;"><b>Name (+1p)</b></td>
					<td align="left" style="padding: 4px; background: #d0d0d0;"><b>Members (+2p)</b></td>
					<td align="left" style="padding: 4px; background: #d0d0d0;"><b>Contents (+4p)</b></td>
					<td align="left" style="padding: 4px; background: #d0d0d0;"><b>Calculated Status</b></td>
				</tr>
				<?php
				foreach ($res_groups as $group) {
					$fam=0; $ref=0; $sin=0; $tra=0;
					$puntosfam=0; $puntosref=0; $puntossin=0; $puntostra=0;
					?>
					<tr>
						<td align="left" style="vertical-align: top; padding: 4px; background: #f0f0f0;"><?php echo $group->id; ?></td>
						<td align="left" style="vertical-align: top; padding: 4px; background: #f0f0f0;">
						<?php
						echo $group->name;

						if ($type=='familiares') { $puntosfam = 1; echo '<br />(familiares)'; }
						elseif ($type=='reflexivos') { $puntosref = 1; echo '<br />(reflexivos)'; }
						elseif ($type=='sintomaticos') { $puntossin = 1; echo '<br />(sintomaticos)'; }
						elseif ($type=='transgresores') { $puntostra = 1; echo '<br />(transgresores)'; }
						?>
						</td>
						<td align="left" style="vertical-align: top; padding: 4px; background: #f0f0f0; font-size: 9px;">
						<?php
						// Contar el nº de miembros y el estado de animo de cada uno
						// +2 PUNTOS
						$table3 = $wpdb->prefix . 'bp_groups_members';
						$members = $wpdb->get_results ('SELECT user_id FROM '. $table3.' WHERE is_confirmed="1" AND group_id="'.$group->id.'"');
						$totalmem = count($members);

						if (count($members)>0) {
							foreach ($members as $member) {
								$user_mood = xprofile_get_field_data( 'EstadoAnimo', $member->user_id);
								if ($user_mood == 'familiares')
									$fam++;
								elseif ($user_mood == 'reflexivos')
									$ref++;
								elseif ($user_mood == 'sintomaticos')
									$sin++;
								elseif ($user_mood == 'transgresores')
									$tra++;
							}
						}

						$puntosfam += $fam*2;
						$puntosref += $ref*2;
						$puntossin += $sin*2;
						$puntostra += $tra*2;

						echo 'TOT: ' .$totalmem;
						echo '<br />fam: ' .$fam;
						echo '&nbsp;|&nbsp;ref: ' .$ref;
						echo '&nbsp;|&nbsp;sin: ' .$sin;
						echo '&nbsp;|&nbsp;tra: ' .$tra;

						echo '<br />fam: ' .$puntosfam;
						echo '&nbsp;|&nbsp;ref: ' .$puntosref;
						echo '&nbsp;|&nbsp;sin: ' .$puntossin;
						echo '&nbsp;|&nbsp;tra: ' .$puntostra;
						?>
						</td>
						<td align="left" style="vertical-align: top; padding: 4px; background: #f0f0f0; font-size: 9px;">
						<?php
						// Contar el nº de contenidos y el estado de animo de cada uno
						// +4 PUNTOS
						$table4 = $wpdb->prefix . 'bp_activity';
						$total_cont = 0;

						$res_cont = $wpdb->get_results ('SELECT id FROM '.$table4.' WHERE type = "activity_update" AND (component="activity" OR component="groups") AND item_id = "'. $group->id.'"');
						$total_cont = count($res_cont);
						$fam=0; $ref=0; $sin=0; $tra=0;

						if ($total_cont>0) {

							foreach ($res_cont as $cont) {
								$cont_mood = bp_activity_get_meta($cont->id, 'current_mood');
								if ($cont_mood == 'familiares')
									$fam++;
								elseif ($cont_mood == 'reflexivos')
									$ref++;
								elseif ($cont_mood == 'sintomaticos')
									$sin++;
								elseif ($cont_mood == 'transgresores')
									$tra++;
							}

							// Calculated Status Colors
							if ($calculated_status == 'familiares')
								$status_color_calc = 'status_color_fam';
							elseif ($calculated_status == 'reflexivos')
								$status_color_calc = 'status_color_ref';
							elseif ($calculated_status == 'sintomaticos')
								$status_color_calc = 'status_color_sin';
							elseif ($calculated_status == 'transgresores')
								$status_color_calc = 'status_color_tra';
						}

						$puntosfam += $fam*4;
						$puntosref += $ref*4;
						$puntossin += $sin*4;
						$puntostra += $tra*4;

						echo "TOT: ".$total_cont;
						echo '<br />fam: ' .$fam;
						echo '&nbsp;|&nbsp;ref: ' .$ref;
						echo '&nbsp;|&nbsp;sin: ' .$sin;
						echo '&nbsp;|&nbsp;tra: ' .$tra;

						echo '<br />fam: ' .$puntosfam;
						echo '&nbsp;|&nbsp;ref: ' .$puntosref;
						echo '&nbsp;|&nbsp;sin: ' .$puntossin;
						echo '&nbsp;|&nbsp;tra: ' .$puntostra;
						?>
						</td>
						<td class="<?php echo $status_color_calc; ?>" align="center" style="vertical-align: middle; padding: 4px; font-size: 9px; background: #f0f0f0;">
						<?php
						$estado = array('fam' => $puntosfam,
										'ref' => $puntosref,
										'sin' => $puntossin,
										'tra' => $puntostra);

						arsort($estado);
						echo key($estado);
						echo $estado[0];
						?>
						</td>
					</tr>
				<?php }
				?>
			</table>

		<?php }
		else { echo 'No se encontraron grupos'; }
		?>
		</div> <!-- .tabbertab -->

		<div class="tabbertab <?php if ($_GET['section']=='testimonios') echo 'tabbertabdefault'; ?>">
		<h2>Testimonios</h2>

		<p>
		<?php if ($type == 'familiares') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=familiares&section=testimonios">familiares</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=familiares&section=testimonios">familiares</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'sintomaticos') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=sintomaticos&section=testimonios">sintomaticos</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=sintomaticos&section=testimonios">sintomaticos</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'reflexivos') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=reflexivos&section=testimonios">reflexivos</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=reflexivos&section=testimonios">reflexivos</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'transgresores') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=transgresores&section=testimonios">transgresores</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=transgresores&section=testimonios">transgresores</a>
		<?php endif; ?>
		</p>

		<?php
		global $wpdb;
		$table = $wpdb->prefix . 'posts';
		$table2 = $wpdb->prefix . 'postmeta';

		// Testimonios
		$res_testimonios = $wpdb->get_results ('SELECT p.ID, p.post_title, pm.meta_value FROM '. $table .' p
		INNER JOIN '.$table2.' pm ON p.id = pm.post_id AND pm.meta_key="_wp_page_type" AND pm.meta_value="testimonio"
		INNER JOIN '.$table2.' pm2 ON p.id = pm2.post_id AND pm2.meta_key="iron-estado" AND pm2.meta_value="'. $type .'"
		ORDER BY p.post_title');

		$ntestimonios = count($res_testimonios);
		if ($ntestimonios>0) { ?>

			<p><b>NUM.: </b><?php echo $ntestimonios; ?></p>

			<table>
				<tr>
					<td align="left" style="width: 20px; padding: 4px; background: #d0d0d0;"><b>Id</b></td>
					<td align="left" style="width: 350px; padding: 4px; background: #d0d0d0;"><b>Name</b></td>
					<td align="left" style="padding: 4px; background: #d0d0d0;"><b>Status</b></td>
				</tr>
				<?php
				foreach ($res_testimonios as $testimonio) { ?>
					<tr>
						<td align="left" style="vertical-align: top; padding: 4px; background: #f0f0f0;"><?php echo $testimonio->ID; ?></td>
						<td align="left" style="padding: 4px; background: #f0f0f0;"><?php echo $testimonio->post_title; ?></td>
						<td align="left" style="padding: 4px; background: #f0f0f0;"><?php echo $type; ?></td>
					</tr>
				<?php }
				?>
			</table>

		<?php }
		else { echo 'No se encontraron testimonios'; }
		?>
		</div> <!-- .tabbertab -->

		<div class="tabbertab <?php if ($_GET['section']=='preguntas') echo 'tabbertabdefault'; ?>">
		<h2>Preguntas</h2>

		<p>
		<?php if ($type == 'familiares') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=familiares&section=preguntas">familiares</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=familiares&section=preguntas">familiares</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'sintomaticos') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=sintomaticos&section=preguntas">sintomaticos</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=sintomaticos&section=preguntas">sintomaticos</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'reflexivos') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=reflexivos&section=preguntas">reflexivos</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=reflexivos&section=preguntas">reflexivos</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'transgresores') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=transgresores&section=preguntas">transgresores</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=transgresores&section=preguntas">transgresores</a>
		<?php endif; ?>
		</p>

		<?php
		global $wpdb;
		$table = $wpdb->prefix . 'posts';
		$table2 = $wpdb->prefix . 'postmeta';

		// Preguntas
		$res_preguntas = $wpdb->get_results ('SELECT p.ID, p.post_title, pm.meta_value FROM '. $table .' p
		INNER JOIN '.$table2.' pm ON p.id = pm.post_id AND pm.meta_key="_wp_page_type" AND pm.meta_value="pregunta"
		INNER JOIN '.$table2.' pm2 ON p.id = pm2.post_id AND pm2.meta_key="iron-estado" AND pm2.meta_value="'. $type .'"
		ORDER BY p.post_title');

		$npreguntas = count($res_preguntas);
		if ($npreguntas>0) { ?>

			<p><b>NUM.: </b><?php echo $npreguntas; ?></p>

			<table>
				<tr>
					<td align="left" style="width: 20px; padding: 4px; background: #d0d0d0;"><b>Id</b></td>
					<td align="left" style="width: 350px; padding: 4px; background: #d0d0d0;"><b>Name</b></td>
					<td align="left" style="padding: 4px; background: #d0d0d0;"><b>Status</b></td>
				</tr>
				<?php
				foreach ($res_preguntas as $pregunta) { ?>
					<tr>
						<td align="left" style="vertical-align: top; padding: 4px; background: #f0f0f0;"><?php echo $pregunta->ID; ?></td>
						<td align="left" style="padding: 4px; background: #f0f0f0;"><?php echo $pregunta->post_title; ?></td>
						<td align="left" style="padding: 4px; background: #f0f0f0;"><?php echo $type; ?></td>
					</tr>
				<?php }
				?>
			</table>

		<?php }
		else { echo 'No se encontraron preguntas.'; }
		?>
		</div> <!-- .tabbertab -->

		<div class="tabbertab <?php if ($_GET['section']=='eventos') echo 'tabbertabdefault'; ?>">
		<h2>Eventos</h2>

		<p>
		<?php if ($type == 'familiares') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=familiares&section=eventos">familiares</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=familiares&section=eventos">familiares</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'sintomaticos') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=sintomaticos&section=eventos">sintomaticos</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=sintomaticos&section=eventos">sintomaticos</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'reflexivos') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=reflexivos&section=eventos">reflexivos</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=reflexivos&section=eventos">reflexivos</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'transgresores') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=transgresores&section=eventos">transgresores</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=transgresores&section=eventos">transgresores</a>
		<?php endif; ?>
		</p>

		<?php
		global $wpdb;
		$table = $wpdb->prefix . 'jet_events';
		$table2 = $wpdb->prefix . 'jet_events_eventmeta';

		// Eventos
		$res_eventos = $wpdb->get_results ('SELECT e.id, e.name, em.meta_value FROM '. $table .' e
		INNER JOIN '.$table2.' em ON e.id = em.event_id AND em.meta_key="ironestado" AND em.meta_value="'.$type.'"
		ORDER BY e.name');

		$neventos = count($res_eventos);
		if ($neventos>0) { ?>

			<p><b>NUM.: </b><?php echo $neventos; ?></p>

			<table>
				<tr>
					<td align="left" style="width: 20px; padding: 4px; background: #d0d0d0;"><b>Id</b></td>
					<td align="left" style="width: 350px; padding: 4px; background: #d0d0d0;"><b>Name</b></td>
					<td align="left" style="padding: 4px; background: #d0d0d0;"><b>Status</b></td>
				</tr>
				<?php
				foreach ($res_eventos as $evento) { ?>
					<tr>
						<td align="left" style="vertical-align: top; padding: 4px; background: #f0f0f0;"><?php echo $evento->id; ?></td>
						<td align="left" style="padding: 4px; background: #f0f0f0;"><?php echo $evento->name; ?></td>
						<td align="left" style="padding: 4px; background: #f0f0f0;"><?php echo $evento->meta_value; ?></td>
					</tr>
				<?php }
				?>
			</table>

		<?php }
		else { echo 'No se encontraron eventos.'; }
		?>
		</div> <!-- .tabbertab -->

		<div class="tabbertab <?php if ($_GET['section']=='activity') echo 'tabbertabdefault'; ?>">
		<h2>Actividades</h2>

		<p>
		<?php if ($type == 'familiares') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=familiares&section=activity">familiares</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=familiares&section=activity">familiares</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'sintomaticos') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=sintomaticos&section=activity">sintomaticos</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=sintomaticos&section=activity">sintomaticos</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'reflexivos') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=reflexivos&section=activity">reflexivos</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=reflexivos&section=activity">reflexivos</a>
		<?php endif; ?>
		&nbsp;|&nbsp;
		<?php if ($type == 'transgresores') : ?>
			<b><a href="admin.php?page=iron-categorize-handle&type=transgresores&section=activity">transgresores</a></b>
		<?php else : ?>
			<a href="admin.php?page=iron-categorize-handle&type=transgresores&section=activity">transgresores</a>
		<?php endif; ?>
		</p>

		<?php
		global $wpdb;
		$table = $wpdb->prefix . 'bp_activity';
		$table2 = $wpdb->prefix . 'bp_activity_meta';

		// Activity
		$res_activity = $wpdb->get_results ('SELECT a.id, a.content, am.meta_value FROM '. $table .' a
		INNER JOIN '.$table2.' am ON a.id = am.activity_id AND am.meta_key="current_mood" AND am.meta_value="'.$type.'"
		ORDER BY a.content');

		$nactivity = count($res_activity);
		if ($nactivity>0) { ?>

			<p><b>NUM.: </b><?php echo $nactivity; ?></p>

			<table>
				<tr>
					<td align="left" style="width: 20px; padding: 4px; background: #d0d0d0;"><b>Id</b></td>
					<td align="left" style="width: 350px; padding: 4px; background: #d0d0d0;"><b>Name</b></td>
					<td align="left" style="padding: 4px; background: #d0d0d0;"><b>Status</b></td>
				</tr>
				<?php
				foreach ($res_activity as $activity) { ?>
					<tr>
						<td align="left" style="vertical-align: top; padding: 4px; background: #f0f0f0;"><?php echo $activity->id; ?></td>
						<td align="left" style="padding: 4px; background: #f0f0f0;"><?php echo stripslashes( $activity->content ); ?></td>
						<td align="left" style="padding: 4px; background: #f0f0f0;"><?php echo $activity->meta_value; ?></td>
					</tr>
				<?php }
				?>
			</table>

		<?php }
		else { echo 'No se encontraron eventos.'; }
		?>
		</div> <!-- .tabbertab -->

	</div> <!-- .tabber -->

<?php }
?>