Add files from 1.11.x

pull/3124/head
Julio Montoya 6 years ago
parent aa4c592bfd
commit 5b445b7a5c
  1. 192
      public/main/auth/justification.php
  2. 23
      public/main/inc/ajax/career.ajax.php
  3. 281
      public/main/inc/lib/NotificationEvent.php
  4. 42
      public/main/notification_event/add.php
  5. 54
      public/main/notification_event/edit.php
  6. 50
      public/main/notification_event/list.php

@ -0,0 +1,192 @@
<?php
/* For licensing terms, see /license.txt */
$cidReset = true;
require_once __DIR__.'/../inc/global.inc.php';
api_block_anonymous_users(true);
$allowJustification = api_get_plugin_setting('justification', 'tool_enable') === 'true';
if (!$allowJustification) {
api_not_allowed(true);
}
$user_data = api_get_user_info(api_get_user_id());
$justification = '';
$plugin = Justification::create();
$fields = $plugin->getList();
$formValidator = new FormValidator('justification');
$formValidator->addHeader($plugin->get_lang('Justification'));
foreach ($fields as $field) {
$formValidator->addHtml('<a name="'.$field['code'].'"></a>');
$formValidator->addFile($field['code'].'_file', [$field['name'], $field['comment']]);
if ($field['date_manual_on']) {
$formValidator->addDatePicker($field['code'].'_date', $plugin->get_lang('DateValidity'));
}
$formValidator->addHtml('<hr>');
}
$formValidator->addButtonSend(get_lang('Send'));
if ($formValidator->validate() && isset($_FILES)) {
foreach ($fields as $field) {
$fieldId = $field['id'];
$days = $field['validity_duration'];
if (isset($_FILES[$field['code'].'_file']) && !empty($_FILES[$field['code'].'_file']['tmp_name'])) {
$file = $_FILES[$field['code'].'_file'];
} else {
continue;
}
$date = isset($_REQUEST[$field['code'].'_date']) ? $_REQUEST[$field['code'].'_date'].' 13:00:00' : api_get_local_time();
$startDate = api_get_utc_datetime($date, false, true);
$interval = new \DateInterval('P'.$days.'D');
$startDate->add($interval);
$finalDate = $startDate->format('Y-m-d');
$file['name'] = api_replace_dangerous_char($file['name']);
$fileName = $file['name'];
$params = [
'file_path' => $fileName,
'user_id' => api_get_user_id(),
'date_validity' => $finalDate,
'justification_document_id' => $fieldId,
];
$id = Database::insert('justification_document_rel_users', $params);
if ($id) {
api_upload_file('justification', $file, $id);
Display::addFlash(Display::return_message($plugin->get_lang('JustificationSaved')));
}
}
header('Location: '.api_get_self());
exit;
}
$userJustifications = $plugin->getUserJustificationList(api_get_user_id());
$userJustificationList = '';
$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : '';
$justificationContent = '';
switch ($action) {
case 'edit_justification':
$justificationId = isset($_REQUEST['justification_id']) ? (int) $_REQUEST['justification_id'] : '';
$userJustification = $plugin->getUserJustification($justificationId);
$justification = $plugin->getJustification($userJustification['justification_document_id']);
if ($justification['date_manual_on'] == 0) {
api_not_allowed(true);
}
$formEdit = new FormValidator('edit', 'post', api_get_self().'?a=edit_justification&justification_id='.$justificationId);
$formEdit->addHeader($justification['name']);
$element = $formEdit->addDatePicker('date_validity', $plugin->get_lang('ValidityDate'));
$element->setValue($userJustification['date_validity']);
$formEdit->addButtonUpdate(get_lang('Update'));
$formEdit->setDefaults($userJustification);
$justificationContent = $formEdit->returnForm();
if ($formEdit->validate()) {
$values = $formEdit->getSubmitValues();
$date = Database::escape_string($values['date_validity']);
$sql = "UPDATE justification_document_rel_users SET date_validity = '$date'
WHERE id = $justificationId AND user_id = ".$user_data['id'];
Database::query($sql);
Display::addFlash(Display::return_message(get_lang('Updated')));
header('Location: '.api_get_self());
exit;
}
break;
case 'delete_justification':
$justificationId = isset($_REQUEST['justification_id']) ? (int) $_REQUEST['justification_id'] : '';
$userJustification = $plugin->getUserJustification($justificationId);
if ($userJustification && $userJustification['user_id'] == api_get_user_id()) {
api_remove_uploaded_file_by_id('justification', $justificationId, $userJustification['file_path']);
$sql = "DELETE FROM justification_document_rel_users
WHERE id = $justificationId AND user_id = ".$user_data['id'];
Database::query($sql);
Display::addFlash(Display::return_message(get_lang('Deleted')));
}
header('Location: '.api_get_self());
exit;
break;
}
if (!empty($userJustifications)) {
$userJustificationList .= Display::page_subheader3($plugin->get_lang('MyJustifications'));
$table = new HTML_Table(['class' => 'data_table']);
$column = 0;
$row = 0;
$headers = [
get_lang('Name'),
get_lang('File'),
$plugin->get_lang('ValidityDate'),
get_lang('Actions'),
];
foreach ($headers as $header) {
$table->setHeaderContents($row, $column, $header);
$column++;
}
$row = 1;
foreach ($userJustifications as $userJustification) {
$justification = $plugin->getJustification($userJustification['justification_document_id']);
$url = api_get_uploaded_web_url('justification', $userJustification['id'], $userJustification['file_path']);
$link = Display::url($userJustification['file_path'], $url);
$col = 0;
$table->setCellContents($row, $col++, $justification['name']);
$table->setCellContents($row, $col++, $link);
$date = $userJustification['date_validity'];
if ($userJustification['date_validity'] < api_get_local_time()) {
$date = Display::label($userJustification['date_validity'], 'warning');
}
$table->setCellContents($row, $col++, $date);
$actions = '';
if ($justification['date_manual_on'] == 1) {
$actions .= Display::url(get_lang('Edit'), api_get_self().'?a=edit_justification&justification_id='.$userJustification['id'], ['class' => 'btn btn-primary']);
}
$actions .= '&nbsp;'.Display::url(get_lang('Delete'), api_get_self().'?a=delete_justification&justification_id='.$userJustification['id'], ['class' => 'btn btn-danger']);
$table->setCellContents($row, $col++, $actions);
$row++;
}
$userJustificationList .= $justificationContent.$table->toHtml();
}
$justificationTab = '';
$headers = [
[
'url' => api_get_path(WEB_CODE_PATH).'auth/profile.php',
'content' => get_lang('Profile'),
],
[
'url' => api_get_path(WEB_CODE_PATH).'auth/justification.php',
'content' => $plugin->get_lang('Justification'),
],
];
$justificationTab = Display::tabsOnlyLink($headers, 2);
$justification = $justificationTab.$formValidator->returnForm().$userJustificationList;
$tpl = new Template(get_lang('ModifyProfile'));
SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'home');
$menu = SocialManager::show_social_menu(
'home',
null,
api_get_user_id(),
false,
false
);
$tpl->assign('social_menu_block', $menu);
$tpl->assign('social_right_content', $justification);
$social_layout = $tpl->get_template('social/edit_profile.tpl');
$tpl->display($social_layout);

@ -0,0 +1,23 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Responses to AJAX calls.
*/
require_once __DIR__.'/../global.inc.php';
api_protect_admin_script();
$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
switch ($action) {
case 'get_promotions':
$careerId = isset($_REQUEST['career_id']) ? (int) $_REQUEST['career_id'] : 0;
$career = new Promotion();
$promotions = $career->get_all_promotions_by_career_id($careerId);
echo json_encode($promotions);
break;
}

@ -0,0 +1,281 @@
<?php
/* For licensing terms, see /license.txt */
class NotificationEvent extends Model
{
public $table;
public $columns = [
'id',
'title',
'content',
'link',
'persistent',
'day_diff',
'event_type',
'event_id',
];
const ACCOUNT_EXPIRATION = 1;
const JUSTIFICATION_EXPIRATION = 2;
public $extraFieldName;
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
$this->table = 'notification_event';
$this->extraFieldName = 'notification_event';
}
public function eventTypeToString($eventTypeId)
{
$list = $this->getEventsForSelect();
return $list[$eventTypeId];
}
public function getEventsForSelect()
{
return [
self::ACCOUNT_EXPIRATION => get_lang('AccountExpiration'),
self::JUSTIFICATION_EXPIRATION => get_lang('JustificationExpiration'),
];
}
public function getForm(FormValidator $form, $data = [])
{
$options = $this->getEventsForSelect();
$form->addSelect('event_type', get_lang('EventType'), $options);
$form->freeze('event_type');
$eventType = $data['event_type'];
switch ($eventType) {
case self::JUSTIFICATION_EXPIRATION:
$plugin = Justification::create();
$list = $plugin->getList();
$list = array_column($list, 'name', 'id');
$form->addSelect('event_id', get_lang('JustificationType'), $list);
break;
default:
break;
}
$form->freeze('event_id');
$form->addText('title', get_lang('Title'));
$form->addTextarea('content', get_lang('Content'));
$form->addText('link', get_lang('Link'), false);
$form->addCheckBox('persistent', get_lang('Persistent'));
$form->addNumeric('day_diff', get_lang('DayDiff'), false);
return $form;
}
public function getAddForm(FormValidator $form)
{
$options = $this->getEventsForSelect();
$eventType = $form->getSubmitValue('event_type');
$form->addSelect(
'event_type',
get_lang('EventType'),
$options,
['placeholder' => get_lang('SelectAnOption'), 'onchange' => 'document.add.submit()']
);
if (!empty($eventType)) {
$form->freeze('event_type');
$form->addText('title', get_lang('Title'));
$form->addTextarea('content', get_lang('Content'));
$form->addText('link', get_lang('Link'), false);
$form->addCheckBox('persistent', get_lang('Persistent'));
$form->addNumeric('day_diff', get_lang('DayDiff'), false);
switch ($eventType) {
case self::JUSTIFICATION_EXPIRATION:
$plugin = Justification::create();
$list = $plugin->getList();
$list = array_column($list, 'name', 'id');
$form->addSelect('event_id', get_lang('JustificationType'), $list);
break;
default:
break;
}
$form->addButtonSave(get_lang('Save'));
}
return $form;
}
public function getUserExtraData($userId)
{
$data = UserManager::get_extra_user_data_by_field($userId, $this->extraFieldName);
return isset($data['notification_event']) ? $data['notification_event'] : '';
}
public function getNotificationsByUser($userId)
{
$userInfo = api_get_user_info($userId);
$events = $this->get_all();
$extraFieldData = $this->getUserExtraData(api_get_user_id());
$allowJustification = api_get_plugin_setting('justification', 'tool_enable') === 'true';
$userJustificationList = [];
if ($allowJustification) {
$plugin = Justification::create();
$userJustificationList = $plugin->getUserJustificationList($userId);
}
$notifications = [];
foreach ($events as $event) {
$days = (int) $event['day_diff'];
$checkIsRead = $event['persistent'] == 0 ? true : false;
$eventItemId = $event['event_id'];
switch ($event['event_type']) {
case self::ACCOUNT_EXPIRATION:
if (empty($userInfo['expiration_date'])) {
break;
}
$id = 'id_'.self::ACCOUNT_EXPIRATION.'_event_'.$event['id'].'_'.$userInfo['id'];
$read = false;
if ($checkIsRead) {
$read = $this->isRead($id, $extraFieldData);
}
$showNotification = $this->showNotification($userInfo['expiration_date'], $days);
if ($showNotification && $read === false) {
$notifications[] = [
'id' => $id,
'title' => $event['title'],
'content' => $event['content'],
'event_text' => get_lang('ExpirationDate').': '.api_get_local_time($userInfo['expiration_date']),
'link' => $event['link'],
'persistent' => $event['persistent']
];
}
break;
case self::JUSTIFICATION_EXPIRATION:
if (!empty($userJustificationList)) {
foreach ($userJustificationList as $userJustification) {
if (empty($userJustification['date_validity'])) {
continue;
}
if ($eventItemId != $userJustification['justification_document_id']) {
continue;
}
$showNotification = $this->showNotification($userJustification['date_validity'], $days);
$id = 'id_'.self::JUSTIFICATION_EXPIRATION.'_event_'.$event['id'].'_'.$userJustification['id'];
$fieldData = $plugin->getJustification($userJustification['justification_document_id']);
$read = false;
if ($checkIsRead) {
$read = $this->isRead($id, $extraFieldData);
}
$eventText = $plugin->get_lang('Justification').': '.$fieldData['name'].' <br />';
$eventText .= $plugin->get_lang('JustificationDate').': '.$userJustification['date_validity'];
$url = $event['link'];
if (empty($url)) {
$url = api_get_path(WEB_CODE_PATH).'auth/justification.php#'.$fieldData['code'];
}
if ($showNotification && $read === false) {
$notifications[] = [
'id' => $id,
'title' => $event['title'],
'content' => $event['content'],
'event_text' => $eventText,
'link' => $url,
'persistent' => $event['persistent']
];
}
}
}
break;
}
}
return $notifications;
}
public function isRead($id, $extraData)
{
$userId = api_get_user_id();
if (empty($extraData)) {
return false;
}
$data = $this->getUserExtraData($userId);
if (empty($data)) {
return false;
}
$data = json_decode($data);
if (in_array($id, $data)) {
return true;
}
return false;
}
public function markAsRead($id)
{
if (empty($id)) {
return false;
}
$userId = api_get_user_id();
$data = $this->getUserExtraData($userId);
if (!empty($data)) {
$data = json_decode($data);
} else {
$data = [];
}
$data[] = $id;
$data = json_encode($data);
UserManager::update_extra_field_value($userId, $this->extraFieldName, $data);
return true;
}
public function showNotification($date, $dayDiff)
{
$today = api_get_utc_datetime();
$expiration = api_get_utc_datetime($date, false, true);
$interval = new DateInterval('P'.$dayDiff.'D');
$diff = $expiration->sub($interval);
if ($diff->format('Y-m-d H:i:s') < $today) {
return true;
}
return false;
}
public function install()
{
$sql = "CREATE TABLE IF NOT EXISTS notification_event (
id INT unsigned NOT NULL auto_increment PRIMARY KEY,
title VARCHAR(255),
content TEXT,
link TEXT,
persistent INT,
day_diff INT,
event_type VARCHAR(255)
)";
Database::query($sql);
}
}

@ -0,0 +1,42 @@
<?php
/* For license terms, see /license.txt */
require_once __DIR__.'/../../main/inc/global.inc.php';
api_protect_admin_script();
$tool = 'notification_event';
$tpl = new Template($tool);
$fields = [];
$manager = new NotificationEvent();
$form = new FormValidator('add');
$form = $manager->getAddForm($form);
if (isset($_POST) && isset($_POST['title']) && $form->validate()) {
$values = $form->getSubmitValues();
$manager->save($values);
Display::addFlash(Display::return_message(get_lang('Saved')));
$url = api_get_path(WEB_CODE_PATH).'notification_event/list.php?';
header('Location: '.$url);
exit;
}
$actionLinks = Display::toolbarButton(
get_lang('Back'),
api_get_path(WEB_CODE_PATH).'notification_event/list.php',
'arrow-left',
'primary'
);
$tpl->assign(
'actions',
Display::toolbarAction('toolbar', [$actionLinks])
);
$content = $form->returnForm();
$tpl->assign('content', $content);
$tpl->display_one_col_template();

@ -0,0 +1,54 @@
<?php
/* For license terms, see /license.txt */
require_once __DIR__.'/../../main/inc/global.inc.php';
api_protect_admin_script();
$tool = 'notification_event';
$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
if (empty($id)) {
api_not_allowed();
}
$manager = new NotificationEvent();
$notification = $manager->get($id);
$tpl = new Template($tool);
$fields = [];
$form = new FormValidator('edit', 'post', api_get_self().'?id='.$id);
$form = $manager->getForm($form, $notification);
$form->setDefaults($notification);
$form->addButtonSave(get_lang('Update'));
if ($form->validate()) {
$values = $form->getSubmitValues();
$values['id'] = $id;
$values['persistent'] = isset($values['persistent']) ? 1 : 0;
$manager->update($values);
Display::addFlash(get_lang('Updated'));
$url = api_get_path(WEB_CODE_PATH).'notification_event/list.php?';
header('Location: '.$url);
exit;
}
$actionLinks = Display::toolbarButton(
get_lang('Back'),
api_get_path(WEB_CODE_PATH).'notification_event/list.php',
'arrow-left',
'primary'
);
$tpl->assign(
'actions',
Display::toolbarAction('toolbar', [$actionLinks])
);
$content = $form->returnForm();
$tpl->assign('content', $content);
$tpl->display_one_col_template();

@ -0,0 +1,50 @@
<?php
/* For license terms, see /license.txt */
require_once __DIR__.'/../../main/inc/global.inc.php';
api_protect_admin_script();
$tool = 'notification_event';
$tpl = new Template($tool);
$fields = [];
$manager = new NotificationEvent();
$list = $manager->get_all();
foreach ($list as &$item) {
$item['event_type'] = $manager->eventTypeToString($item['event_type']);
}
$tpl->assign('list', $list);
$content = $tpl->fetch($tpl->get_template('notification_event/list.tpl'));
$actionLinks = '';
$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : '';
$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
switch ($action) {
case 'delete':
$manager->delete($id);
Display::addFlash(Display::return_message(get_lang('Deleted')));
header('Location: '.api_get_self());
exit;
break;
}
$actionLinks .= Display::toolbarButton(
get_lang('Add'),
api_get_path(WEB_CODE_PATH).'notification_event/add.php',
'plus',
'primary'
);
$tpl->assign(
'actions',
Display::toolbarAction('toolbar', [$actionLinks])
);
$tpl->assign('content', $content);
$tpl->display_one_col_template();
Loading…
Cancel
Save