I'm aware there's an existing script like this, but I wanted to create my own with its own purpose: any item can be held by a pet, no matter what the item function is (e.g. whether it's a key item or a consumable food item), and it can be taken away or switched with a different item at any time, and shown off; essentially as a pet "treasure."
Essentially, this script installs a "give" button to the inventory that lets users give any owned item to a pet (different from using or tossing). You can give a new treasure item to a pet and it'll retrieve the old item in its place, or you can go to a pet's My Adopt manage page and click "Remove Treasure."
First, navigate to service/helper/itemtablehelper.php and add this underneath the getUseForm() function, above the getSellForm() function.
Now go to controller/main/inventorycontroller.php and add this function underneath the uses() function, above the sell() function.
Go to view/main/inventoryview.php and put this after uses(), above sell().
Add "Give" after "Use" and before "Sell" in $inventoryTable->buildHeaders(...) in the topmost index() function in that same inventoryview.php file like so:
...and add this line as well, after
Now navigate to model\domainmodel\ownedadoptable.php.
Add
Above
Open view\main\myadoptsview.php. Paste this in the manage() function, before
Now add this after
"templates/icons/package-delete.png" references an image you haven't uploaded yet, so it's time to upload it! Save this image (or, replace it as you'd like) as "package-delete.png" and upload it to the templates\icons subdirectory.
Now, go back to view\main\myadoptsview.php. Paste this inside class Myadoptsview, right before the final closing bracket.
Now to update your tables!
Navigate to (prefix)_owned_adoptables, edit the structure of the table, and add 1 new column named "treasure" with type "varchar(60)", null set to No, and default set to "noitem". It should look like this when done.
By default, this script only displays your pet's treasure on the Manage Your Adoptable page. It's up to you to add more functionality and display pet treasures on other pages, like a pet's Level Up page, a user profile's adopts tab, the My Adopts page, or wherever else you might think of.
Try using this script:
Add $treasuretxt to a new Comment, and you should have text and an image with your pet's treasure!
Essentially, this script installs a "give" button to the inventory that lets users give any owned item to a pet (different from using or tossing). You can give a new treasure item to a pet and it'll retrieve the old item in its place, or you can go to a pet's My Adopt manage page and click "Remove Treasure."
First, navigate to service/helper/itemtablehelper.php and add this underneath the getUseForm() function, above the getSellForm() function.
PHP:
public function getGiveForm(OwnedItem $item){
$giveForm = new FormBuilder("giveform", "inventory/give", "post");
$giveForm->setLineBreak(FALSE);
$giveForm->buildComment("")
->buildPasswordField("hidden", "action", "give")
->buildPasswordField("hidden", "item", $item->getItemID())
->buildButton("Give", "give", "give");
return $giveForm;
}
Now go to controller/main/inventorycontroller.php and add this function underneath the uses() function, above the sell() function.
PHP:
public function give() {
$mysidia = Registry::get("mysidia");
$item = new OwnedItem($mysidia->input->post("item"), $mysidia->user->getID());
if(!$item->inInventory()) throw new ItemException("use_none");
if($mysidia->input->post("aid")) {
// if input wasn't valid
if ($mysidia->input->post("validation") != "valid") {
throw new ItemException("use_fail");
} else {
// give treasure to adoptable
$mysidia->db->update("owned_adoptables", array("treasure" => $item->getItem()), "aid = '{$mysidia->input->post('aid')}'");
}
}
$stmt = $mysidia->db->select("owned_adoptables", ["aid", "name"], "owner = '{$mysidia->user->getID()}'");
$map = $mysidia->db->fetchMap($stmt);
$this->setField("item", $item);
$this->setField("petMap", $map);
}
Go to view/main/inventoryview.php and put this after uses(), above sell().
PHP:
public function give(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
if($mysidia->input->post("aid")){
$message = (string)$this->getField("message");
$document->setTitle($this->lang->global_action_complete);
$document->addLangvar($message);
return;
}
$petMap = $this->getField("petMap");
$document->setTitle($this->lang->select_title);
$document->addLangvar($this->lang->select);
$chooseFrom = new Form("chooseform", "give", "post");
$adoptable = new DropdownList("aid");
$adoptable->add(new Option("None Selected", "none"));
if($petMap->size() > 0){
$iterator = $petMap->iterator();
while($iterator->hasNext()){
$adopt = $iterator->nextEntry();
$adoptable->add(new Option($adopt->getValue(), $adopt->getKey()));
}
}
$chooseFrom->add($adoptable);
$chooseFrom->add(new PasswordField("hidden", "item", $mysidia->input->post("item")));
$chooseFrom->add(new PasswordField("hidden", "validation", "valid"));
$chooseFrom->add(new Button("Choose this Adopt", "submit", "submit"));
$document->add($chooseFrom);
}
Add "Give" after "Use" and before "Sell" in $inventoryTable->buildHeaders(...) in the topmost index() function in that same inventoryview.php file like so:
...and add this line as well, after
$cells->add(new TCell($inventoryTable->getHelper()->getUseForm($item)));
PHP:
$cells->add(new TCell($inventoryTable->getHelper()->getGiveForm($item)));
Now navigate to model\domainmodel\ownedadoptable.php.
Add
protected $treasure;
under the other protected variables at the way top.Above
protected function save($field, $value)
etc., copy paste this.
PHP:
public function getTreasure() {
return $this->treasure;
}
public function giveTreasure($treasure, $assignMode = "") {
$mysidia = Registry::get("mysidia");
$item = new OwnedItem($treasure, $mysidia->user->getID());
$item->remove(1, $mysidia->user->getID());
if ($this->treasure != "noitem") {
$olditem = new OwnedItem($this->treasure, $mysidia->user->getID());
$olditem->add(1, $mysidia->user->getID());
}
if ($assignMode == Model::UPDATE) $this->save("treasure", $treasure);
$this->treasure = $treasure;
}
public function takeTreasure($assignMode = "") {
$mysidia = Registry::get("mysidia");
$item = new OwnedItem($this->treasure, $mysidia->user->getID());
$item->add(1, $mysidia->user->getID());
if ($assignMode == Model::UPDATE) $this->save("treasure", "noitem");
$this->treasure = "noitem";
}
Open view\main\myadoptsview.php. Paste this in the manage() function, before
$document->add(new Image("templates/icons/add.gif"));
PHP:
if ($ownedAdopt->getTreasure() != "noitem") {
$treasure = new OwnedItem(($ownedAdopt->getTreasure()), $mysidia->user->getID());
$document->add(new Comment("<div style='display:flex;align-items:center;'>
<p><img src='{$treasure->getImageURL()}' alt='{$treasure->getItemname()}'></p>
<p><b>Treasure:</b> {$treasure->getItemname()}</p>
</div>"));
} else {
$document->add(new Comment("<div><p><b>Treasure:</b> None</p></div>"));
}
$document->add(new Link("levelup/click/{$ownedAdopt->getID()}", " Click Level Up Profile", TRUE));
PHP:
if ($ownedAdopt->getTreasure() != "noitem") {
$document->add(new Image("templates/icons/package-delete.png"));
$document->add(new Link("myadopts/treasure/{$ownedAdopt->getID()}", " Remove Treasure", TRUE));
}
"templates/icons/package-delete.png" references an image you haven't uploaded yet, so it's time to upload it! Save this image (or, replace it as you'd like) as "package-delete.png" and upload it to the templates\icons subdirectory.
Now, go back to view\main\myadoptsview.php. Paste this inside class Myadoptsview, right before the final closing bracket.
PHP:
public function treasure() {
$mysidia = Registry::get("mysidia");
$adopt = $this->getField("adopt");
$image = $this->getField("image");
$document = $this->document;
$document->setTitle("Remove " . $adopt->getName() . "'s Treasure");
$document->add($image);
$document->addLangvar("<p>Successfully removed {$adopt->getName()}'s treasure and returned it to the inventory.</p>");
if ($adopt->getGender() == "m") {$pronoun = "his";} else {$pronoun = "her";}
$message = "<p>You can continue to manage {$adopt->getName()}'s settings from {$pronoun} <a href='myadopts/manage/{$adopt->getAdoptID()}'>My Adopts manage page</a>.</p>";
}
Now to update your tables!
Navigate to (prefix)_owned_adoptables, edit the structure of the table, and add 1 new column named "treasure" with type "varchar(60)", null set to No, and default set to "noitem". It should look like this when done.
By default, this script only displays your pet's treasure on the Manage Your Adoptable page. It's up to you to add more functionality and display pet treasures on other pages, like a pet's Level Up page, a user profile's adopts tab, the My Adopts page, or wherever else you might think of.
Try using this script:
PHP:
$treasure = new OwnedItem($mysidia->db->select("owned_adoptables", ["treasure"], "aid = '{$this->adopt->getAdoptID()}'")->fetchColumn(), $mysidia->user->getID());
$treasuretxt = "{$treasure->getItemname()} <img class='treasure-img' src='{$treasure->getImageURL($fetchMode = 'Model::GUI')}' alt='{$treasure->getItemname()}'>";
Add $treasuretxt to a new Comment, and you should have text and an image with your pet's treasure!