|
24 | 24 |
|
25 | 25 |
|
26 | 26 | # validate csrf cookie |
27 | | -$User->Crypto->csrf_cookie ("validate", "logicalCircuit", $_POST['csrf_cookie']) === false ? $Result->show("danger", _("Invalid CSRF cookie"), true) : ""; |
| 27 | +$User->Crypto->csrf_cookie ("validate", "circuitsLogical", $_POST['csrf_cookie']) === false ? $Result->show("danger", _("Invalid CSRF cookie"), true) : ""; |
28 | 28 | # validate action |
29 | 29 | $Admin->validate_action ($_POST['action'], true); |
30 | 30 | # get modified details |
|
37 | 37 | if($circuit['logical_cid'] == "") { $Result->show("danger", _('Logical Circuit ID is mandatory').'!', true); } |
38 | 38 |
|
39 | 39 | # Validate to make sure there aren't duplicates of the same circuit in the list of circuit ids |
40 | | -#Create list of member circuit IDs for mapping |
41 | | -$id_list = explode("." , rtrim($_POST['circuit_list'],".")); |
| 40 | +# Create list of member circuit IDs for mapping |
| 41 | +$_POST['circuit_list'] = str_replace("undefined.", "", $_POST['circuit_list']); |
| 42 | +$id_list = $_POST['circuit_list']!=="" ? explode("." , rtrim($_POST['circuit_list'],".")) : []; |
42 | 43 | if(sizeof($id_list ) != sizeof(array_unique($id_list))){ $Result->show("danger", _('Remove duplicates of circuit').'!', true); } |
43 | 44 |
|
44 | | - |
45 | | - |
46 | 45 | # fetch custom fields |
47 | | -$custom = $Tools->fetch_custom_fields('logicalCircuit'); |
| 46 | +$custom = $Tools->fetch_custom_fields('circuitsLogical'); |
48 | 47 | if(sizeof($custom) > 0) { |
49 | 48 | foreach($custom as $myField) { |
50 | | - |
51 | 49 | //replace possible ___ back to spaces |
52 | 50 | $myField['nameTest'] = str_replace(" ", "___", $myField['name']); |
53 | 51 | if(isset($circuit[$myField['nameTest']])) { $circuit[$myField['name']] = $circuit[$myField['nameTest']];} |
54 | | - |
55 | 52 | //booleans can be only 0 and 1! |
56 | 53 | if($myField['type']=="tinyint(1)") { |
57 | 54 | if($circuit[$myField['name']]>1) { |
|
60 | 57 | } |
61 | 58 | //not null! |
62 | 59 | if($myField['Null']=="NO" && strlen($circuit[$myField['name']])==0) { $Result->show("danger", $myField['name'].'" can not be empty!', true); } |
63 | | - |
64 | 60 | # save to update array |
65 | 61 | $update[$myField['name']] = $circuit[$myField['nameTest']]; |
66 | 62 | } |
67 | 63 | } |
68 | 64 |
|
69 | 65 | # set update values |
70 | 66 | $values = array( |
71 | | - "id" => $circuit['id'], |
72 | | - "logical_cid" => $circuit['logical_cid'], |
73 | | - "purpose" => $circuit['purpose'], |
74 | | - "comments" => $circuit['comments'], |
| 67 | + "id" => $circuit['id'], |
| 68 | + "logical_cid" => $circuit['logical_cid'], |
| 69 | + "purpose" => $circuit['purpose'], |
| 70 | + "comments" => $circuit['comments'], |
75 | 71 | "member_count" => sizeof($id_list) |
76 | 72 | ); |
77 | 73 |
|
|
80 | 76 | $values = array_merge($values, $update); |
81 | 77 | } |
82 | 78 |
|
83 | | -# update device |
84 | | -if(!$Admin->object_modify("logicalCircuit", $circuit['action'], "id", $values)) {} |
85 | | - |
86 | | -//If this is a new circuit, locate the ID (last_insert_id() would probably be better suited for this) |
87 | | -if($circuit['id'] == ""){ |
88 | | - $query[] = "select"; |
89 | | - $query[] = "id"; |
90 | | - $query[] = "from logicalCircuit"; |
91 | | - if($circuit['id'] == "") |
92 | | - $query[] = "where logical_cid = '".$_POST['logical_cid']."';"; |
93 | | - |
94 | | - //error_log(implode("\n", $query)); |
95 | | - try{ $db_circuit = $Database->getObjectsQuery(implode("\n", $query), array()); } |
96 | | - catch (Exception $e){ |
97 | | - $Result->show("danger", $e->getMessage(), true); |
| 79 | +# update circuit |
| 80 | +if(!$Admin->object_modify("circuitsLogical", $circuit['action'], "id", $values)) {} |
| 81 | +else { |
| 82 | + $Result->show("success", _("Circuit $circuit[action] success!")); |
| 83 | + |
| 84 | + // If this is a new circuit, save id of insert and process |
| 85 | + if($circuit['id'] == "") { |
| 86 | + if ($Admin->lastId==null) { |
| 87 | + $Result->show("danger", _('Logical circuit added, but failed to create mapping').'!', true); |
| 88 | + } |
| 89 | + else { |
| 90 | + $circuit['id'] = $Admin->lastId; |
| 91 | + } |
98 | 92 | } |
99 | | - //Grab the first row circuit ID |
100 | | - $circuit['id'] = $db_circuit[0]->id; |
101 | | -} |
102 | 93 |
|
103 | | -if($circuit['id'] == ""){ |
104 | | - $Result->show("danger", _('Logical circuit added, but failed to create mapping').'!', true); |
105 | | -}else{ |
106 | | - $drop_query = "DELETE FROM `logicalCircuitMapping` where `logicalCircuit_id` = ".$circuit['id'].";"; |
107 | | - try { $Database->runQuery($drop_query); } |
108 | | - catch (Exception $e) { |
109 | | - $Result->show("danger", _("Error dropping mapping: ").$e->getMessage()); |
| 94 | + // delete |
| 95 | + if($circuit['action'] != 'add') { |
| 96 | + try { $Database->deleteObjectsByIdentifier("circuitsLogicalMapping", "logicalCircuit_id", $circuit['id']); } |
| 97 | + catch (Exception $e) { |
| 98 | + $Result->show("danger", _("Error dropping mapping: ").$e->getMessage()); |
| 99 | + } |
110 | 100 | } |
111 | | - if($circuit['action'] != 'delete'){ |
112 | | - #Grab list of IDs and create list |
113 | | - $id_list = explode("." , rtrim($_POST['circuit_list'],".")); |
114 | | - $order = 0; |
115 | | - foreach($id_list as $member_id){ |
116 | | - $insert_query = "INSERT INTO logicalCircuitMapping (`logicalCircuit_id`,`circuit_id`,`order`) VALUES ('$circuit[id]','$member_id','$order')"; |
117 | | - try { $Database->runQuery($insert_query); } |
118 | | - catch (Exception $e) { |
119 | | - $Result->show("danger", _("Error inserting mapping: ").$e->getMessage()); |
| 101 | + |
| 102 | + // add mapping |
| 103 | + // Grab list of IDs and create list |
| 104 | + $order = 0; |
| 105 | + |
| 106 | + if(sizeof($id_list)>0) { |
| 107 | + foreach($id_list as $member_id) { |
| 108 | + // insert values |
| 109 | + $values = [ |
| 110 | + "logicalCircuit_id" => $circuit['id'], |
| 111 | + "circuit_id" => $member_id, |
| 112 | + "order" => $order |
| 113 | + ]; |
| 114 | + |
| 115 | + // insert to mapping |
| 116 | + if(!$Admin->object_modify("circuitsLogicalMapping", "add", "id", $values)) { |
| 117 | + $Result->show("danger", _("Error inserting mapping.")); |
120 | 118 | } |
121 | 119 | $order++; |
122 | 120 | } |
| 121 | + // all ok |
123 | 122 | $Result->show("success", _("Logical Circuit $circuit[action] successful").'!', false); |
124 | 123 | } |
| 124 | + else { |
| 125 | + $Result->show("warning", _("No Logical Circuits selected").'!', false); |
| 126 | + } |
125 | 127 | } |
0 commit comments