Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
feat(file): add comment field
Browse files Browse the repository at this point in the history
Some minor refactor done by btry and squashed in this commit

Signed-off-by: Oyabi <mousaka666@hotmail.fr>
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
Oyabi authored and DIOHz0r committed Feb 16, 2018
1 parent 436512e commit 3ffc4c4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 24 deletions.
6 changes: 5 additions & 1 deletion front/file.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
$flyveFile->check(-1, CREATE, $_POST);
$flyveFile->add($_POST);
Html::back();
} else if (isset($_POST['update'])) {
$flyveFile->check(-1, UPDATE, $_POST);
$flyveFile->update($_POST);
Html::back();
} else {
$flyveFile->check($_GET['id'], READ);
Html::header(
Expand All @@ -67,4 +71,4 @@
} else {
Html::footer();
}
}
}
30 changes: 20 additions & 10 deletions inc/file.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,12 @@ public function prepareInputForAdd($input) {
*/
public function prepareInputForUpdate($input) {
// Find the added file
if (isset($_POST['_file'][0]) && is_string($_POST['_file'][0])) {
if (!isAPI()) {
// from GLPI UI
$actualFilename = $_POST['_file'][0];
$uploadedFile = GLPI_TMP_DIR . "/" . $_POST['_file'][0];
if (isset($_POST['_file'][0]) && is_string($_POST['_file'][0])) {
$actualFilename = $_POST['_file'][0];
$uploadedFile = GLPI_TMP_DIR . "/" . $_POST['_file'][0];
}
} else {
// from API
if (isset($_FILES['file']['error'])) {
Expand Down Expand Up @@ -212,14 +214,13 @@ public function prepareInputForUpdate($input) {
if ($filename != $this->fields['source']) {
unlink(FLYVEMDM_FILE_PATH . "/" . $this->fields['source']);
}
// File updated, then increment its version
$input['version'] = $this->fields['version'] + 1;
} else {
// No file uploaded
unset($input['source']);
}

// File updated, then increment its version
$input['version'] = $this->fields['version']++;


return $input;
}

Expand Down Expand Up @@ -280,9 +281,9 @@ protected function createEntityDirectory($dir) {
public function getSearchOptionsNew() {
$tab = parent::getSearchOptionsNew();

$tab[0] = [
'id' => 'common',
'name' => __s('File', 'flyvemdm'),
$tab[] = [
'id' => 'common',
'name' => __s('File', 'flyvemdm'),
];

$tab[] = [
Expand All @@ -303,6 +304,14 @@ public function getSearchOptionsNew() {
'massiveaction' => false,
];

$tab[] = [
'id' => '4',
'table' => $this->getTable(),
'field' => 'comment',
'name' => __('Comment'),
'datetype' => 'text',
];

return $tab;
}

Expand Down Expand Up @@ -496,6 +505,7 @@ public function showForm($ID, array $options = []) {
'isNewID' => $this->isNewID($ID),
'file' => $fields,
'upload' => Html::file(['name' => 'file', 'display' => false]),
'comment' => $fields['comment'],
];
echo $twig->render('file.html', $data);

Expand Down
32 changes: 28 additions & 4 deletions install/installer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public function install() {
$this->createNotificationTargetInvitation();
$this->createJobs();
$this->createRootEntityConfig();
$this->createDisplayPreferences();

Config::setConfigurationValues('flyvemdm', ['version' => PLUGIN_FLYVEMDM_VERSION]);

Expand Down Expand Up @@ -1532,10 +1533,33 @@ protected function deleteRelations() {
}
}

protected function createDisplayPreferences() {
$displayPreference = new DisplayPreference();
$itemtype = PluginFlyvemdmFile::class;
$criteria = "`itemtype` = '$itemtype' AND `num` = '1' AND `users_id` = '0'";
if (count($displayPreference->find($criteria)) == 0) {
$displayPreference->add([
'itemtype' => PluginFlyvemdmFile::class,
'num' => '1',
'rank' => '1',
User::getForeignKeyField() => '0'
]);
}
$criteria = "`itemtype` = '$itemtype' AND `num` = '4' AND `users_id` = '0'";
if (count($displayPreference->find($criteria)) == 0) {
$displayPreference->add([
'itemtype' => PluginFlyvemdmFile::class,
'num' => '4',
'rank' => '2',
User::getForeignKeyField() => '0'
]);
}
}

protected function deleteDisplayPreferences() {
// To cleanup display preferences if any
//$displayPreference = new DisplayPreference();
//$displayPreference->deleteByCriteria(["`num` >= " . PluginFlyvemdmConfig::RESERVED_TYPE_RANGE_MIN . "
// AND `num` <= " . PluginFlyvemdmConfig::RESERVED_TYPE_RANGE_MAX]);
global $DB;

$table = DisplayPreference::getTable();
$DB->query("DELETE FROM `$table` WHERE `itemtype` LIKE 'PluginFlyvemdm%'");
}
}
2 changes: 1 addition & 1 deletion install/mysql/plugin_flyvemdm_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_flyvemdm_files` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
`source` varchar(255) NOT NULL DEFAULT '',
`comment` text DEFAULT NULL,
`entities_id` int(11) NOT NULL DEFAULT '0',
`version` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
Expand Down Expand Up @@ -258,4 +259,3 @@ CREATE TABLE `glpi_plugin_flyvemdm_taskstatuses` (
INDEX `plugin_flyvemdm_agents_id` (`plugin_flyvemdm_agents_id`),
INDEX `plugin_flyvemdm_tasks_id` (`plugin_flyvemdm_tasks_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

13 changes: 13 additions & 0 deletions install/upgrade/update_to_dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,19 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) {

$table = 'glpi_plugin_flyvemdm_files';
$migration->addKey($table, 'entities_id', 'entities_id');
$migration->addField($table, 'comment', 'text');

// Add display preferences for PluginFlyvemdmFile
$query = "SELECT * FROM `glpi_displaypreferences`
WHERE `itemtype` = 'PluginFlyvemdmFile'
AND `users_id`='0'";
$result=$DB->query($query);
if ($DB->numrows($result) == '0') {
$query = "INSERT INTO `glpi_displaypreferences` (`id`, `itemtype`, `num`, `rank`, `users_id`)
VALUES (NULL, 'PluginFlyvemdmFile', '1', '1', '0'),
(NULL, 'PluginFlyvemdmFile', '4', '2', '0');";
$DB->query($query);
}

$table = 'glpi_plugin_flyvemdm_fleets';
$migration->addField($table, 'is_recursive', 'bool');
Expand Down
17 changes: 9 additions & 8 deletions tpl/file.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<tr class='tab_bg_1'>
<td>
{{ __('Name') }}{{ withTemplate }}
{{ __('Name') }}{{ withTemplate }}
</td>
<td>
<input type="text" name="name" value="{{ file.name }}">
</td>
<td></td>
<td></td>
<td>{{ __('Version') }}</td>
<td>{{ file.version }}</td>
</tr>
<tr class='tab_bg_1'>
<td>
{{ __('Version') }}
{{ __('file size', 'flyvemdm') }}
</td>
<td>
{{ file.version }}
{{ file.filesize }}
</td>
<td>
{{ __('file size', 'flyvemdm') }}
{{ __('Comment') }}{{ withTemplate }}
</td>
<td>
{{ file.filesize }}
<td><textarea cols="45" rows="5"
name="comment" aria-label="{{ __('Comment') }}{{ withTemplate }}"
>{{ comment }}</textarea>
</td>
</tr>
{% if canUpdate == true %}
Expand Down

0 comments on commit 3ffc4c4

Please sign in to comment.