<?php
/*
Plugin Name: Irontec - Ayuda Modal
Plugin URI: http://www.irontec.com
Description: Create modal help dialogs in your theme.
Version: 0.1
Author: Irontec
Author URI: http://www.irontec.com
License: GNU General Public License 2.0 (GPL) http://www.gnu.org/licenses/gpl.html
*/

// Define plugin version and plugin db version
define( 'IRON_AYUDAMODAL_VERSION', '0.2' );
define( 'IRON_AYUDAMODAL_DB_VERSION', '0.1' );
define( 'IRON_AYUDAMODAL_URL', WP_PLUGIN_URL . '/irontec-ayudamodal' );

// Activation hook
register_activation_hook( __FILE__, 'iron_ayudamodal_activation' );
// Deactivation hook
register_deactivation_hook( __FILE__, 'iron_ayudamodal_deactivation' );
// Add the plugin Administration Menu
add_action('admin_menu', 'iron_ayudamodal_create_menu');

/*
Activation of the plugin.
*/
function iron_ayudamodal_activation() {
	// Check DB version first
	if ( get_site_option( 'iron_ayudamodal_db_version' ) < IRON_AYUDAMODAL_DB_VERSION || !get_site_option( 'iron_ayudamodal_db_version' )) {
		iron_ayudamodal_install_db();
	}
}

/*
Deactivation of the plugin.
*/
function iron_ayudamodal_deactivation() {
	// Do NOT delete DB TABLE upon deactivation
	iron_ayudamodal_uninstall_db();
}

/*
Installation of the DB tables
*/
function iron_ayudamodal_install_db () {
	global $wpdb;

	$iron_ayudamodal_db_version = IRON_AYUDAMODAL_DB_VERSION;

	if ( !empty($wpdb->charset) )
		$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";

	// Create table ayudamodal
	$table_name = $wpdb->prefix . "iron_ayudamodal";

	if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {

		$sql[] = "CREATE TABLE {$table_name} (
	  		id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
			name varchar(250) NOT NULL,
			content text(0) NOT NULL,
			content_eu text(0) NOT NULL,
			content_en text(0) NOT NULL
	 	   ) {$charset_collate};";

		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);
	}

	// Default options
	add_option("iron_ayudamodal_db_version", $iron_ayudamodal_db_version);
}

/*
Uninstall of the DB tables
*/
function iron_ayudamodal_uninstall_db () {
	global $wpdb;

	// delete options from wp_options
	delete_option("iron_ayudamodal_db_version");
}

/*
Administration Menu
*/
function iron_ayudamodal_create_menu() {
	//create new top-level menu
	add_menu_page('Iron Ayuda', 'Iron Ayuda', 'manage_options', 'iron-ayuda-handle', 'iron_ayudamodal_front_page', plugins_url('images/irontec.gif', __FILE__));
}

/*
Administration Front page
*/
function iron_ayudamodal_front_page() {

	// Styles
	wp_deregister_style('iron-ayudamodal-css');
	wp_enqueue_style('iron-ayudamodal-css', IRON_AYUDAMODAL_URL.'/css/style.css');
	wp_print_styles('iron-ayudamodal-css');

	// 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');

	// Limpiamos variables
	$act=trim(substr(strip_tags($_GET['act']),0,10));
	$_GET['id']=trim(substr(strip_tags($_GET['id']),0,5));

	$cur_menu="iron-ayuda-handle";
	?>
	<div class="ratingadmindiv wrap">
	<img style="float: left; margin: 13px;" src="<?php echo IRON_AYUDAMODAL_URL; ?>/images/logo_irontec_2.0.png" />
	<h2>Ayuda Modal Front Page</h2>

	<?php
    global $wpdb;

    // action delete
    if ($act=="delete" && $_GET['id']) {

        $table_name = $wpdb->prefix . "iron_ayudamodal";
        $id=trim(substr(strip_tags($_GET['id']),0,3));

        $wpdb->query("DELETE FROM ".$table_name." WHERE id=".$id);
        echo '<p>Registro eliminado.</p>';
        echo '<p><a href="admin.php?page='. $cur_menu .'">Volver</a></p>';
    }
    // action add
    elseif ($act=="add") { ?>

    <div class="ironclear"></div>

    <p>&middot;&nbsp;<b>Insertando</b>&nbsp;|&nbsp;<a href="?page=<?php echo $cur_menu; ?>">Volver</a></p>

        <form id="iron-tunnels-form" method="post" action="">

        <table class="form-table">
            <tr valign="top">
            <td class="iron-td" style="width: 100px;"><b>Nombre</b></td>
            <td>
            <input type="text" name="Name" style="width: 300px;">
            </td>
            </tr>
            <tr valign="top">
            <td class="iron-td" style="width: 100px;"><b>Texto (ES)</b></td>
            <td>
            <textarea name="Content" style="width: 300px; height: 80px;"></textarea>
            </td>
            </tr>
            <tr valign="top">
            <td class="iron-td" style="width: 100px;"><b>Texto (EU)</b></td>
            <td>
            <textarea name="ContentEu" style="width: 300px; height: 80px;"></textarea>
            </td>
            </tr>
            <tr valign="top">
            <td class="iron-td" style="width: 100px;"><b>Texto (EN)</b></td>
            <td>
            <textarea name="ContentEn" style="width: 300px; height: 80px;"></textarea>
            </td>
            </tr>
        </table>

        <p class="submit">
            <input type="hidden" name="act" value="add">
            <input type="hidden" name="conf" value="true">
            <input type="hidden" name="page" value="<?php echo $cur_menu; ?>">
            <input type="submit" class="button-primary" value="<?php _e('Guardar') ?>" />
        </p>

        </form>

        <?php
        // Insert
        $Name=trim(substr(strip_tags($_POST['Name']),0,100));
        $Content=trim(substr(strip_tags($_POST['Content']),0,800));
        $ContentEu=trim(substr(strip_tags($_POST['ContentEu']),0,800));
        $ContentEn=trim(substr(strip_tags($_POST['ContentEn']),0,800));

        if ($_POST['conf'] == "true") {

            $table_name = $wpdb->prefix . "iron_ayudamodal";
            $exists = $wpdb->get_results( "SELECT (id) AS itemid FROM ".$table_name." WHERE name LIKE '".$Name."'");
            // Db Insert
            if ($exists[0]->itemid == 0) {
                $wpdb->query('INSERT INTO '.$table_name.' (name, content, content_eu, content_en) VALUES ("'.$Name.'", "'.$Content.'", "'.$ContentEu.'", "'.$ContentEn.'")');
                echo "<script> alert (\"Ayuda insertada.\"); </script>";
            }
            else
                echo "<script> alert (\"Ya existe una etiqueta con ese nombre !.\"); </script>";
        }
    }
    // action edit
    elseif ($act=="edit" && $_GET['id']) {

        $table_name = $wpdb->prefix . "iron_ayudamodal";
        $row = $wpdb->get_results( "SELECT * FROM ".$table_name." WHERE id=".$_GET['id']);
        ?>

        <div class="ironclear"></div>

        <p>&middot;&nbsp;<b>Editando</b>&nbsp;|&nbsp;<a href="?page=<?php echo $cur_menu; ?>">Volver</a></p>

        <form id="iron-tunnels-form" method="post" action="">

        <table class="form-table">
            <tr valign="top">
            <td class="iron-td" style="width: 100px;"><b>Nombre</b></td>
            <td>
            <input type="text" name="Name" style="width: 300px;" value="<?php echo $row[0]->name; ?>">
            </td>
            </tr>
            <tr valign="top">
            <td class="iron-td" style="width: 100px;"><b>Texto (ES)</b></td>
            <td>
            <textarea name="Content" style="width: 300px; height: 80px;"><?php echo $row[0]->content; ?></textarea>
            </td>
            </tr>
            <tr valign="top">
            <td class="iron-td" style="width: 100px;"><b>Texto (EU)</b></td>
            <td>
            <textarea name="ContentEu" style="width: 300px; height: 80px;"><?php echo $row[0]->content_eu; ?></textarea>
            </td>
            </tr>
            <tr valign="top">
            <td class="iron-td" style="width: 100px;"><b>Texto (EN)</b></td>
            <td>
            <textarea name="ContentEn" style="width: 300px; height: 80px;"><?php echo $row[0]->content_en; ?></textarea>
            </td>
            </tr>
        </table>

        <p class="submit">
            <input type="hidden" name="act" value="edit">
            <input type="hidden" name="conf" value="true">
            <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
            <input type="hidden" name="page" value="<?php echo $cur_menu; ?>">
            <input type="submit" class="button-primary" value="<?php _e('Guardar') ?>" />
        </p>

        </form>

        <?php
        // Edit
        if ($_POST['conf'] == "true" && $_POST['id']) {
            // RECOGEMOS LAS VARIABLES
            $Name=trim(substr(strip_tags($_POST['Name']),0,100));
            $Content=trim(substr(strip_tags($_POST['Content']),0,800));
            $ContentEu=trim(substr(strip_tags($_POST['ContentEu']),0,800));
            $ContentEn=trim(substr(strip_tags($_POST['ContentEn']),0,800));

            // GUARDAMOS EN LA BBDD
            $table_name = $wpdb->prefix . "iron_ayudamodal";
            $wpdb->query('UPDATE '.$table_name.' SET name="'.$Name.'", content="'.$Content.'", content_eu="'.$ContentEu.'", content_en="'.$ContentEn.'" WHERE id='.$_POST['id']);

            echo "<script> alert (\"Ayuda modificada.\"); </script>";
        }
    }
    else {
    ?>

    <div class="ironclear"></div>

    <p>&middot;&nbsp;<a href="?page=<?php echo $cur_menu; ?>&act=add">A&ntilde;adir Ayuda</a></p>

    <?php
    global $wpdb;
    $table_name = $wpdb->prefix . "iron_ayudamodal";

    // Obtain the different item types
    $results = $wpdb->get_results(
    "SELECT * FROM ".$table_name." ORDER BY Name");

    if (sizeof($results) > 0) { ?>
        <div class="tunnels-overflow">
            <table id="iron-tunnels-table">
                <tr>
                    <td class="iron-td-mini iron-header"><b>Acciones</b></td>
                    <td class="iron-td-mini iron-header"><b>ID</b></td>
                    <td class="iron-td-normal iron-header" style="width: 200px;"><b>Nombre</b></td>
                    <td class="iron-td-normal iron-header"><b>Texto (ES)</b></td>
                    <td class="iron-td-normal iron-header"><b>Texto (EU)</b></td>
                    <td class="iron-td-normal iron-header"><b>Texto (EN)</b></td>
                </tr>
                <?php
                    foreach ($results as $item_help) {
                        ?>
                                <tr>
                                <td class="iron-td-mini">
                                <a href="?page=<?php echo $cur_menu; ?>&act=edit&id=<?php echo $item_help->id; ?>"><img src="<?php echo IRON_AYUDAMODAL_URL.'/images/edit.gif'; ?>" alt="<?php _e( 'Editar','buddypress' ) ?>" title="<?php _e( 'Editar','buddypress' ) ?>"></a>
                                <a href="?page=<?php echo $cur_menu; ?>&act=delete&id=<?php echo $item_help->id; ?>" onclick="if(!confirm('Eliminar registro ?')) {return false;}"><img src="<?php echo IRON_AYUDAMODAL_URL.'/images/delete.gif'; ?>" alt="<?php _e( 'Eliminar','buddypress' )?>" title="<?php _e( 'Eliminar','buddypress' )?>"></a>
                                </td>
                                <td class="iron-td-small"><?php echo $item_help->id; ?></td>
                                <td class="iron-td-normal"><?php echo $item_help->name; ?></td>
                                <td class="iron-td-normal"><?php echo $item_help->content; ?></td>
                                <td class="iron-td-normal"><?php echo $item_help->content_eu; ?></td>
                                <td class="iron-td-normal"><?php echo $item_help->content_en; ?></td>
                                </tr>
                            <?php }
                            ?>
                        </table>
                        </div>
                    <?php }
                    else
                        echo "<p>No hay ayudas creadas !</p>";
                    ?>


	</div> <!-- admindiv -->
	<?php }
}
?>