Skip to content

Commit 12bf304

Browse files
committed
Merge pull request #24 from diraol/issue75
#75 - Interface de gerência de contribuição em PDF
2 parents 9bba038 + 032e993 commit 12bf304

File tree

3 files changed

+126
-7
lines changed

3 files changed

+126
-7
lines changed

content-texto-em-debate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<h4 class="divider-top red font-roboto mt-lg pt-lg">Contribuições em PDF</h4>
1414
<!-- data-section-id iniciando em um valor alto para não conflitar com os comentários do texto -->
1515
<?php
16-
$pdf_contribution_list = get_post_meta(get_the_ID(), 'pdf_contribution_list');
17-
16+
$pdf_contribution_list = get_post_meta(get_the_ID(), 'pdf_contribution_list', true);
17+
1818
$data_section = 10000;
1919

2020
foreach ($pdf_contribution_list as $pdf_contribution) {

functions.php

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,30 @@
3131
"#8B3E6B" );
3232

3333

34+
add_action( 'wp_enqueue_scripts', 'dadospessoais_scripts' );
3435
function dadospessoais_scripts() {
3536
wp_enqueue_script( 'dadospessoais', get_stylesheet_directory_uri() . '/js/dadospessoais.js' , array('jquery') );
3637

3738
$var_plataforma = array();
3839
$var_plataforma['signup_url'] = wp_registration_url();
3940
$var_plataforma['login_url'] = wp_login_url();
41+
$var_plataforma['configs'] = array(
42+
'ajaxurl' => admin_url('admin-ajax.php'),
43+
'ajaxgif' => get_template_directory_uri() . '/images/ajax-loader.gif'
44+
);
45+
wp_localize_script( 'dadospessoais', 'dadosPessoais', $var_plataforma );
46+
}
4047

48+
49+
add_action( 'admin_enqueue_scripts', 'dadospessoais_admin_scripts');
50+
function dadospessoais_admin_scripts() {
51+
wp_enqueue_script( 'dadospessoais', get_stylesheet_directory_uri() . '/js/dadospessoais-admin.js' , array('jquery') );
52+
$var_plataforma = array();
53+
$var_plataforma['ajaxurl'] = admin_url('admin-ajax.php');
54+
$var_plataforma['ajaxgif'] = get_template_directory_uri() . '/images/ajax-loader.gif';
4155
wp_localize_script( 'dadospessoais', 'dadosPessoais', $var_plataforma );
4256
}
4357

44-
add_action( 'wp_enqueue_scripts', 'dadospessoais_scripts' );
4558

4659
// Registro do Custom Post Type "Texto em Discussão"
4760
function dados_pessoais_post_types() {
@@ -122,11 +135,10 @@ function dados_pessoais_post_types() {
122135
'register_meta_box_cb' => 'dadospessoais_eixo_metaboxes'
123136
);
124137
register_post_type( 'eixo-de-debate', $args_eixo );
125-
126138
}
127-
128139
add_action( 'init', 'dados_pessoais_post_types', 10, 2);
129140

141+
130142
/**
131143
* Incluí novas cores no editor visual
132144
*
@@ -201,6 +213,8 @@ function dadospessoais_cor_eixo_metabox() {
201213
* @return mixed
202214
*/
203215
function dadospessoais_salvar_cor_meta($post_id, $post) {
216+
print_r($_POST);
217+
wp_die();
204218
if (!isset($_POST['cormeta_noncename'])) {
205219
return $post->ID;
206220
}
@@ -211,8 +225,9 @@ function dadospessoais_salvar_cor_meta($post_id, $post) {
211225
}
212226

213227
// O usuário tem permissão de edição de página?
214-
if ( !current_user_can( 'edit_page', $post->ID ))
228+
if ( !current_user_can( 'edit_page', $post->ID )) {
215229
return $post->ID;
230+
}
216231

217232
$cor_eixo = $_POST['_cor_eixo'];
218233

@@ -350,9 +365,64 @@ function dadospessoais_get_english_information_link() {
350365
return get_permalink($page_english);
351366
}
352367

368+
353369
/**
354370
* Obtem o objeto page do anteprojeto
355371
*/
356372
function dadospessoais_get_anteprojeto_page() {
357373
return dadospessoais_get_by_slug('anteprojeto-de-lei-para-a-protecao-de-dados-pessoais', OBJECT, 'texto-em-debate');
358-
}
374+
}
375+
376+
377+
/**
378+
* Adicionando MetaBoxes na área de admin para gerenciar os PDFs enviados como contribuições
379+
*/
380+
add_action( 'add_meta_boxes', 'dadospessoais_exibicao_metabox' );
381+
function dadospessoais_exibicao_metabox()
382+
{
383+
add_meta_box( 'contribuicoes-pdf', 'Contribuições em PDF', 'dadospessoais_contribuicoes_render', 'texto-em-debate', 'side', 'core' );
384+
}
385+
386+
387+
function dadospessoais_contribuicoes_render($post)
388+
{
389+
$pdf_contribution_list = get_post_meta($post->ID, 'pdf_contribution_list', true);
390+
391+
$counter = 1;
392+
foreach ($pdf_contribution_list as $key=> $pdf_contribution) {
393+
?>
394+
<p id="delete_pdf_key_<?php echo $key; ?>">
395+
<b>Contribuição <?php echo $counter; ?></b>
396+
<input type="button" class="ed_button button button-small" onclick="deletar_pdf(<?php echo $key; ?>, <?php echo $post->ID; ?>)" value="Deletar"/><br/>
397+
Autor:<br/>
398+
<input type="text" value = "<?php echo $pdf_contribution['author']; ?>" id="autor_contribuicao_<?php echo $key ?>"/>
399+
<input type="button" class="ed_button button button-small" value="OK" onclick="altera_autor_pdf(<?php echo $key; ?>, <?php echo $post->ID; ?>, jQuery('#autor_contribuicao_<?php echo $key; ?>').val())"></br>
400+
<a href="<?php echo $pdf_contribution['pdf_url']; ?>">Contribuição</a>
401+
</p>
402+
<hr/>
403+
<?php
404+
$counter++;
405+
}
406+
}
407+
408+
409+
410+
function dadospessoais_remove_pdf_callback(){
411+
$pdf_contribution_list = get_post_meta($_POST['post_ID'], 'pdf_contribution_list', true);
412+
$base_upload_path = wp_upload_dir();
413+
414+
if (unlink($base_upload_path['basedir'] . explode($base_upload_path['baseurl'],$pdf_contribution_list[$_POST['chave']]['pdf_url'])[1])) {
415+
unset($pdf_contribution_list[$_POST['chave']]);
416+
update_post_meta($_POST['post_ID'],'pdf_contribution_list',$pdf_contribution_list);
417+
} else {
418+
return false;
419+
}
420+
}
421+
add_action('wp_ajax_dadospessoais_remove_pdf', 'dadospessoais_remove_pdf_callback');
422+
423+
function dadospessoais_altera_autor_pdf_callback(){
424+
$pdf_contribution_list = get_post_meta($_POST['post_ID'], 'pdf_contribution_list', true);
425+
$pdf_contribution_list[$_POST['chave']]['author'] = $_POST['novo_autor'];
426+
update_post_meta($_POST['post_ID'],'pdf_contribution_list',$pdf_contribution_list);
427+
}
428+
add_action('wp_ajax_dadospessoais_altera_autor_pdf', 'dadospessoais_altera_autor_pdf_callback');

js/dadospessoais-admin.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function deletar_pdf(key, post_ID) {
2+
var loader = '<span id="loader-gif">Carregando mais notícias... <img src="' + dadosPessoais.ajaxgif + '"/></span>';
3+
jQuery("#delete_pdf_key_" + key).append(loader);
4+
5+
var data = {
6+
'action': 'dadospessoais_remove_pdf',
7+
'chave': key,
8+
'post_ID': post_ID
9+
}
10+
11+
jQuery.ajax({
12+
url: dadosPessoais.ajaxurl,
13+
method: 'POST',
14+
data: data,
15+
success: function (html) {
16+
jQuery('#loader-gif').remove();
17+
jQuery("#delete_pdf_key_" + key).remove();
18+
},
19+
error: function(){
20+
alert("Erro ao deletar arquivo");
21+
}
22+
});
23+
24+
return false;
25+
}
26+
27+
function altera_autor_pdf(key, post_ID, novo_autor) {
28+
var loader = '<span id="loader-gif">Carregando mais notícias... <img src="' + dadosPessoais.ajaxgif + '"/></span>';
29+
jQuery("#delete_pdf_key_" + key).append(loader);
30+
31+
var data = {
32+
'action': 'dadospessoais_altera_autor_pdf',
33+
'chave': key,
34+
'post_ID': post_ID,
35+
'novo_autor': novo_autor
36+
}
37+
38+
jQuery.ajax({
39+
url: dadosPessoais.ajaxurl,
40+
method: 'POST',
41+
data: data,
42+
success: function (html) {
43+
jQuery('#loader-gif').remove();
44+
},
45+
error: function(){
46+
alert("Erro ao modificar autor");
47+
}
48+
});
49+
}

0 commit comments

Comments
 (0)