function handle_update_cake($type, $from, $data, $res) { global $connection; global $logger; $from->need_level("owner"); $logger->debug("Start updating the cake " . $from->pieid . " initiated by used " . $from->user_id()); $changes = $data['data']; // name, description foreach (array('name', 'description') as $prop) { if (array_key_exists($prop, $changes)) { $logger->debug("Updating property '" . $prop . "'"); if (!pg_query_params($connection, 'UPDATE pies SET "' . $prop . '" = $2 WHERE id = $1', array($from->pieid, $changes[$prop]))) { throw new Exception("Failed to update cake settings for prop: " . $prop); } } } // visible = true/false if (array_key_exists('visible', $changes)) { $logger->debug("Updating property 'visible' = " . $changes['visible']); if (!pg_query($connection, 'UPDATE pies SET "visible" = ' . ($changes['visible'] ? 'true' : 'false') . ' WHERE id = ' . $from->pieid)) { throw new Exception("Failed to update cake settings for prop: visible"); } } // // update settings JSON // // firstly load: $result = pg_query($connection, 'SELECT settings FROM pies WHERE id = ' . $from->pieid); if (!$result) { throw new Exception("Failed to load pie information. Can't update settings JSON"); } $settings = json_decode(pg_fetch_result($result, 0, 'settings'), true); // Update with new data if (array_key_exists('links', $changes)) { $settings['links'] = $changes['links']; } // Update with new data if (array_key_exists('statuses', $changes)) { $settings['statuses'] = $changes['statuses']; } // Save new JSON if (!pg_query_params($connection, 'UPDATE pies SET settings = $1 where id = $2', array(json_encode($settings), $from->pieid))) { throw new Exception("Failed to update pie entry with new JSON"); } _send_cake_info($res, $from, false); }
function handle_init_session($type, $from, $data, $res) { # TODO move here all other initialization steps _youare($res, $from); _send_cake_info($res, $from, true); $res->to_session($from, array("after_init")); }