Avatar Gallery Mod

Step 1:

Make a new table in phpmyadmin called prefix_avatars and add 3 columns like the above image.


Step 2:

We've got to make three new files:

(home)/admincp/addavatar.php:

<?php


use Resource\Native\String;


class ACPAddAvatarController extends AppController{


const PARAM = "type";

public function __construct(){

parent::__construct();

$mysidia = Registry::get("mysidia");

if($mysidia->usergroup->getpermission("canmanageadopts") != "yes"){

throw new NoPermissionException("You do not have permission to manage avatars.");

}

}


public function add(){

// The action of creating a new marking

$mysidia = Registry::get("mysidia");

if($mysidia->input->post("submit")){

// The form has been submitted, it's time to validate data and add a record to database.

if($mysidia->session->fetch("acpAddAvatar") != "add"){

$this->setFlag("global_error", "Session already expired...");

return;

}

$mysidia->db->insert("avatars", array("id" => NULL, "name" => $mysidia->input->post("name"), "url" => $mysidia->input->post("url"), "usergroup" => $mysidia->input->post("usergroup")));

$mysidia->session->terminate("acpAddAvatar");

return;

}

$mysidia->session->assign("acpAddAvatar", "add", TRUE);

}


public function delete(){


}

}

?>


(home)/admincp/view/addavatarview.php:

<?php


class ACPAddAvatarView extends View{


public function index(){

parent::index();

$document = $this->document;

$document->add(new Link("admincp/addavatar/add", "Add a new user avatar",TRUE));

$document->add(new Link("admincp/addavatar/delete", "Delete an avatar"));

}


public function add(){

// The action of creating a new base!

$mysidia = Registry::get("mysidia");

$document = $this->document;

if($mysidia->input->post("submit")){

$document->setTitle("Added Avatar");

$document->add(new Comment("You added a new avatar."));

return;

}

$document->setTitle("Add Avatar");

$document->add(new Comment("Fill out this form to add a new avatar."));

$adoptForm = new Form("addform", "", "post");

$title = new Comment("Create a New Avatar:");

$title->setBold();

$title->setUnderlined();

$adoptForm->add($title);

$basicInfo = new FieldSetBuilder("Basic Information");

$basicInfo->add(new Comment("Name: ", FALSE));

$basicInfo->add(new TextField("name"));

$basicInfo->add(new Comment("(This is the name of the avatar used to call it.)"));

$basicInfo->add(new Comment("URL: ", FALSE));

$basicInfo->add(new TextField("url"));

$basicInfo->add(new Comment("(The image URL.)"));

$basicInfo->add(new Comment("Usergroup: ", FALSE));

$basicInfo->add(new TextField("usergroup"));

$basicInfo->add(new Comment("(Which user group is allowed to use this avatar. Use registered to restrict it to just regular members, or admin for staff members only.)"));

$adoptForm->add($basicInfo);



$adoptForm->add(new Button("Create", "submit", "submit"));

$document->add($adoptForm);

}


public function delete(){


}

}

?>


(home)/lang/admincp/lang_addavatar.php

<?php


//Language variables used for AdminCP/AddAvatar Page


$lang['default_title'] = "Add Avatar";

$lang['default'] = "From this page you can add a new user avatar.<br>";


?>


Save those, now we have to make it so you can easily get to that page in the AdminCP.


Step 3:

Go to classes/class_adminsidebar.php and scroll down to this section:

$components->add(new Division(new Comment("Users", FALSE)));

Now add this code under the line $users = new Division;

$users->add(new Link("admincp/addavatar", "Add Avatars"));


Save and close out of there. Now you can go to your AdminCP, click the Users drop-down, and see the Add Avatars option.


Step 4:

Now we must edit the files view/accountview.php and account.php to add a new function called avatar.

In account.php place this code at the closing bracket for the Contacts function:

public function avatar(){

$mysidia = Registry::get("mysidia");

//Determine if user is staff or not

$group = $mysidia->db->select("users", array("usergroup"), "username = '{$mysidia->user->username}'")->fetchColumn();

if($group == 3){

$stmt = $mysidia->db->select("avatars", array("id"), "usergroup = 'registered'");

$this->setField("stmt", new DatabaseStatement($stmt));

}

else{

$stmt = $mysidia->db->select("avatars", array("id"));

$this->setField("stmt", new DatabaseStatement($stmt));

}

if($mysidia->input->post("submit")){

$mysidia->db->update("users_profile", array("avatar" => $mysidia->input->post("avatar")), "username = '{$mysidia->user->username}'");

return;

}


}

Next in view/accountview.php add this function:

public function avatar(){

$mysidia = Registry::get("mysidia");

$document = $this->document;

if($mysidia->input->post("submit")){

$document->setTitle("Avatar Changed");

$document->add(new Comment("You successfully changed your avatar."));

return;

}

$document->setTitle("Change Avatar");

$document->add(new Comment("You can choose from the available avatars below.

<form action='../../account/avatar' method='post'>"));

$stmt = $this->getField("stmt")->get();

if($stmt->rowCount() == 0){

$document->add(new Comment("No available avatars."));

return;

}

while($id = $stmt->fetchColumn()){

$name = $mysidia->db->select("avatars", array("name"), "id = $id")->fetchColumn();

$url = $mysidia->db->select("avatars", array("url"), "id = $id")->fetchColumn();

$document->add(new Comment("<input type='radio' id='{$name}' name='avatar' value='{$url}'>

<label for='avatar'><img src='{$url}'/></label><br>"));

}

$document->add(new Comment("<input type='submit' id='submit' name='submit' value='Select'>

</form>"));

}

Step 5:

The final step is making it so your users can access the avatar gallery to change their avatar. You can do this a variety of ways, but this is how I do it.

In view/accountview.php go down to where the account links are, under $document->add($settings);. Now under that add this:

$document->add(new Link("account/avatar", "Change Avatar", TRUE));

Now you want to get rid of the other areas where they can change their avatar. In that same file scroll down to $profileForm->add($formTitle); and REMOVE these lines:

$profileForm->add(new Comment("Avatar: ", FALSE));

$profileForm->add(new TextField("avatar", $profile->getAvatar()));

Save this file and close out. Now open up account.php and scroll down to the $mysidia->db->update under Public Function Profile. Replace the update with this:

$mysidia->db->update("users_profile", array("nickname" => $mysidia->input->post("nickname"), "gender" => $mysidia->input->post("gender"), "color" => $mysidia->input->post("color"), "bio" => $mysidia->input->post("bio"), "favpet" => $mysidia->input->post("favpet"), "about" => $mysidia->input->post("about")), "username = '{$mysidia->user->username}'");

That just removes where it adds the avatar, since we don't want it being added there.

That's it! Now when a user goes to their account settings page a link to change their avatar will appear and take them to the avatar gallery.

If you have any questions feel free to reach out on the Mysidia Adoptables Discord.