Test Failed
Push — master ( 247090...f61425 )
by SuperNova.WS
18:56 queued 11:23
created
Labels
Severity
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
include('common.' . substr(strrchr(__FILE__, '.'), 1));
12
13
lng_include('notes');
14
15
$template = SnTemplate::gettemplate('notes', true);
0 ignored issues
show
true of type true is incompatible with the type null|template expected by parameter $template of SnTemplate::gettemplate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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