function setUserPrefs($userId, $prefs) { global $dbCon; $cols = array('color', 'font_color', 'font_size', 'font_type', 'width', 'height', 'bg_color', 'bg_img'); $cols_Vals = array(); foreach ($cols as $col) { if (isset($prefs[$col])) { $cols_Vals[] = $col . '=' . to_sql_type($prefs[$col]); } } $cols_Vals = implode(', ', $cols_Vals); try { $sql = "UPDATE user_prefs SET {$cols_Vals} WHERE(user_id={$userId})"; //echo ($sql); $rowCount = $dbCon->exec($sql); if ($rowCount > 0) { $reply = array('prefs_set' => true); } else { $reply = array('prefs_set' => false); } } catch (Exception $e) { $reply = array('errors' => $e->getMessage()); } replyJson($reply); }
/** * Mark notes as deleted in DB * @param int $userId * @param array $notesIds ; */ function delete_notes($userId, $notesIds) { global $dbCon; $deleted = array(); $errors = array(); foreach ($notesIds as $id) { $now = date('Y-m-d G:i:s'); $sql = "UPDATE notes SET state='deleted', date_deleted='{$now}' WHERE (id='{$id}' and user_id='{$userId}' and state!='deleted')"; //$sql = "UPDATE notes SET state='deleted', date_deleted='$now' WHERE (id=$id and state!='deleted')"; try { $rowCount = $dbCon->exec($sql); if ($rowCount > 0) { $deleted[] = $id; //note: may not be necesary; } else { $errors[] = 'Error in delete_notes() > noteId: ' . $id . ' doesn\'t exist or doesn\'t belong to user'; } } catch (Exception $e) { $errors[] = 'Error in delete_notes() with noteId: ' . $id . ' > ' . $e->getMessage(); } } replyJson(array('deleted' => $deleted, 'errors' => $errors)); }
break; case 'save_notes': save_notes($userId, $request['notas']); break; case 'delete_notes': delete_notes($userId, $request['ids']); break; case 'getUserPrefs': getUserPrefs($userId); break; case 'setUserPrefs': setUserPrefs($userId, $request['arg']); break; case 'logout': logoutUser(); break; default: $reply['errors'] = array('Invalid Action or no Action'); replyJson($reply); } } else { $reply['errors'] = array('Error decoding POST: no action'); //nota: simplify! replyJson($reply); } } else { $reply['errors'] = array('Error decoding POST: no data'); //nota: simplify! replyJson($reply); } exit;