Initial commit
This commit is contained in:
99
includes/class-ramais-admin.php
Normal file
99
includes/class-ramais-admin.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Ramais_Admin {
|
||||
private $database;
|
||||
|
||||
public function __construct($database) {
|
||||
$this->database = $database;
|
||||
$this->init_hooks();
|
||||
}
|
||||
|
||||
private function init_hooks() {
|
||||
add_action('admin_menu', array($this, 'add_admin_menu'));
|
||||
add_action('admin_post_add_ramal', array($this, 'handle_add_ramal'));
|
||||
}
|
||||
|
||||
public function add_admin_menu() {
|
||||
add_menu_page(
|
||||
__('Ramais Telefônicos', 'ramais-telefonicos'),
|
||||
__('Ramais', 'ramais-telefonicos'),
|
||||
'manage_options',
|
||||
'ramais-telefonicos',
|
||||
array($this, 'render_admin_page'),
|
||||
'dashicons-phone',
|
||||
20
|
||||
);
|
||||
}
|
||||
|
||||
public function render_admin_page() {
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_die(__('Você não tem permissão para acessar esta página.', 'ramais-telefonicos'));
|
||||
}
|
||||
|
||||
$status = isset($_GET['status']) ? sanitize_text_field($_GET['status']) : '';
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1><?php _e('Adicionar Novo Ramal', 'ramais-telefonicos'); ?></h1>
|
||||
|
||||
<?php if ($status === 'success'): ?>
|
||||
<div class="notice notice-success"><p><?php _e('Ramal adicionado com sucesso!', 'ramais-telefonicos'); ?></p></div>
|
||||
<?php elseif ($status === 'error'): ?>
|
||||
<div class="notice notice-error"><p><?php _e('Erro ao adicionar ramal. Verifique os dados.', 'ramais-telefonicos'); ?></p></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
|
||||
<input type="hidden" name="action" value="add_ramal">
|
||||
<?php wp_nonce_field('add_ramal_nonce'); ?>
|
||||
|
||||
<table class="form-table">
|
||||
<tr>
|
||||
<th scope="row"><label for="ramal"><?php _e('Ramal', 'ramais-telefonicos'); ?></label></th>
|
||||
<td><input type="text" name="ramal" id="ramal" class="regular-text" required></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="responsavel"><?php _e('Responsável', 'ramais-telefonicos'); ?></label></th>
|
||||
<td><input type="text" name="responsavel" id="responsavel" class="regular-text" required></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="secretaria"><?php _e('Secretaria', 'ramais-telefonicos'); ?></label></th>
|
||||
<td><input type="text" name="secretaria" id="secretaria" class="regular-text" required></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="setor"><?php _e('Setor', 'ramais-telefonicos'); ?></label></th>
|
||||
<td><input type="text" name="setor" id="setor" class="regular-text" required></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="email"><?php _e('Email', 'ramais-telefonicos'); ?></label></th>
|
||||
<td><input type="email" name="email" id="email" class="regular-text" required></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<?php submit_button(__('Adicionar Ramal', 'ramais-telefonicos')); ?>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function handle_add_ramal() {
|
||||
if (!current_user_can('manage_options') || !wp_verify_nonce($_POST['_wpnonce'], 'add_ramal_nonce')) {
|
||||
wp_die(__('Ação não autorizada.', 'ramais-telefonicos'));
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'ramal' => $_POST['ramal'] ?? '',
|
||||
'responsavel' => $_POST['responsavel'] ?? '',
|
||||
'secretaria' => $_POST['secretaria'] ?? '',
|
||||
'setor' => $_POST['setor'] ?? '',
|
||||
'email' => $_POST['email'] ?? ''
|
||||
);
|
||||
|
||||
$success = $this->database->add_ramal($data);
|
||||
$status = $success ? 'success' : 'error';
|
||||
|
||||
wp_redirect(admin_url('admin.php?page=ramais-telefonicos&status=' . $status));
|
||||
exit;
|
||||
}
|
||||
}
|
43
includes/class-ramais-ajax.php
Normal file
43
includes/class-ramais-ajax.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Ramais_Ajax {
|
||||
private $database;
|
||||
|
||||
public function __construct($database) {
|
||||
$this->database = $database;
|
||||
$this->init_hooks();
|
||||
}
|
||||
|
||||
private function init_hooks() {
|
||||
add_action('wp_ajax_filtrar_ramais', array($this, 'ajax_filter_ramais'));
|
||||
add_action('wp_ajax_nopriv_filtrar_ramais', array($this, 'ajax_filter_ramais'));
|
||||
}
|
||||
|
||||
public function ajax_filter_ramais() {
|
||||
check_ajax_referer('ramais_telefonicos_nonce', 'nonce');
|
||||
|
||||
$page = isset($_POST['page']) ? absint($_POST['page']) : 1;
|
||||
$filters = [
|
||||
'search' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
|
||||
'secretaria' => isset($_POST['secretaria']) ? sanitize_text_field($_POST['secretaria']) : '',
|
||||
'setor' => isset($_POST['setor']) ? sanitize_text_field($_POST['setor']) : ''
|
||||
];
|
||||
|
||||
$data = $this->database->get_filtered_ramais($filters, $page);
|
||||
$ramais = $data['data'];
|
||||
$total_pages = $data['pages'];
|
||||
$current_page = $data['current_page'];
|
||||
|
||||
// Indica que é apenas a seção de resultados
|
||||
$is_results_section = true;
|
||||
|
||||
ob_start();
|
||||
include RAMAIS_TELEFONICOS_PLUGIN_DIR . 'templates/lista-ramais.php';
|
||||
$html = ob_get_clean();
|
||||
|
||||
wp_send_json_success($html);
|
||||
}
|
||||
}
|
140
includes/class-ramais-database.php
Normal file
140
includes/class-ramais-database.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Ramais_Database {
|
||||
private $csv_file;
|
||||
private $columns = array('ramal', 'responsavel', 'secretaria', 'setor', 'email');
|
||||
|
||||
public function __construct() {
|
||||
$this->csv_file = RAMAIS_TELEFONICOS_CSV_FILE;
|
||||
$this->ensure_csv_exists();
|
||||
}
|
||||
|
||||
private function ensure_csv_exists() {
|
||||
if (!file_exists($this->csv_file)) {
|
||||
wp_mkdir_p(RAMAIS_TELEFONICOS_CSV_DIR);
|
||||
$this->write_csv_row($this->columns);
|
||||
}
|
||||
}
|
||||
|
||||
public function get_all_ramais() {
|
||||
if (!file_exists($this->csv_file)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$ramais = array();
|
||||
$handle = fopen($this->csv_file, 'r');
|
||||
|
||||
if ($handle !== false) {
|
||||
// Pula o cabeçalho
|
||||
fgetcsv($handle);
|
||||
|
||||
while (($data = fgetcsv($handle)) !== false) {
|
||||
if (count($data) === count($this->columns)) {
|
||||
$ramais[] = array_combine($this->columns, $data);
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
return $ramais;
|
||||
}
|
||||
|
||||
public function get_filtered_ramais($filters = array(), $page = 1, $per_page = RAMAIS_TELEFONICOS_PER_PAGE) {
|
||||
$all_ramais = $this->get_all_ramais();
|
||||
$filtered = array();
|
||||
|
||||
foreach ($all_ramais as $ramal) {
|
||||
$match = true;
|
||||
|
||||
if (!empty($filters['search'])) {
|
||||
$search = strtolower($filters['search']);
|
||||
$match = false;
|
||||
foreach ($ramal as $value) {
|
||||
if (strpos(strtolower($value), $search) !== false) {
|
||||
$match = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($match && !empty($filters['secretaria']) && $ramal['secretaria'] !== $filters['secretaria']) {
|
||||
$match = false;
|
||||
}
|
||||
|
||||
if ($match && !empty($filters['setor']) && $ramal['setor'] !== $filters['setor']) {
|
||||
$match = false;
|
||||
}
|
||||
|
||||
if ($match) {
|
||||
$filtered[] = $ramal;
|
||||
}
|
||||
}
|
||||
|
||||
$total = count($filtered);
|
||||
$total_pages = ceil($total / $per_page);
|
||||
$offset = ($page - 1) * $per_page;
|
||||
$paginated = array_slice($filtered, $offset, $per_page);
|
||||
|
||||
return array(
|
||||
'data' => $paginated,
|
||||
'total' => $total,
|
||||
'pages' => $total_pages,
|
||||
'current_page' => $page
|
||||
);
|
||||
}
|
||||
|
||||
public function add_ramal($data) {
|
||||
$sanitized = array();
|
||||
foreach ($this->columns as $col) {
|
||||
$sanitized[$col] = $this->sanitize_field($col, $data[$col] ?? '');
|
||||
}
|
||||
|
||||
if ($this->validate_ramal($sanitized)) {
|
||||
return $this->write_csv_row($sanitized);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function sanitize_field($field, $value) {
|
||||
switch ($field) {
|
||||
case 'email':
|
||||
return sanitize_email($value);
|
||||
case 'ramal':
|
||||
return preg_replace('/[^0-9]/', '', $value);
|
||||
default:
|
||||
return sanitize_text_field($value);
|
||||
}
|
||||
}
|
||||
|
||||
private function validate_ramal($data) {
|
||||
foreach ($data as $value) {
|
||||
if (empty($value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return is_email($data['email']);
|
||||
}
|
||||
|
||||
private function write_csv_row($data) {
|
||||
$handle = fopen($this->csv_file, 'a');
|
||||
if ($handle === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$success = fputcsv($handle, $data);
|
||||
fclose($handle);
|
||||
return (bool)$success;
|
||||
}
|
||||
|
||||
public function get_unique_values($field) {
|
||||
$ramais = $this->get_all_ramais();
|
||||
$values = array_column($ramais, $field);
|
||||
$unique = array_unique($values);
|
||||
sort($unique);
|
||||
return $unique;
|
||||
}
|
||||
}
|
39
includes/class-ramais-frontend.php
Normal file
39
includes/class-ramais-frontend.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Ramais_Frontend {
|
||||
private $database;
|
||||
|
||||
public function __construct($database) {
|
||||
$this->database = $database;
|
||||
$this->init_hooks();
|
||||
}
|
||||
|
||||
private function init_hooks() {
|
||||
add_shortcode('lista_ramais', array($this, 'render_ramais_list'));
|
||||
}
|
||||
|
||||
public function render_ramais_list($atts) {
|
||||
$atts = shortcode_atts(array(
|
||||
'per_page' => RAMAIS_TELEFONICOS_PER_PAGE
|
||||
), $atts);
|
||||
|
||||
$page = isset($_GET['ramais_page']) ? absint($_GET['ramais_page']) : 1;
|
||||
$filters = array(
|
||||
'search' => isset($_GET['ramais_search']) ? sanitize_text_field($_GET['ramais_search']) : '',
|
||||
'secretaria' => isset($_GET['ramais_secretaria']) ? sanitize_text_field($_GET['ramais_secretaria']) : '',
|
||||
'setor' => isset($_GET['ramais_setor']) ? sanitize_text_field($_GET['ramais_setor']) : ''
|
||||
);
|
||||
|
||||
$data = $this->database->get_filtered_ramais($filters, $page, $atts['per_page']);
|
||||
$ramais = $data['data'];
|
||||
$total_pages = $data['pages'];
|
||||
$current_page = $data['current_page'];
|
||||
|
||||
ob_start();
|
||||
include RAMAIS_TELEFONICOS_PLUGIN_DIR . 'templates/lista-ramais.php';
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user