Warninglists WIP

pull/1106/head
Iglocska 2016-04-21 22:58:49 +02:00
parent 681e8b5f72
commit f8005ac1aa
9 changed files with 424 additions and 2 deletions

View File

@ -1 +1 @@
{"major":2, "minor":4, "hotfix":37}
{"major":2, "minor":4, "hotfix":38}

View File

@ -0,0 +1,121 @@
<?php
App::uses('AppController', 'Controller');
class WarninglistsController extends AppController {
public $components = array('Session', 'RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
}
public $paginate = array(
'limit' => 60,
'maxLimit' => 9999, // LATER we will bump here on a problem once we have more than 9999 events <- no we won't, this is the max a user van view/page.
'contain' => array(
'WarninglistType'
),
'order' => array(
'Warninglist.id' => 'DESC'
),
);
public function index() {
$this->paginate['recursive'] = -1;
$warninglists = $this->paginate();
foreach ($warninglists as &$warninglist) {
$warninglist['Warninglist']['valid_attributes'] = array();
foreach ($warninglist['WarninglistType'] as $type) $warninglist['Warninglist']['valid_attributes'][] = $type['type'];
$warninglist['Warninglist']['valid_attributes'] = implode(', ', $warninglist['Warninglist']['valid_attributes']);
unset($warninglist['WarninglistType']);
}
$this->set('warninglists', $warninglists);
}
public function update() {
$result = $this->Warninglist->update();
$this->Log = ClassRegistry::init('Log');
$fails = 0;
$successes = 0;
if (!empty($result)) {
if (isset($result['success'])) {
foreach ($result['success'] as $id => &$success) {
if (isset($success['old'])) $change = $success['name'] . ': updated from v' . $success['old'] . ' to v' . $success['new'];
else $change = $success['name'] . ' v' . $success['new'] . ' installed';
$this->Log->create();
$this->Log->save(array(
'org' => $this->Auth->user('Organisation')['name'],
'model' => 'Warninglist',
'model_id' => $id,
'email' => $this->Auth->user('email'),
'action' => 'update',
'user_id' => $this->Auth->user('id'),
'title' => 'Warning list updated',
'change' => $change,
));
$successes++;
}
}
if (isset($result['fails'])) {
foreach ($result['fails'] as $id => &$fail) {
$this->Log->create();
$this->Log->save(array(
'org' => $this->Auth->user('Organisation')['name'],
'model' => 'Warninglist',
'model_id' => $id,
'email' => $this->Auth->user('email'),
'action' => 'update',
'user_id' => $this->Auth->user('id'),
'title' => 'Warning list failed to update',
'change' => $fail['name'] . ' could not be installed/updated. Error: ' . $fail['fail'],
));
$fails++;
}
}
} else {
$this->Log->create();
$this->Log->save(array(
'org' => $this->Auth->user('Organisation')['name'],
'model' => 'Warninglist',
'model_id' => 0,
'email' => $this->Auth->user('email'),
'action' => 'update',
'user_id' => $this->Auth->user('id'),
'title' => 'Warninglist update (nothing to update)',
'change' => 'Executed an update of the warning lists, but there was nothing to update.',
));
}
if ($successes == 0 && $fails == 0) $this->Session->setFlash('All warninglists are up to date already.');
else if ($successes == 0) $this->Session->setFlash('Could not update any of the warning lists');
else {
$message = 'Successfully updated ' . $successes . ' warninglists.';
if ($fails != 0) $message . ' However, could not update ' . $fails . ' warning list.';
$this->Session->setFlash($message);
}
$this->redirect(array('controller' => 'warninglists', 'action' => 'index'));
}
public function toggleEnable($id) {
$currentState = $this->Warninglist->find('first', array('conditions' => array('id' => $id), 'recursive' => -1));
if (empty($currentState)) return new CakeResponse(array('body'=> json_encode(array('saved' => false, 'errors' => 'Warninglist not found.')), 'status' => 200));
if ($currentState['Warninglist']['enabled']) {
$currentState['Warninglist']['enabled'] = false;
$message = 'disabled';
} else {
$currentState['Warninglist']['enabled'] = true;
$message = 'enabled';
}
if ($this->Warninglist->save($currentState)) {
return new CakeResponse(array('body'=> json_encode(array('saved' => true, 'success' => 'Warninglist ' . $message)), 'status' => 200));
} else {
return new CakeResponse(array('body'=> json_encode(array('saved' => false, 'errors' => 'Warninglist could not be enabled.')), 'status' => 200));
}
}
public function getToggleField($id) {
if (!$this->request->is('ajax')) throw new MethodNotAllowedException('This action is available via AJAX only.');
$this->layout = 'ajax';
$currentState = $this->Warninglist->find('first', array('conditions' => array('id' => $id), 'recursive' => -1, 'fields' => array('id', 'enabled')));
$this->set('item', $currentState);
$this->render('ajax/getToggleField');
}
}

View File

@ -49,7 +49,7 @@ class AppModel extends Model {
// major -> minor -> hotfix -> requires_logout
public $db_changes = array(
2 => array(
4 => array(18 => false, 19 => false, 20 => false, 25 => false, 27 => false, 32 => false, 33 => true)
4 => array(18 => false, 19 => false, 20 => false, 25 => false, 27 => false, 32 => false, 33 => true, 38 => true)
)
);
@ -325,6 +325,31 @@ class AppModel extends Model {
case '2.4.33':
$sqlArray[] = "ALTER TABLE `users` ADD `force_logout` tinyint(1) NOT NULL DEFAULT '0';";
break;
case '2.4.38':
$sqlArray[] = "CREATE TABLE IF NOT EXISTS `warninglists` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_bin NOT NULL,
`type` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT 'string',
`description` text COLLATE utf8_bin NOT NULL,
`version` int(11) NOT NULL DEFAULT '1',
`enabled` tinyint(1) NOT NULL DEFAULT '0',
`warninglist_entry_count` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$sqlArray[] = "CREATE TABLE IF NOT EXISTS `warninglist_entries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`warninglist_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$sqlArray[] = "CREATE TABLE IF NOT EXISTS `warninglist_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(255) COLLATE utf8_bin NOT NULL,
`warninglist_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
break;
break;
case 'fixNonEmptySharingGroupID':
$sqlArray[] = 'UPDATE `events` SET `sharing_group_id` = 0 WHERE `distribution` != 4';
$sqlArray[] = 'UPDATE `attributes` SET `sharing_group_id` = 0 WHERE `distribution` != 4';

96
app/Model/Warninglist.php Normal file
View File

@ -0,0 +1,96 @@
<?php
App::uses('AppModel', 'Model');
class Warninglist extends AppModel{
public $useTable = 'warninglists';
public $recursive = -1;
public $actsAs = array(
'Containable',
);
public $validate = array(
'name' => array(
'rule' => array('valueNotEmpty'),
),
'description' => array(
'rule' => array('valueNotEmpty'),
),
'version' => array(
'rule' => array('numeric'),
),
);
public $hasMany = array(
'WarninglistEntry' => array(
'dependent' => true
),
'WarninglistType' => array(
'dependent' => true
)
);
public function beforeValidate($options = array()) {
parent::beforeValidate();
return true;
}
public function checkValidTypeJSON($check) {
return true;
}
public function update() {
$directories = glob(APP . 'files' . DS . 'warninglists' . DS . 'lists' . DS . '*', GLOB_ONLYDIR);
$updated = array();
foreach ($directories as &$dir) {
$file = new File ($dir . DS . 'list.json');
$list = json_decode($file->read(), true);
$file->close();
if (!isset($list['version'])) $list['version'] = 1;
if (!isset($list['type'])) $list['type'] = 'string';
else if (is_array($list['type'])) $list['type'] = $list['type'][0];
$current = $this->find('first', array(
'conditions' => array('name' => $list['name']),
'recursive' => -1,
'fields' => array('*')
));
if (empty($current) || $list['version'] > $current['Warninglist']['version']) {
$result = $this->__updateList($list, $current);
if (is_numeric($result)) {
$updated['success'][$result] = array('name' => $list['name'], 'new' => $list['version']);
if (!empty($current)) $updated['success'][$result]['old'] = $current['Warninglist']['version'];
} else {
$updated['fails'][] = array('name' => $list['name'], 'fail' => json_encode($result));
}
}
}
return $updated;
}
private function __updateList($list, $current) {
$list['enabled'] = false;
$warninglist = array();
if (!empty($current)) {
if ($current['Warninglist']['enabled']) $list['enabled'] = true;
$this->deleteAll(array('Warninglist.id' => $current['Warninglist']['id']));
}
$fieldsToSave = array('name', 'version', 'description', 'type', 'enabled');
foreach ($fieldsToSave as $fieldToSave) $warninglist['Warninglist'][$fieldToSave] = $list[$fieldToSave];
$this->create();
if ($this->save($warninglist)) {
foreach ($list['list'] as $value) {
$this->WarninglistEntry->create();
$this->WarninglistEntry->save(array('WarninglistEntry' => array('value' => $value, 'warninglist_id' => $this->id)));
}
if (!empty($list['matching_attributes'])) {
foreach ($list['matching_attributes'] as $type) {
$this->WarninglistType->create();
$this->WarninglistType->save(array('WarninglistType' => array('type' => $type, 'warninglist_id' => $this->id)));
}
} else {
$this->WarninglistType->create();
$this->WarninglistType->save(array('WarninglistType' => array('type' => 'ALL', 'warninglist_id' => $this->id)));
}
return $this->id;
} else return $this->validationErrors;
}
}

View File

@ -0,0 +1,28 @@
<?php
App::uses('AppModel', 'Model');
class WarninglistEntry extends AppModel{
public $useTable = 'warninglist_entries';
public $recursive = -1;
public $actsAs = array(
'Containable',
);
public $validate = array(
'value' => array(
'rule' => array('valueNotEmpty'),
)
);
public $belongsTo = array(
'Warninglist' => array(
'className' => 'Warninglist',
'foreignKey' => 'warninglist_id',
'counterCache' => true
)
);
public function beforeValidate($options = array()) {
parent::beforeValidate();
return true;
}
}

View File

@ -0,0 +1,24 @@
<?php
App::uses('AppModel', 'Model');
class WarninglistType extends AppModel{
public $useTable = 'warninglist_types';
public $recursive = -1;
public $actsAs = array(
'Containable',
);
public $validate = array(
'type' => array(
'rule' => array('valueNotEmpty'),
)
);
public $belongsTo = array(
'Warninglist'
);
public function beforeValidate($options = array()) {
parent::beforeValidate();
return true;
}
}

View File

@ -0,0 +1,5 @@
<?php
echo $this->Form->create('Warninglist', array('id' => 'enable_form_' . $item['Warninglist']['id'], 'url' => '/warninglists/toggleEnable/' . $item['Warninglist']['id']));
echo $this->Form->input('enable', array('id' => 'enable_checkbox_' . $item['Warninglist']['id'], 'checked' => $item['Warninglist']['enabled'], 'label' => false, 'onclick' => 'toggleSetting(event, "warninglist_enable", "' . $item['Warninglist']['id'] . '")'));
echo $this->Form->end();
?>

View File

@ -0,0 +1,79 @@
<div class="taxonomies index">
<h2>Warninglists</h2>
<div class="pagination">
<ul>
<?php
$this->Paginator->options(array(
'update' => '.span12',
'evalScripts' => true,
'before' => '$(".progress").show()',
'complete' => '$(".progress").hide()',
));
echo $this->Paginator->prev('&laquo; ' . __('previous'), array('tag' => 'li', 'escape' => false), null, array('tag' => 'li', 'class' => 'prev disabled', 'escape' => false, 'disabledTag' => 'span'));
echo $this->Paginator->numbers(array('modulus' => 20, 'separator' => '', 'tag' => 'li', 'currentClass' => 'active', 'currentTag' => 'span'));
echo $this->Paginator->next(__('next') . ' &raquo;', array('tag' => 'li', 'escape' => false), null, array('tag' => 'li', 'class' => 'next disabled', 'escape' => false, 'disabledTag' => 'span'));
?>
</ul>
</div>
<table class="table table-striped table-hover table-condensed">
<tr>
<th><?php echo $this->Paginator->sort('id');?></th>
<th><?php echo $this->Paginator->sort('name');?></th>
<th><?php echo $this->Paginator->sort('version');?></th>
<th><?php echo $this->Paginator->sort('description');?></th>
<th><?php echo $this->Paginator->sort('type');?></th>
<th>Valid attributes</th>
<th><?php echo $this->Paginator->sort('warninglist_entry_count', 'Entries');?></th>
<th><?php echo $this->Paginator->sort('enabled');?></th>
<th class="actions"><?php echo __('Actions');?></th>
</tr><?php
foreach ($warninglists as $k => $item): ?>
<tr>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl."/warninglists/view/".h($item['Warninglist']['id']);?>'"><?php echo h($item['Warninglist']['id']); ?>&nbsp;</td>
<td ondblclick="document.location.href ='<?php echo $baseurl."/warninglists/view/".h($item['Warninglist']['id']);?>'"><?php echo h($item['Warninglist']['name']); ?>&nbsp;</td>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl."/warninglists/view/".h($item['Warninglist']['id']);?>'"><?php echo h($item['Warninglist']['version']); ?>&nbsp;</td>
<td ondblclick="document.location.href ='<?php echo $baseurl."/warninglists/view/".h($item['Warninglist']['id']);?>'"><?php echo h($item['Warninglist']['description']); ?>&nbsp;</td>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl."/warninglists/view/".h($item['Warninglist']['id']);?>'"><?php echo h($item['Warninglist']['type']); ?>&nbsp;</td>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl."/warninglists/view/".h($item['Warninglist']['id']);?>'"><?php echo h($item['Warninglist']['valid_attributes']); ?>&nbsp;</td>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl."/warninglists/view/".h($item['Warninglist']['id']);?>'"><?php echo h($item['Warninglist']['warninglist_entry_count']); ?>&nbsp;</td>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl."/warninglists/view/".h($item['Warninglist']['id']);?>'">
<div id="#checkbox_div_<?php echo h($item['Warninglist']['id']);?>">
<?php
echo $this->Form->create('Warninglist', array('id' => 'enable_form_' . $item['Warninglist']['id'], 'url' => '/warninglists/toggleEnable/' . $item['Warninglist']['id']));
echo $this->Form->input('enable', array('id' => 'enable_checkbox_' . $item['Warninglist']['id'], 'checked' => $item['Warninglist']['enabled'], 'label' => false, 'onclick' => 'toggleSetting(event, "warninglist_enable", "' . $item['Warninglist']['id'] . '")'));
echo $this->Form->end();
?>
</div>
</td>
<td class="short action-links">
<?php
if ($isSiteAdmin) {
}
?>
<a href='<?php echo $baseurl."/warninglists/view/". h($item['Warninglist']['id']);?>' class = "icon-list-alt" title = "View"></a>
</td>
</tr><?php
endforeach; ?>
</table>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
));
?>
</p>
<div class="pagination">
<ul>
<?php
echo $this->Paginator->prev('&laquo; ' . __('previous'), array('tag' => 'li', 'escape' => false), null, array('tag' => 'li', 'class' => 'prev disabled', 'escape' => false, 'disabledTag' => 'span'));
echo $this->Paginator->numbers(array('modulus' => 20, 'separator' => '', 'tag' => 'li', 'currentClass' => 'active', 'currentTag' => 'span'));
echo $this->Paginator->next(__('next') . ' &raquo;', array('tag' => 'li', 'escape' => false), null, array('tag' => 'li', 'class' => 'next disabled', 'escape' => false, 'disabledTag' => 'span'));
?>
</ul>
</div>
</div>
<?php
echo $this->element('side_menu', array('menuList' => 'taxonomies', 'menuItem' => 'index'));
?>

View File

@ -83,6 +83,50 @@ function submitDeletion(context_id, action, type, id) {
});
}
function toggleSetting(e, setting, id) {
e.preventDefault();
e.stopPropagation();
switch (setting) {
case 'warninglist_enable':
formID = '#enable_form_' + id;
checkboxDiv = '#checkbox_div_' + id;
break;
}
var formData = $(formID).serialize();
$.ajax({
beforeSend: function (XMLHttpRequest) {
$(".loading").show();
},
data: formData,
success:function (data, textStatus) {
var result = JSON.parse(data);
if (result.success) {
var setting = false;
$.get( "/warninglists/getToggleField/" + id, function(data) {
console.log($(checkboxDiv).html(data));
$(checkboxDiv).html(data);
console.log(data);
console.log(checkboxDiv);
console.log($(checkboxDiv));
});
}
handleGenericAjaxResponse(data);
},
complete:function() {
$(".loading").hide();
$("#confirmation_box").fadeOut();
$("#gray_out").fadeOut();
},
error:function() {
handleGenericAjaxResponse({'saved':false, 'errors':['Request failed due to an unexpected error.']});
},
type:"post",
cache: false,
url: $(formID).attr('action'),
});
}
function initiatePasswordReset(id) {
$.get( "/users/initiatePasswordReset/" + id, function(data) {
$("#confirmation_box").fadeIn();