示例#1
0
function insert_attachments($var, $upload_path, $file_name_prefix, $max_count)
{
    return update_attachments($var, $upload_path, $file_name_prefix, $max_count, array());
}
 public function Execute($db, $params)
 {
     if (!isset($params["OwnerID"]) || !UUID::TryParse($params["OwnerID"], $this->UserID)) {
         header("Content-Type: application/json", true);
         echo '{ "Message": "Invalid parameters" }';
         exit;
     }
     $this->Inventory = new ALT($db);
     $this->Name = 'My Inventory';
     $avtype = "DefaultAvatar";
     if (isset($params["AvatarType"])) {
         $avtype = $params["AvatarType"];
     }
     log_message('info', "Creating avatar inventory with type {$avtype}");
     try {
         $avtypehandler = AvatarInventoryFolderFactory::Create($avtype, $this->Name, $this->UserID);
     } catch (Exception $ex) {
         log_message('error', sprintf("Error occurred in avcreation %s", $ex));
         header("Content-Type: application/json", true);
         echo '{ "Message": "Failed loading avatar template "' . $avtype . ': ' . $ex->getMessage() . ' }';
         exit;
     }
     if (!$avtypehandler) {
         // Handle error and return
         log_message('error', "Failed to create handler for avatar with type {$avtype}");
         header("Content-Type: application/json", true);
         echo '{ "Message": "Invalid parameters" }';
         exit;
     }
     $skeleton = $avtypehandler->Folders();
     $items = $avtypehandler->Items();
     $db->beginTransaction();
     for ($i = 0; $i < count($skeleton); $i++) {
         try {
             $Folder = new InventoryFolder($skeleton[$i]["ID"]);
             $Folder->ParentID = UUID::Parse($skeleton[$i]["ParentID"]);
             $Folder->OwnerID = $this->UserID;
             $Folder->Name = $skeleton[$i]["Name"];
             $Folder->ContentType = $skeleton[$i]["PreferredContentType"];
             if (!$this->Inventory->InsertNode($Folder)) {
                 $db->rollBack();
                 log_message('error', sprintf("Error occurred during folder creation: %s", $this->Inventory->LastError));
                 header("Content-Type: application/json", true);
                 echo '{ "Message": "Inventory creation error" }';
                 exit;
             }
         } catch (Exception $ex) {
             $db->rollBack();
             log_message('error', sprintf("Error occurred during query: %s", $ex));
             header("Content-Type: application/json", true);
             echo '{ "Message": "Database query error" }';
             exit;
         }
     }
     for ($i = 0; $i < count($items); $i++) {
         try {
             $item = new InventoryItem($items[$i]['ID']);
             $item->ParentID = UUID::Parse($items[$i]['ParentID']);
             $item->OwnerID = $this->UserID;
             $creatorID = $this->UserID;
             if (array_key_exists('CreatorID', $items[$i])) {
                 $creatorID = UUID::Parse($items[$i]['CreatorID']);
             }
             $item->CreatorID = $creatorID;
             $item->Name = $items[$i]['Name'];
             $item->AssetID = UUID::Parse($items[$i]['AssetID']);
             if (!$this->Inventory->InsertNode($item)) {
                 $db->rollBack();
                 log_message('error', sprintf("Error occurred during item creation: %s", $this->Inventory->LastError));
                 header("Content-Type: application/json", true);
                 echo '{ "Message": "Inventory creation error" }';
                 exit;
             }
         } catch (Exception $ex) {
             $db->rollBack();
             log_message('error', sprintf("Error occurred during query: %s", $ex));
             header("Content-Type: application/json", true);
             echo '{ "Message": "Database query error" }';
             exit;
         }
     }
     $db->commit();
     // Update this users appearance in the user service
     $appearance = $avtypehandler->Appearance();
     if ($appearance) {
         $response = update_appearance($this->UserID, $appearance);
         if (empty($response['Success'])) {
             header("Content-Type: application/json", true);
             echo sprintf('{ "Message": "%s" }', $response['Message']);
             exit;
         }
     }
     // Update this users attachments in the user service
     $attachments = $avtypehandler->Attachments();
     if ($attachments) {
         $response = update_attachments($this->UserID, $attachments);
         if (empty($response['Success'])) {
             header("Content-Type: application/json", true);
             echo sprintf('{ "Message": "%s" }', $response['Message']);
             exit;
         }
     }
     // Add any additional customizations
     $avtypehandler->Configure();
     // And return success
     header("Content-Type: application/json", true);
     echo sprintf('{"Success": true, "FolderID": "%s"}', $this->UserID);
     exit;
 }