I have a grasp of what I need to do, but I'm having trouble figuring out how adopt shops are created without their own separate php files, like how you do it through the adminCP. I managed to make a way for users to create/edit/remove shops by making a table in the database similar to how other shops are stored:
PHP Code:
<?php
use Resource\Native\String;
use Resource\Collection\LinkedHashMap;
class ShopcpController extends AppController{
const PARAM = "usid";
public function __construct(){
parent::__construct();
$mysidia = Registry::get("mysidia");
if($mysidia->usergroup->getpermission("groupname") != "rootadmins"){
throw new NoPermissionException("Sorry! Only admins can test user shops at this time!");
}
}
public function index(){
parent::index();
$mysidia = Registry::get("mysidia");
$stmt = $mysidia->db->select("user_shops");
$num = $stmt->rowCount();
if($num == 0) throw new InvalidIDException("You don't have a shop!");
$this->setField("stmt", new DatabaseStatement($stmt));
}
public function add(){
$mysidia = Registry::get("mysidia");
if($mysidia->input->post("submit")){
$this->dataValidate();
$npc_url = (!$mysidia->input->post("npc_url"))?$mysidia->input->post("existingnpc_url"):$mysidia->input->post("npc_url");
$tax = (!$mysidia->input->post("tax"))?0:$mysidia->input->post("tax");
$mysidia->db->insert("user_shops", array("us_name" => $mysidia->input->post("us_name"), "npc_url" => $npc_url,
"description" => $mysidia->input->post("description"), "tax" => $tax, "manager_id" => $mysidia->user->uid));
}
}
public function edit(){
$mysidia = Registry::get("mysidia");
if(!$mysidia->input->get("usid")){
$this->index();
throw new NoPermissionException("You do not own this shop!");
return;
}
elseif($mysidia->input->post("submit")){
$this->dataValidate();
$npc_url = (!$mysidia->input->post("npc_url"))?$mysidia->input->post("existingnpc_url"):$mysidia->input->post("npc_url");
$tax = (!$mysidia->input->post("tax"))?0:$mysidia->input->post("tax");
$mysidia->db->update("user_shops", array("us_name" => $mysidia->input->post("us_name"), "description" => $mysidia->input->post("description"), "npc_url" => $npc_url,
"status" => $mysidia->input->post("status"), "tax" => $tax), "usid='{$mysidia->input->get("usid")}'");
return;
}
else{
$shop = $mysidia->db->select("user_shops", array(), "usid='{$mysidia->input->get("usid")}'")->fetchObject();
if(!is_object($shop)) throw new InvalidIDException("nonexist");
$this->setField("user_shop", new DataObject($shop));
}
}
public function delete(){
$mysidia = Registry::get("mysidia");
$document = $mysidia->frame->getDocument();
if($mysidia->user->uid !== $mysidia->input->get("manager_id")){
$this->index();
throw new NoPermissionException("You do not own this shop!");
return;
}
$mysidia->db->delete("user_shops", "usid='{$mysidia->input->get("usid")}'");
}
private function dataValidate(){
$mysidia = Registry::get("mysidia");
if(!$mysidia->input->post("us_name")) throw new BlankFieldException("us_name");
if(!$mysidia->input->post("npc_url") and $mysidia->input->post("existingnpc_url") == "none") throw new BlankFieldException("images");
if(!$mysidia->input->post("status")) throw new BlankFieldException("status");
if($mysidia->input->post("tax") < 0) throw new InvalidActionException("tax");
$shop = $mysidia->db->select("user_shops", array(), "us_name = '{$mysidia->input->post("us_name")}'")->fetchObject();
if($this->action == "add" and is_object($shop)) throw new DuplicateIDException("duplicate");
return TRUE;
}
}
?>
(shopcp.php)
PHP Code:
<?php
use Resource\Native\String;
use Resource\Collection\LinkedHashMap;
class ShopcpView extends View{
public function index(){
parent::index();
$stmt = $this->getField("stmt")->get();
$document = $this->document;
$fields = new LinkedHashMap;
$fields->put(new String("npc_url"), new String("getImage"));
$fields->put(new String("us_name"), NULL);
$fields->put(new String("description"), NULL);
$fields->put(new String("status"), NULL);
$fields->put(new String("usid::edit"), new String("getEditLink"));
$fields->put(new String("usid::delete"), new String("getDeleteLink"));
$shopTable = new TableBuilder("shop");
$shopTable->setAlign(new Align("center", "middle"));
$shopTable->buildHeaders("NPC Image", "Shop", "Description", "Status", "Edit", "Delete");
$shopTable->setHelper(new ShopTableHelper);
$shopTable->buildTable($stmt, $fields);
$document->add($shopTable);
}
public function add(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
if($mysidia->input->post("submit")){
$document->setTitle($this->lang->added_title);
$document->addLangvar($this->lang->added);
return;
}
$document->setTitle($this->lang->add_title);
$document->addLangvar($this->lang->add);
$shopForm = new FormBuilder("addform", "add", "post");
$shopForm->add(new Comment("<br><u><h2>Create A New Shop:</u></h2>", TRUE, "b"));
$shopForm->add(new Comment("<hr>-Basic Settings-", TRUE, "b"));
$shopForm->add(new Comment("Shop Name: ", FALSE));
$shopForm->add(new TextField("us_name"));
$shopForm->add(new Comment($this->lang->us_name_explain));
$shopForm->add(new Comment("<br></br>"));
$shopForm->add(new Comment("Shop Description:"));
$shopForm->add(new TextArea("description", "Here you can enter a description for your shop", 4, 50));
$shopForm->add(new Comment("Shop NPC Image: ", FALSE));
$shopForm->add(new TextField("npc_url"));
$shopForm->add(new Comment($this->lang->npc_url_explain));
$shopForm->add(new Comment("<hr>-Miscellaneous Settings-", TRUE, "b"));
$shopForm->add(new Comment("Shop Status: ", FALSE));
$shopStatus = new RadioList("status");
$shopStatus->add(new RadioButton("Open", "status", "open"));
$shopStatus->add(new RadioButton("Closed", "status", "closed"));
//$shopStatus->add(new RadioButton("Hidden", "status", "invisible"));
$shopStatus->check("closed");
$shopForm->add($shopStatus);
$shopForm->add(new Comment("Non-Resident Tax: ", FALSE));
$shopForm->add(new TextField("NRtax"));
$shopForm->add(new Comment($this->lang->nrtax_explain));
$shopForm->add(new Comment("<br></br>"));
$shopForm->add(new Button("Create Shop", "submit", "submit"));
$document->add($shopForm);
}
public function edit(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
if(!$mysidia->input->get("usid")){
$this->index();
return;
}
elseif($mysidia->input->post("submit")){
$document->setTitle($this->lang->edited_title);
$document->addLangvar($this->lang->edited);
return;
}
else{
$shop = $this->getField("user_shop")->get();
$document->setTitle($this->lang->edit_title);
$document->addLangvar($this->lang->edit);
$shopForm = new FormBuilder("editform", $mysidia->input->get("usid"), "post");
$shopForm->add(new Comment("<br><u>Edit an existing Shop:</u>", TRUE, "b"));
$shopForm->add(new Comment("Basic Settings", TRUE, "b"));
$shopForm->add(new Comment("Shop Name: ", FALSE));
$shopForm->add(new TextField("us_name", $shop->us_name));
$shopForm->add(new Comment($this->lang->us_name_explain));
$shopForm->add(new Comment("Shop Description:"));
$shopForm->add(new TextArea("description", $shop->description, 4, 50));
$shopForm->add(new Comment("Shop NPC Image: ", FALSE));
$shopForm->add(new TextField("npc_url", $shop->npc_url));
$shopForm->add(new Comment($this->lang->npc_url_explain));
$shopForm->add(new Comment("<hr>Miscellaneous Settings:", TRUE, "b"));
$shopForm->add(new Comment("Shop Status: ", FALSE));
$shopStatus = new RadioList("status");
$shopStatus->add(new RadioButton("Open", "status", "open"));
$shopStatus->add(new RadioButton("Closed", "status", "closed"));
//$shopStatus->add(new RadioButton("Hidden", "status", "invisible"));
$shopStatus->check($shop->status);
$shopForm->add($shopStatus);
$shopForm->add(new Comment("Non-Resident Tax: ", FALSE));
$shopForm->add(new TextField("NRtax", $shop->tax));
$shopForm->add(new Comment($this->lang->nrtax_explain));
$shopForm->add(new Button("Edit Shop", "submit", "submit"));
$document->add($shopForm);
}
}
public function delete(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
if(!$mysidia->input->get("usid")){
$this->index();
return;
}
$document->setTitle($this->lang->delete_title);
$document->addLangvar($this->lang->delete);
header("Refresh:3; URL='../../shopcp'");
}
}
?>
(shopcpview.php)
The shop is made, but it somehow needs to be able to be accessed like an actual shop. I planned to have it set up like other sites where the pet in question is still owned by the user selling it until another user buys it, which wouldn't be too difficult. You would just have to add another column to owned adoptables for the usid (user shop ID) and whether or not it is for sale, then a table could just fetch them for the display and from there it would work somewhat similar to the trade code already on the site. There is also the problem of users being able to edit and delete shops that don't belong to them by changing the ID in the url. I tried to remedy this with:
PHP Code:
if($mysidia->user->uid !== $mysidia->input->get("manager_id")){
code for the error goes here
}
But it just throws an error saying that the shop does not belong to the user, even if the manager_id and uid match.