/**
  * Saves network to databse.
  * used for creating and updating the network
  * @access public.
  * called after $this->set_params()
  */
 public function save()
 {
     global $error, $error_msg;
     $error = false;
     Logger::log("[ Enter: function Network::save]\n");
     //first check whether it is insert or update
     // global var $path_prefix has been removed - please, use PA::$path static variable
     if ($this->network_id) {
         $old_type = Network::find_network_type($this->network_id);
         //update
         $update_fields = array('name', 'tagline', 'category_id', 'description', 'extra', 'type', 'inner_logo_image');
         $sql = " UPDATE {networks} SET changed = ? ";
         $data_array = array(time());
         foreach ($update_fields as $field) {
             if (isset($this->{$field})) {
                 $sql .= " , {$field} = ? ";
                 array_push($data_array, $this->{$field});
             }
         }
         $sql .= " WHERE network_id = ? ";
         array_push($data_array, $this->network_id);
         $res = Dal::query($sql, $data_array);
         //fix for changing a network from private to public
         //the waiting_members must be changed to members
         $new_type = $this->type;
         if ($old_type == PRIVATE_NETWORK_TYPE && $new_type == REGULAR_NETWORK_TYPE) {
             Network::approve_all($this->network_id);
         }
     } else {
         //insert
         // here we have to do a lot of steps
         //first check if network already exists with same address
         if (Network::check_already($this->address)) {
             Logger::log("Thowing Exception NETWORK_ALREADY_EXISTS");
             throw new PAException(NETWORK_ALREADY_EXISTS, "Network with same address already exists");
         }
         // checks the permissions of directory network
         $network_folder = PA::$project_dir . "/networks";
         if (!is_writable($network_folder)) {
             Logger::log("Thowing Exception NETWORK_DIRECTORY_PERMISSION_ERROR");
             throw new PAException(NETWORK_DIRECTORY_PERMISSION_ERROR, "Network folder ({$network_folder}) is not writable. Please check the permissions");
         }
         //if we have come this far we can insert the network easily
         // TODO add acl permission check
         //insert into networks
         $this->created = time();
         $this->type = $this->type ? $this->type : REGULAR_NETWORK_TYPE;
         $res = Dal::query("INSERT INTO {networks} (name, address, tagline, type,category_id, description,is_active, created, changed, extra, member_count, owner_id, inner_logo_image) VALUES ( ?, ?, ?,?, ?, ?, ?, ?, ?, ?, 1, ?, ? )", array($this->name, $this->address, $this->tagline, $this->type, $this->category_id, $this->description, 1, $this->created, $this->changed, $this->extra, $this->user_id, $this->inner_logo_image));
         $this->network_id = Dal::insert_id();
         //insert into networks_users
         $user_created = Dal::query_first("SELECT created FROM users WHERE user_id=?", $this->user_id);
         $res = Dal::query("INSERT INTO {networks_users} (network_id, user_id, user_type, created) VALUES (?, ?, ?, ?)", array($this->network_id, $this->user_id, NETWORK_OWNER, $user_created));
         //Now we have inserted new network we need to create other directory and config files as well
         try {
             $this->do_network_setup();
         } catch (PAException $e) {
             $error = TRUE;
             $error_msg = "{$e->message}";
         }
         if ($error) {
             $this->do_rollback();
             throw new PAException(NETWORK_INTERNAL_ERROR, "Some internal error occured while setting up the network. Message: {$error_msg}");
         }
     }
     //.. insert
     Logger::log("[ Exit: function Network::save]\n");
     return $this->network_id;
 }
 function testNetworkCreation()
 {
     // check that we can create networks
     $can = Network::can_network_be_created();
     $this->assertFalse($can['error'], $can['error_msg']);
     // get network owner user and figure out name etc
     $user = Test::get_test_user();
     $name = "testnet" . rand(10000, 99999);
     $network_basic_controls = array();
     // with crossed fingers, we hope that it will succeed without any of the detail here!
     // make a new network
     $net = new Network();
     $net->set_params(array('user_id' => $user->user_id, 'name' => "auto-test network ({$name})", 'address' => $name, 'tagline' => "not much of a tagline", 'category_id' => 8, 'type' => 0, 'description' => "This network has been created automatically by a PHPUnit test.  If the test succeeds, it will be deleted, too!", 'extra' => serialize($network_basic_controls), 'created' => time(), 'changed' => time()));
     $net->save();
     //default_page_setting($net->address);
     // read it in again and see if it still works
     $net_read = Network::get_network_by_address($net->address);
     $this->assertEquals($net_read->network_id, $net->network_id);
     $this->assertEquals($net_read->type, 0);
     $this->assertEquals($net_read->member_count, 1);
     $this->assertEquals($net_read->owner_id, $user->user_id);
     // a user joins
     $user2 = Test::get_test_user(2);
     Network::join($net->network_id, $user2->user_id);
     $this->assertEquals(Network::get_network_by_address($net->address)->member_count, 2);
     // a user leaves
     Network::leave($net->network_id, $user2->user_id);
     $this->assertEquals(Network::get_network_by_address($net->address)->member_count, 1);
     // make it into a moderated network
     $net->type = 2;
     $net->save();
     // check that it really is moderated
     $net_read = Network::get_network_by_address($net->address);
     $this->assertEquals($net_read->network_id, $net->network_id);
     $this->assertEquals($net_read->type, 2);
     // a user requests
     Network::join($net->network_id, $user2->user_id);
     $this->assertEquals(Network::get_network_by_address($net->address)->member_count, 1);
     // request approved
     Network::approve($net->network_id, $user2->user_id);
     $this->assertEquals(Network::get_network_by_address($net->address)->member_count, 2);
     // user leaves
     Network::leave($net->network_id, $user2->user_id);
     $this->assertEquals(Network::get_network_by_address($net->address)->member_count, 1);
     // user requests
     Network::join($net->network_id, $user2->user_id);
     $this->assertEquals(Network::get_network_by_address($net->address)->member_count, 1);
     // all requests accepted (of course, there will only be the one)
     Network::approve_all($net->network_id);
     $this->assertEquals(Network::get_network_by_address($net->address)->member_count, 2);
     // user leaves
     Network::leave($net->network_id, $user2->user_id);
     $this->assertEquals(Network::get_network_by_address($net->address)->member_count, 1);
     // user requests
     Network::join($net->network_id, $user2->user_id);
     $this->assertEquals(Network::get_network_by_address($net->address)->member_count, 1);
     // request denied
     Network::deny($net->network_id, $user2->user_id);
     $this->assertEquals(Network::get_network_by_address($net->address)->member_count, 1);
     // delete network
     Network::delete($net->network_id);
 }