Issues (1369)

1
<?php
2
3
/**
4
 * notes.php
5
 *
6
 * Changelog:
7
 *   2.0 copyright © 2009-2012 Gorlum for http://supernova.ws
8
 *     [!] Wrote from scratch
9
 */
10
11
use DBAL\db_mysql;
12
13
include('common.' . substr(strrchr(__FILE__, '.'), 1));
14
15
lng_include('notes');
16
17
$template = SnTemplate::gettemplate('notes', true);
18
19
$result = array();
20
if(($result_message = sys_get_param_str('MESSAGE')) && isset($lang[$result_message])) {
21
  $result[] = array('STATUS' => sys_get_param_int('STATUS'), 'MESSAGE' => $lang[$result_message]);
22
}
23
24
$note_id_edit = sys_get_param_id('note_id_edit');
25
if(sys_get_param('note_delete')) {
26
  try {
27
    $not = '';
28
    $query_where = '';
29
    switch(sys_get_param_str('note_delete_range')) {
30
      case 'all':
31
      break;
32
33
      case 'marked_not':
34
        $not = 'NOT';
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
35
      case 'marked':
36
        if(!is_array($notes_marked = sys_get_param('note'))) {
37
          throw new exception('note_err_none_selected', ERR_WARNING);
38
        }
39
40
        $notes_marked_filtered = array();
41
        foreach($notes_marked as $note_id => $note_select) {
42
          if($note_select == 'on' && $note_id = idval($note_id)) {
43
            $notes_marked_filtered[] = $note_id;
44
          }
45
        }
46
47
        if(empty($notes_marked_filtered)) {
48
          throw new exception('note_err_none_selected', ERR_WARNING);
49
        }
50
51
        $notes_marked_filtered = implode(',', $notes_marked_filtered);
52
        $query_where = "AND `id` {$not} IN ({$notes_marked_filtered})";
53
      break;
54
55
      default:
56
        throw new exception('note_warn_no_range', ERR_WARNING);
57
      break;
58
    }
59
60
    db_mysql::db_transaction_start();
61
    doquery("DELETE FROM {{notes}} WHERE `owner` = {$user['id']} {$query_where};");
62
    db_mysql::db_transaction_commit();
63
    throw new exception($note_id_edit ? 'note_err_none_changed' : 'note_err_none_added', ERR_NONE);
64
  } catch(exception $e) {
65
    $note_id_edit = 0;
66
    db_mysql::db_transaction_rollback();
67
    $result[] = array(
68
      'STATUS'  => in_array($e->getCode(), array(ERR_NONE, ERR_WARNING, ERR_ERROR)) ? $e->getCode() : ERR_ERROR,
69
      'MESSAGE' => $lang[$e->getMessage()],
70
    );
71
  }
72
} elseif(($note_title = sys_get_param_str('note_title')) || ($note_text = sys_get_param_str('note_text'))) {
73
  $note_title == SN::$db->db_escape($lang['note_new_title']) ? $note_title = '' : false;
74
  ($note_text = sys_get_param_str('note_text')) == SN::$db->db_escape($lang['note_new_text']) ? $note_text = '' : false;
75
76
  try {
77
    $note_galaxy = max(0, min(sys_get_param_id('note_galaxy'), SN::$config->game_maxGalaxy));
78
    $note_system = max(0, min(sys_get_param_id('note_system'), SN::$config->game_maxSystem));
79
    $note_planet = max(0, min(sys_get_param_id('note_planet'), SN::$config->game_maxPlanet + 1));
80
81
    if(!$note_text && !$note_title && !$note_galaxy && !$note_system && !$note_planet) {
82
      throw new exception('note_err_note_empty', ERR_WARNING);
83
    }
84
85
    $note_priority = min(sys_get_param_id('note_priority', 2), count($note_priority_classes) - 1);
86
    $note_planet_type = max(1, min(sys_get_param_id('note_planet_type', 1), count($lang['sys_planet_type'])));
87
    $note_sticky = intval(sys_get_param_id('note_sticky')) ? 1 : 0;
88
89
    db_mysql::db_transaction_start();
90
    if($note_id_edit) {
91
      $check_note_id = doquery("SELECT `id`, `owner` FROM {{notes}} WHERE `id` = {$note_id_edit} LIMIT 1 FOR UPDATE", true);
92
      if(!$check_note_id) {
93
        throw new exception('note_err_note_not_found', ERR_ERROR);
94
      }
95
    }
96
97
    if($note_id_edit) {
98
      if($check_note_id['owner'] != $user['id']) {
99
        throw new exception('note_err_owner_wrong', ERR_ERROR);
100
      }
101
102
      doquery("UPDATE {{notes}} SET `time` = " . SN_TIME_NOW . ", `priority` = {$note_priority}, `title` = '{$note_title}', `text` = '{$note_text}',
103
        `galaxy` = {$note_galaxy}, `system` = {$note_system}, `planet` = {$note_planet}, `planet_type` = {$note_planet_type}, `sticky` = {$note_sticky}
104
        WHERE `id` = {$note_id_edit} LIMIT 1;");
105
    } else {
106
      doquery("INSERT INTO {{notes}} SET `owner` = {$user['id']}, `time` = " . SN_TIME_NOW . ", `priority` = {$note_priority}, `title` = '{$note_title}', `text` = '{$note_text}',
107
        `galaxy` = {$note_galaxy}, `system` = {$note_system}, `planet` = {$note_planet}, `planet_type` = {$note_planet_type}, `sticky` = {$note_sticky};");
108
    }
109
110
    db_mysql::db_transaction_commit();
111
    sys_redirect('notes.php?STATUS=' . ERR_NONE . '&MESSAGE=' . ($note_id_edit ? 'note_err_none_changed' : 'note_err_none_added'));
112
//    throw new exception($note_id_edit ? 'note_err_none_changed' : 'note_err_none_added', ERR_NONE);
113
  } catch(exception $e) {
114
    $note_id_edit = 0;
115
    db_mysql::db_transaction_rollback();
116
    $result[] = array(
117
      'STATUS'  => in_array($e->getCode(), array(ERR_NONE, ERR_WARNING, ERR_ERROR)) ? $e->getCode() : ERR_ERROR,
118
      'MESSAGE' => $lang[$e->getMessage()],
119
    );
120
  }
121
}
122
123
if(!$note_id_edit) {
124
  \Note\Note::note_assign($template, array(
125
    'id' => 0,
126
    'time' => SN_TIME_NOW,
127
    'priority' => 2,
128
    'planet_type' => PT_PLANET,
129
    'title' => $lang['note_new_title'],
130
    'text' => $lang['note_new_text'],
131
  ));
132
}
133
134
$note_exist = false;
135
$notes_query = doquery("SELECT * FROM {{notes}} WHERE owner={$user['id']} ORDER BY priority DESC, galaxy ASC, system ASC, planet ASC, planet_type ASC, `time` DESC");
136
while($note_row = db_fetch($notes_query)) {
137
  \Note\Note::note_assign($template, $note_row);
138
  $note_exist = $note_exist || $note_row['id'] == $note_id_edit;
139
}
140
$note_id_edit = $note_exist ? $note_id_edit : 0;
141
142
foreach($note_priority_classes as $note_priority_id => $note_priority_class) {
143
  $template->assign_block_vars('note_priority', array(
144
    'ID' => $note_priority_id,
145
    'CLASS' => $note_priority_classes[$note_priority_id],
146
    'TEXT' => $lang['sys_notes_priorities'][$note_priority_id],
147
  ));
148
}
149
150
foreach($lang['sys_planet_type'] as $planet_type_id => $planet_type_string) {
151
  $template->assign_block_vars('planet_type', array(
152
    'ID' => $planet_type_id,
153
    'TEXT' => $planet_type_string,
154
  ));
155
}
156
157
foreach($result as $result_data) {
158
  $template->assign_block_vars('result', $result_data);
159
}
160
161
$template->assign_vars(array(
162
  'PAGE_HEADER' => $lang['note_page_header'],
163
  'NOTE_ID_EDIT' => $note_id_edit,
164
  'NOTE_FULL_RENDER' => true,
165
));
166
167
SnTemplate::display($template);
168