Completed
Push — work-fleets ( d28adc...7496c9 )
by SuperNova.WS
06:01
created

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Vector\Vector;
12
13
include('common.' . substr(strrchr(__FILE__, '.'), 1));
14
15
lng_include('notes');
16
17
$template = gettemplate('notes', true);
18
19
$result = array();
20
if(($result_message = sys_get_param_str('MESSAGE')) && isset(classLocale::$lang[$result_message])) {
21
  $result[] = array('STATUS' => sys_get_param_int('STATUS'), 'MESSAGE' => classLocale::$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';
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
    sn_db_transaction_start();
61
    DBStaticNote::db_note_list_delete($user, $query_where);
62
    sn_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
    sn_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' => classLocale::$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 == db_escape(classLocale::$lang['note_new_title']) ? $note_title = '' : false;
74
  ($note_text = sys_get_param_str('note_text')) == db_escape(classLocale::$lang['note_new_text']) ? $note_text = '' : false;
75
76
  try {
77
    $note_galaxy = max(0, min(sys_get_param_id('note_galaxy'), Vector::$knownGalaxies));
78
    $note_system = max(0, min(sys_get_param_id('note_system'), Vector::$knownSystems));
79
    $note_planet = max(0, min(sys_get_param_id('note_planet'), Vector::$knownPlanets + 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(classLocale::$lang['sys_planet_type'])));
87
    $note_sticky = intval(sys_get_param_id('note_sticky')) ? 1 : 0;
88
89
    sn_db_transaction_start();
90
    if($note_id_edit) {
91
      $check_note_id = DBStaticNote::db_note_get_id_and_owner($note_id_edit);
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
      DBStaticNote::db_note_update_by_id($note_priority, $note_title, $note_text, $note_galaxy, $note_system, $note_planet, $note_planet_type, $note_sticky, $note_id_edit);
103
    } else {
104
      DBStaticNote::db_note_insert($user, $note_priority, $note_title, $note_text, $note_galaxy, $note_system, $note_planet, $note_planet_type, $note_sticky);
105
    }
106
107
    sn_db_transaction_commit();
108
    sys_redirect('notes.php?STATUS=' . ERR_NONE . '&MESSAGE=' . ($note_id_edit ? 'note_err_none_changed' : 'note_err_none_added'));
109
//    throw new exception($note_id_edit ? 'note_err_none_changed' : 'note_err_none_added', ERR_NONE);
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110
  } catch(Exception $e) {
111
    $note_id_edit = 0;
112
    sn_db_transaction_rollback();
113
    $result[] = array(
114
      'STATUS'  => in_array($e->getCode(), array(ERR_NONE, ERR_WARNING, ERR_ERROR)) ? $e->getCode() : ERR_ERROR,
115
      'MESSAGE' => classLocale::$lang[$e->getMessage()],
116
    );
117
  }
118
}
119
120
if(!$note_id_edit) {
121
  note_assign($template, array(
122
    'id'          => 0,
123
    'time'        => SN_TIME_NOW,
124
    'priority'    => 2,
125
    'planet_type' => PT_PLANET,
126
    'title'       => classLocale::$lang['note_new_title'],
127
    'text'        => classLocale::$lang['note_new_text'],
128
  ));
129
}
130
131
$note_exist = false;
132
$notes_query = DBStaticNote::db_note_list_by_owner($user['id']);
133
while($note_row = db_fetch($notes_query)) {
134
  note_assign($template, $note_row);
135
  $note_exist = $note_exist || $note_row['id'] == $note_id_edit;
136
}
137
$note_id_edit = $note_exist ? $note_id_edit : 0;
138
139
foreach($note_priority_classes as $note_priority_id => $note_priority_class) {
140
  $template->assign_block_vars('note_priority', array(
141
    'ID'    => $note_priority_id,
142
    'CLASS' => $note_priority_classes[$note_priority_id],
143
    'TEXT'  => classLocale::$lang['sys_notes_priorities'][$note_priority_id],
144
  ));
145
}
146
147
foreach(classLocale::$lang['sys_planet_type'] as $planet_type_id => $planet_type_string) {
148
  $template->assign_block_vars('planet_type', array(
149
    'ID'   => $planet_type_id,
150
    'TEXT' => $planet_type_string,
151
  ));
152
}
153
154
foreach($result as $result_data) {
155
  $template->assign_block_vars('result', $result_data);
156
}
157
158
$template->assign_vars(array(
159
  'PAGE_HEADER'      => classLocale::$lang['note_page_header'],
160
  'NOTE_ID_EDIT'     => $note_id_edit,
161
  'NOTE_FULL_RENDER' => true,
162
));
163
164
display($template);
165