<?php
global $bp;

/**
 * Si es un miembro privado no puede postear
 */
if ( xprofile_get_field_data( 'Privacidad', get_current_user_id()) == 'Privado' ) {
    return false;
}

/**
 * Fileuploader
 */
wp_deregister_script( 'fileuploader' );
wp_register_script( 'fileuploader', get_stylesheet_directory_uri() .'/_inc/muro/fileuploader.js', '' );
wp_print_scripts( 'fileuploader' );
wp_deregister_style('fileuploader-css');
wp_enqueue_style('fileuploader-css', get_stylesheet_directory_uri() .'/_inc/muro/fileuploader.css', '' );
wp_print_styles('fileuploader-css');

// File Upload
$file_value = $_FILES['myfile']['name'];
$size       = $_FILES['myfile']['size'];
$mime       = $_FILES['myfile']['type'];

if ($_FILES['myfile']['tmp_name']) {
    $tam = getimagesize($_FILES['myfile']['tmp_name']);
}

// Filename
$tmp_filename = $_FILES['myfile']['tmp_name'];
$tmp_filename = explode('/',$tmp_filename);
$tmp_filename = $tmp_filename[sizeof($tmp_filename)-1];

if ($tmp_filename) {
    $new_filename = str_replace("tmp_","",$tmp_filename);
    $new_filename = str_replace(".tmp","",$new_filename);
    $new_filename = $new_filename.".jpg";
}

if ($new_filename) { 
    $post_type="image";
} else {
    $post_type="text";
}

// CHEQUEOS
if ( $size < 2500000 && (
                            $mime == "image/jpeg"  ||
                            $mime == "image/pjpeg" ||
                            $mime == "image/png"   ||
                            $mime == "image/gif"
                        )
    ) {
    $result = 0;
} else {
    $result = 2;
}

// SI PASA LOS CHEQUEOS, SUBIMOS LA IMAGEN
if ($result == 0){

    // Ruta a uploads
    $path = wp_upload_dir();
    $path = $path["basedir"].'/activity/'.$bp->loggedin_user->id;
    //$path= SITEROOT . '/wp-content/uploads/activity/'.$bp->loggedin_user->id;
    //Si no existe la carpeta /tmp la crea
    if (!file_exists($path)) {
        // chmod
        mkdir($path, 0777, true);
        chmod($path, 0777);
    }

    // Rutas completas
    $tmp_dest_path   = $path."/".$tmp_filename;
    $dest_path       = $path."/".$new_filename;
    $dest_path_thumb = $path."/thumb_".$new_filename;
    $dest_path_full  = $path."/full_".$new_filename;

    if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $tmp_dest_path)) {
        $result = 1;
    }

    // Generate thumb
    ImgResize($tmp_dest_path, $dest_path_thumb, 0,40);
    // Copy full size
    ImgResize($tmp_dest_path, $dest_path_full, 0,0);

    // If the width is > 400, resize the image
    if ($tam[0]>"400") {
        if ($tam[0]>"400") ImgResize($tmp_dest_path, $dest_path, 400,0);
    } else {
        // Do not resize
        ImgResize($tmp_dest_path, $dest_path, 0,0);
    }

    // chmod
    chmod($dest_path, 0777);

    if (file_exists($tmp_dest_path))
        unlink($tmp_dest_path);
    }

//FUNCION PARA RESIZEAR LAS IMAGENES QUE SE SUBEN
//$ruta ES DONDE ESTA LA IMAGEN
//$maxx ES LA ANCHURA MAXIMA QUE VA A TENER
//$maxy ES LA ALTURA MAXIMA QUE VA A TENER
function ImgResize($nombre_logo, $ruta, $maxx, $maxy) {
      
    if ($nombre_logo!="") {
        
        $info = getimagesize($nombre_logo);
        
        $X = $info[0];
        $Y = $info[1];
        if ($maxx >$X || $maxx==0)
            $maxx=$X;
        if ($maxy>$Y || $maxy==0)
            $maxy=$Y;
        $escX=$X/$maxx;
        $escY=$Y/$maxy;
        $n=0;
        if ($info[2]==1)
            $imOrigen=imagecreatefromgif($nombre_logo);
        elseif ($info[2]==2)
            $imOrigen=imagecreatefromjpeg($nombre_logo);
        elseif ($info[2]==3)
            $imOrigen=imagecreatefrompng($nombre_logo);
        else
            $no=1;
        if ($n==0) {
            if ($escX<$escY) {
                $nX=$X/$escY;
                $nY=$Y/$escY;
            }
            else {
                $nX=$X/$escX;
                $nY=$Y/$escX;
            }
        $imDestino=imagecreatetruecolor($nX,$nY);
        imagecopyresampled($imDestino,$imOrigen,0,0,0,0,$nX,$nY,$X,$Y);
        imagejpeg($imDestino,$ruta,100);
        }
    }
}
?>

<div class="iron-activity-share-text">
    <?php _e ('&iquest; Qu&eacute; tal hoy ?','buddypress'); ?>
    <div class="pop-status-content">
        <p class="pop-status-friends">
            <?php _e( 'Esta publicación solo la vera tus amigos.', 'kronikoensarea' ); ?>
            <button type="button" class="close" data-dismiss="alert">×</button>
        </p>
    </div>
</div>

<?php
/*
<div style="float: right; margin-right: 18px; margin-top: -26px;">
<a class="cluetip" href="#" rel="<?php echo get_stylesheet_directory_uri() ?>/_inc/tips.php?id=2">
<img src="<?php echo get_stylesheet_directory_uri() ?>/_inc/images/icon_help.gif" />
</a>
</div>
*/
?>

<div id="iron-activity-share">

    <div id="dinamic_upload" class="loadimg" style="height: 45px; margin-left: 15px; <?php if (!$_FILES['myfile']['tmp_name']) echo 'display: none;'; ?>">
        <form id="uploadform" name="uploadform" action="" method="post" enctype="multipart/form-data" >
            <div id="upload-loading" class="divloading" style="position: absolute; margin: auto; margin-left: 345px; margin-top: 6px; display: none;">
                <p id="f1_upload_process">
                    <img src="<?php echo get_stylesheet_directory_uri() . '/_inc/images/loading.gif'; ?>" />
                </p>
            </div>
            <?php if ($_FILES['myfile']['tmp_name']) { ?>
                <div id="upload-result" style="position: absolute; margin-left: 350px; margin-top: -7px;">
                    <img src=<?php echo site_url() . '/wp-content/uploads/activity/'. get_current_user_id().'/thumb_'.$new_filename; ?> />
                </div>
            <?php } ?>
            <p id="f1_upload_form" style="margin: 0px;">
                <div>
                  <div id="imageFileuploader">
                      <?php echo __('seleccionar imagen','buddypress'); ?>
                  </div>
                </div>
                
                <img src="<?php echo site_url() . '/wp-content/themes/cvp-irontec/_inc/images/button_cancel.png'; ?>" id="imageFileuploader-cancel" >
                <span id="info" class="iron-file-name"></span>
            </p>
        </form>
    </div>
    
    <form action="<?php bp_activity_post_form_action() ?>" method="post" id="whats-new-form" name="whats-new-form" class="standard-form" enctype="multipart/form-data">

        <?php do_action( 'bp_before_activity_post_form' ) ?>

        <div id="add_data" style="margin-left: 20px; height: 38px; display: none;">
            <input name="activity-enlace" id="activity-enlace" maxlength="250" type="text" style="height: 12px; width: 250px;" value="http://">
            <img src="<?php echo site_url() . '/wp-content/themes/cvp-irontec/_inc/images/button_cancel.png'; ?>" id="activity-enlace-cancel" >
        </div>

        <div id="whats-new-content">
            <center>
                <div id="iron-share-div" <?php if (!$_FILES['myfile']['tmp_name']) echo 'class="post-bar-close"'; else echo 'class="post-bar-open"'; ?>>
                    <fieldset>
                    
                        <textarea cols="50" rows="4" name="whats-new" id="whats-new"><?php if ( isset( $_GET['r'] ) ) : ?>@<?php echo esc_attr( $_GET['r'] ) ?> <?php endif; ?></textarea>
                        
                        <div class="post-bar-options">
                            <ul style="display:none;">
                                <li id="share-options-image" style="cursor: pointer; font-size: 10px; margin-right: 8px;">
                                 <img style="float: left; margin-right: 4px; cursor: pointer;" src="<?php echo get_stylesheet_directory_uri() ?>/_inc/images/icon_post_pic.png" />
                                <?php echo __('Fotos', 'buddypress'); ?></li>
                                <li id="share-options-link" style="cursor: pointer; font-size: 10px; margin-bottom: 5px;">
                                 <img style="float: left; margin-right: 4px; cursor: pointer;" src="<?php echo get_stylesheet_directory_uri() ?>/_inc/images/icon_post_link.png" />
                                <?php echo __('Enlaces', 'buddypress'); ?></li>
                                <!--<li id="share-options-type">contenido</li>-->
    
                                <?php
                                global $bp;
                                if ($bp->current_component != 'groups' ) { ?>
                                    
                                    <li id="share-options-priv">
                                        <img src="<?php echo get_stylesheet_directory_uri(); ?>/_inc/images/closed.png" alt="Privacidad" title="<?php _e( 'Privacidad','buddypress' ) ?>" />
                                        <select name="activity-privacity" id="activity-privacity">
                                        <option value="amigos"><?php _e ('con mis amigos','buddypress'); ?></option>
                                        <?php /*<option value="privado">s&oacute;lo para m&iacute;</option>*/?>
                                        <option value="publico" selected><?php _e ('con toda la comunidad','buddypress'); ?></option>
                                        </select>
                                    </li>
                                <?php } else { ?>
                                    <li id="share-options-priv">
                                    &nbsp;
                                    </li>
                                    <input type="hidden" name="activity-privacity" id="activity-privacity" value="amigos">
                                <?php } ?>
                                
                            </ul>
                            
                            <span class="ajax-loader"></span>
                                <input type="submit" name="aw-whats-new-submit" id="aw-whats-new-submit" value="<?php _e( 'Publicar', 'buddypress' ) ?>" />
                        </div>
                        
                    </fieldset>
                    
                </div><!-- iron-share-div -->
            </center>
            <?php wp_nonce_field( 'post_update', '_wpnonce_post_update' ); ?>

            <input type="hidden" name="activity-origmood" id="activity-origmood" value="<?php echo $origmood; ?>">
            <input type="hidden" name="activity-type" id="activity-type" value="<?php echo $post_type; ?>">
            <input type="hidden" name="activity-imagen" id="activity-imagen" value="<?php echo $new_filename; ?>">

        <?php if ( function_exists('bp_has_groups') && !bp_is_my_profile() && !bp_is_group() ) : ?>

            <input type="hidden" id="whats-new-post-object" name="whats-new-post-object" value="groups" />

        <?php elseif ( bp_is_group_home() ) : ?>

            <input type="hidden" id="whats-new-post-object" name="whats-new-post-object" value="groups" />
            <input type="hidden" id="whats-new-post-in" name="whats-new-post-in" value="<?php bp_group_id() ?>" />

        <?php endif; ?>

        </div><!-- #whats-new-content -->

        <?php do_action( 'bp_after_activity_post_form' ) ?>

    </form><!-- #whats-new-form -->
</div><!-- iron-activity-share -->