Skip to content

Commit

Permalink
Merge pull request #172 from Softcatala/programes_formulari
Browse files Browse the repository at this point in the history
#merged
  • Loading branch information
paugnu committed Mar 25, 2016
2 parents f7d9c82 + 83ba839 commit 69b1d03
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 176 deletions.
4 changes: 2 additions & 2 deletions archive-programa.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$context['post'] = $post;
$context['content_title'] = 'Programes i aplicacions';
$context['post_type'] = $post_type;
$context['conditions_text'] = "Si voleu afegir un programa nou...";
$context['conditions_text'] = get_option( 'sc_text_programes' );
$context['sidebar_top'] = Timber::get_widgets('sidebar_top');
$context['sidebar_bottom'] = Timber::get_widgets('sidebar_bottom');
$context['sidebar_elements'] = array( 'static/ajudeu.twig', 'static/dubte_forum.twig', 'baixades.twig', 'links.twig' );
Expand Down Expand Up @@ -93,4 +93,4 @@
if (count($context['posts']) == 0 && $flag_search == true ) {
throw_error( '404', 'No programs found' );
}
Timber::render( $templates, $context );
Timber::render( $templates, $context );
1 change: 1 addition & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function include_sc_settings() {
register_setting( 'softcatala-group', 'api_diccionari_sinonims' );
register_setting( 'softcatala-group', 'catalanitzador_post_id' );
register_setting( 'softcatala-group', 'aparells_post_id' );
register_setting( 'softcatala-group', 'sc_text_programes' );

//Email contact parameters
$sections = $this->get_email_sections();
Expand Down
84 changes: 53 additions & 31 deletions inc/ajax_operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
add_action( 'wp_ajax_nopriv_search_program', 'sc_search_program' );
add_action( 'wp_ajax_add_new_program', 'sc_add_new_program' );
add_action( 'wp_ajax_nopriv_add_new_program', 'sc_add_new_program' );
add_action( 'wp_ajax_add_new_baixada', 'sc_add_new_baixada' );
add_action( 'wp_ajax_nopriv_add_new_baixada', 'sc_add_new_baixada' );
/** CONTACT FORM **/
add_action( 'wp_ajax_contact_form', 'sc_contact_form' );
add_action( 'wp_ajax_nopriv_contact_form', 'sc_contact_form' );
Expand Down Expand Up @@ -235,6 +237,44 @@ function sc_contact_form() {
die();
}

/**
* Function to add a download related to a program
*
* @return json response
*/
function sc_add_new_baixada() {
$return = array();
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], $_POST["action"] )) {
$return['status'] = 0;
} else {
$baixades = json_decode(stripslashes($_POST["baixades"]));
$programa_id = sanitize_text_field( $_POST["programa_id"] );
$nom = sanitize_text_field( $_POST["nom"] );
$slug = sanitize_title_with_dashes( $nom );

//Related downloads
foreach ( $baixades as $baixada ) {
$terms_baixada = array(
'sistema-operatiu-programa' => array($baixada->sistema_operatiu)
);

$metadata_baixada = array (
'url_baixada' => $baixada->url,
'versio_baixada' => $baixada->versio,
'arquitectura_baixada' => $baixada->arquitectura,
'post_id' => $programa_id
);
$return_baixada = sc_add_draft_content('baixada', $nom, '', $slug, $terms_baixada, $metadata_baixada);

if( $return_baixada['status'] == 1 ) {
$return['status'] = 1;
}
}
}
$response = json_encode( $return );
die( $response );
}

/**
* Function to add a new draft program into database
*
Expand All @@ -243,7 +283,7 @@ function sc_contact_form() {
function sc_add_new_program() {
$return = array();
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], $_POST["action"] )) {
$return['status'] == 0;
$return['status'] = 0;
} else {
$nom = sanitize_text_field( $_POST["nom"] );
$email_usuari = sanitize_email( $_POST["email_usuari"] );
Expand All @@ -254,7 +294,6 @@ function sc_add_new_program() {
$llicencia = sanitize_text_field( $_POST["llicencia"] );
$categoria_programa = sanitize_text_field( $_POST["categoria_programa"] );
$slug = sanitize_title_with_dashes( $nom );
$baixades = json_decode(stripslashes($_POST["baixades"]));

$terms = array(
'categoria-programa' => array($categoria_programa),
Expand All @@ -278,35 +317,18 @@ function sc_add_new_program() {
);
sc_update_metadata ( $return['post_id'], $metadata );

//Related downloads
foreach ( $baixades as $baixada ) {
$terms_baixada = array(
'sistema-operatiu-programa' => array($baixada->sistema_operatiu)
);

$metadata_baixada = array (
'url_baixada' => $baixada->url,
'versio_baixada' => $baixada->versio,
'arquitectura_baixada' => $baixada->arquitectura,
'post_id' => $return['post_id']
);
$return_baixada = sc_add_draft_content('baixada', $nom, '', $slug, $terms_baixada, $metadata_baixada);
}
$to_email = get_option( 'email_rebost ' );
$nom_from = "Programes i aplicacions de Softcatalà";
$assumpte = "[Programes] Programa enviat per formulari";

if( $return_baixada['status'] == 1 ) {
$to_email = "web@softcatala.org";
$nom_from = "Programes i aplicacions de Softcatalà";
$assumpte = "[Programes] Programa enviat per formulari";

$fields = array(
"Nom del programa" => $nom,
"Descripció" => $descripcio,
"Comentari de l'usuari" => $comentari_usuari,
"Email de l'usuari" => $email_usuari,
"URL Dashboard" => admin_url("post.php?post=" . $return['post_id'] . "&action=edit")
);
sendEmailForm($to_email, $nom_from, $assumpte, $fields);
}
$fields = array(
"Nom del programa" => $nom,
"Descripció" => $descripcio,
"Comentari de l'usuari" => $comentari_usuari,
"Email de l'usuari" => $email_usuari,
"URL Dashboard" => admin_url("post.php?post=" . $return['post_id'] . "&action=edit")
);
sendEmailForm($to_email, $nom_from, $assumpte, $fields);
}
}

Expand Down Expand Up @@ -611,4 +633,4 @@ function sc_update_metadata( $post_id, $metadata ) {
$result = true;
}
return $result;
}
}
90 changes: 65 additions & 25 deletions static/js/programes.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function form_search_ok(result) {
var response = result.text;
}
jQuery("#text_response").html(response);
jQuery("#pas_2").show();
jQuery("#pas_1").show();
}

function form_sent_ko(result) {
Expand Down Expand Up @@ -172,6 +172,60 @@ $add_program_form.on('submit', function(ev) {
post_data.append('categoria_programa', jQuery('input[name=categoria_programa]:checked').val());
post_data.append('autor_traduccio', jQuery('input[name=autor_traduccio]').val());

post_data.append('action', 'add_new_program');
post_data.append('_wpnonce', jQuery('input[name=_wpnonce_program]').val());

var logo = jQuery(document).find('input[name="logo"]');
var logo_file = logo[0].files[0];
post_data.append("logo", logo_file);

var captura = jQuery(document).find('input[name="captura"]');
var captura_file = captura[0].files[0];
post_data.append("captura", captura_file);

jQuery.ajax({
type: 'POST',
url: scajax.ajax_url,
data: post_data,
dataType: 'json',
contentType: false,
processData: false,
success : form_add_ok,
error : form_sent_ko
});
});

function form_add_ok(result) {
jQuery("#loading_program").hide();
jQuery("#form_2").hide();
jQuery("#form_3").fadeIn();
jQuery("#form_3").addClass('actiu');
jQuery("#form_2").removeClass('actiu');
jQuery("#programa_id").val(result.post_id);
}

jQuery('#add_new_baixada').on('click', function () {
var content = jQuery('#baixada_fields').prop('outerHTML');
current_baixada_id = baixada_id;
baixada_id = baixada_id + 1;
pattern = "[1]";
re = new RegExp(pattern, "g");
res2 = content.replace(re, baixada_id);
jQuery( "#baixada_group").append(res2);
});

/** Formulari afegeix baixada **/
var $add_baixades_form = jQuery('#baixades_form');

$add_baixades_form.on('submit', function(ev) {
ev.preventDefault();
jQuery("#loading_program").fadeIn();

//Data
var post_data = new FormData();
post_data.append('programa_id', jQuery('input[name=programa_id]').val());
post_data.append('nom', jQuery('input[name=nom]').val());

//Programes
var urls_baixada = [];
jQuery(".url_baixada").each(function() {
Expand Down Expand Up @@ -208,18 +262,9 @@ $add_program_form.on('submit', function(ev) {
});

baixadesjson = JSON.stringify(baixades);

post_data.append('baixades', baixadesjson);
post_data.append('action', 'add_new_program');
post_data.append('_wpnonce', jQuery('input[name=_wpnonce_program]').val());

var logo = jQuery(document).find('input[name="logo"]');
var logo_file = logo[0].files[0];
post_data.append("logo", logo_file);

var captura = jQuery(document).find('input[name="captura"]');
var captura_file = captura[0].files[0];
post_data.append("captura", captura_file);
post_data.append('action', 'add_new_baixada');
post_data.append('_wpnonce', jQuery('input[name=_wpnonce_baixada]').val());

jQuery.ajax({
type: 'POST',
Expand All @@ -228,32 +273,27 @@ $add_program_form.on('submit', function(ev) {
dataType: 'json',
contentType: false,
processData: false,
success : form_add_ok,
error : form_sent_ko
success : form_baixada_add_ok,
error : form_baixada_add_ko
});
});

function form_add_ok(result) {
function form_baixada_add_ok() {
jQuery("#loading_program").hide();
jQuery("#form_3").hide();
jQuery("#form_4").fadeIn();
jQuery("#form_4").addClass('actiu');
jQuery("#form_3").removeClass('actiu');
}

jQuery('#add_new_baixada').on('click', function () {
var content = jQuery('#baixada_fields').prop('outerHTML');
current_baixada_id = baixada_id;
baixada_id = baixada_id + 1;
pattern = "[1]";
re = new RegExp(pattern, "g");
res2 = content.replace(re, baixada_id);
jQuery( "#baixada_group").append(res2);
});
function form_baixada_add_ko() {

}

jQuery('#afegeix_programa_button').on('click', function () {
if (!jQuery('#form_3').hasClass('actiu')) {
jQuery('#form_4').hide();
jQuery('#form_2').hide();
jQuery('#form_1').show();
}
});
});
13 changes: 13 additions & 0 deletions templates/admin/sc-dash.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<a data-id="llistes" class="nav-tab" href="#">Llistes de correu</a>
<a data-id="servidors_api" class="nav-tab" href="#">Servidors API</a>
<a data-id="rewrites" class="nav-tab" href="#">Paràmetres de rewrites</a>
<a data-id="textos" class="nav-tab" href="#">Textos</a>
</h2>

<form method="post" action="options.php">
Expand Down Expand Up @@ -80,6 +81,18 @@
</tbody></table>
</p>
</div>
<div id="textos" style="display:none;">
<h3>Textos generals del web</h3>
<p>
<table class="form-table">
<tbody><tr valign="top">
<tr>
<th scope="row"><label>Text del formulari per afegir programa nou</label></th>
<td><textarea style="width: 50%" id="sc_text_programes" name="sc_text_programes">{{ function( 'get_option', 'sc_text_programes') }}</textarea></td>
</tr>
</tbody></table>
</p>
</div>
</section>

{{ function('submit_button') }}
Expand Down
Loading

0 comments on commit 69b1d03

Please sign in to comment.