Plugin Mascotas - FacturaScripts
Complete plugin for pet management in veterinary clinics
📥 Download Complete Plugin🚀 What does this Plugin do?
Main Features
- Complete pet management: Create, edit, list and delete
- Custom fields: Name, species, gender, age, date of birth
- Special actions: Bulk deletion and record copying
- Integration with FacturaScripts: "Clínica" menu with custom icons
Technical Features
- MVC Architecture: Separate Model, View, Controller
- Database: Optimized table with serial primary key
- XML Views: Responsive and customizable interface
- Extended controllers: Advanced functionality included
🔍 Explore the Plugin Components
Select which part of the code you want to analyze in detail:
Pet Plugin Components
⚙️ Settings - facturascripts.ini
name = 'MiPlugin'
description = 'Mi primer plugin para FacturaScripts'
version = 0.1
min_version = 2024
Configuration File Explanation
- name: Unique plugin identifier
- description: Description shown in the admin panel
- version: Plugin's semantic version
- min_version: Minimum required FacturaScripts version
🗄️ Data Model - Mascota.php
Model Code
<? php
namespace FacturaScripts\Plugins\MiPlugin\Model;
use FacturaScripts\Core\Model\Base\ModelClass;
use FacturaScripts\Core\Model\Base\ModelTrait;
class Mascota extends ModelClass
{
use ModelTrait;
public $idmascota;
public $nombre;
public $edad;
public $especie;
public $genero;
public $fechanacimiento;
public static function primaryColumn(): string
{
return 'idmascota';
}
public static function tableName(): string
{
return 'mascotas';
}
}
Model Analysis
- Namespace: Organizes the code according to the folder structure
- ModelTrait: Provides automatic CRUD methods (save, delete, load)
- Public properties: Map directly to the database columns
- primaryColumn(): Defines the table's primary key
- tableName(): Specifies the table name in the DB
🗃️ Database Schema - mascotas.xml
Table/mascotas.xml
<?xml version="1.0" encoding="UTF-8"?>
<table>
<column>
<name>idmascota</name>
<type>serial</type>
<null>NO</null>
</column>
<column>
<name>nombre</name>
<type>character varying(25)</type>
<null>NO</null>
</column>
<column>
<name>edad</name>
<type>integer</type>
</column>
<column>
<name>especie</name>
<type>character varying(25)</type>
</column>
<column>
<name>genero</name>
<type>character varying(25)</type>
</column>
<column>
<name>fechanacimiento</name>
<type>date</type>
</column>
<constraint>
<name>mascotas_pkey</name>
<type>PRIMARY KEY (idmascota)</type>
</constraint>
</table>
Database Structure
- idmascota (serial): Auto-incrementing primary key
- nombre (varchar 25): Required field for the name
- edad (integer): Integer number for the age
- especie/género (varchar 25): Length-limited text
- fechanacimiento (date): Date type for storing birthdays
🎮 Controllers
EditMascota.php - Edit Controller
<? php
namespace FacturaScripts\Plugins\MiPlugin\Controller;
use FacturaScripts\Core\Lib\ExtendedController\EditController;
class EditMascota extends EditController
{
public function getModelClassName(): string
{
return 'Mascota';
}
}
Function: Minimalist controller that inherits all editing functionality from EditController. It only needs to specify which model it handles.
ListMascota.php - List Controller
<? php
namespace FacturaScripts\Plugins\MiPlugin\Controller;
use FacturaScripts\Core\Lib\ExtendedController\ListController;
use FacturaScripts\Dinamic\Model\Mascota;
use FacturaScripts\Core\Tools;
class ListMascota extends ListController
{
public function getPageData(): array
{
$page = parent::getPageData();
$page['title'] = 'Mascotas';
$page['menu'] = 'Clinica';
$page['icon'] = 'fa-solid fa-paw';
return $page;
}
protected function createViews()
{
$this->addView("ListMascota", 'mascota','mascota','fa-solid fa-paw')
->addOrderBy(['nombre'], 'nombre')
->addSearchFields(['nombre']);
$this->addView("ListCliente", "Cliente", "Clientes","fa-solid fa-person")
->addOrderBy(['nombre'], 'nombre')
->addSearchFields(['nombre']);
}
protected function execPreviousAction($action){
switch($action){
case "borrar-todo":
$this->borrarTodo();
break;
case "copiar":
$this->copiar();
break;
default:
parent::execPreviousAction($action);
}
}
protected function borrarTodo(){
$mascotaModel = new Mascota();
$mascotas = $mascotaModel->all();
foreach($mascotas as $mascota){
$mascota->delete();
}
Tools::log()->notice('Se han borrado todas las mascotas correctamente');
}
protected function copiar(){
for($i = 0; $i <= 100; $i++){
$mascota = new Mascota();
$mascota->nombre = 'Copia';
$mascota->especie = 'Perro';
$mascota->genero = 'macho';
$mascota->edad = '5';
$mascota->fechanacimiento = 'pepe';
$mascota->save();
}
Tools::log()->notice('Se ha copiado la mascota correctamente');
}
}
Advanced features: Includes custom actions (delete all, copy), multiple views, sorting and search.
👁️ XML Views
EditMascota.xml - Edit View
<?xml version='1.0' encoding='UTF-8'?>
<view>
<columns>
<column name='Nombre' numcolumns='6' order='100'>
<widget type='text' fieldname='nombre'/>
</column>
<column name='Especie' order='105'>
<widget type='text' fieldname='especie'/>
</column>
<column name='Genero' order='110'>
<widget type='text' fieldname='genero'/>
</column>
<column name='Edad' order='115'>
<widget type='text' fieldname='edad'/>
</column>
<column name='Fecha de Nacimiento' order='120'>
<widget type='date' fieldname='fecha_nacimiento'/>
</column>
</columns>
</view>
Responsive layout: Nombre takes up 6 columns (half the width), the other fields are distributed automatically.
ListMascota.xml - List View
<?xml version='1.0' encoding='UTF-8'?>
<view>
<columns>
<column name='Nombre' order='100'>
<widget type='text' fieldname='nombre'/>
</column>
<column name='Especie' order='105'>
<widget type='text' fieldname='especie'/>
</column>
<column name='Genero' order='110'>
<widget type='text' fieldname='genero'/>
</column>
<column name='Edad' order='115'>
<widget type='text' fieldname='edad'/>
</column>
<column name='Fecha de Nacimiento' order='120'>
<widget type='date' fieldname='fecha_nacimiento'/>
</column>
</columns>
<rows>
<row type="actions">
<button action="borrar-todo" color="warning" icon="fas fa-vial" label="Borrar" type="action"/>
<button action="copiar" color="warning" icon="fas fa-terminal" label="Copiar" type="action"/>
</row>
</rows>
</view>
Custom actions: Buttons at the bottom to delete all and copy records, connected to the controller's methods.
🔄 Plugin Workflow
1. Installation and Activation
- Copy the MiPlugin folder to /Plugins/
- Activate from Admin → Plugins
- The system automatically creates the 'mascotas' table
2. Navigation
- A new "Clínica" menu appears with a paw icon
- "Mascotas" submenu for managing records
- List view with action buttons
3. Features
- Create: Form with automatic validation
- Edit: Responsive interface with all fields
- List: With sorting and search
- Bulk actions: Delete all and copy records
✅ Best Practices Implemented
structure
- Namespaces organized according to standards
- Clear MVC separation
- Well-formed XML files
Functionality
- Inherits from FacturaScripts base classes
- Uses Traits for common functionality
- Informative log messages
UX/UI
- Consistent FontAwesome icons
- Responsive layout
- Clearly labeled actions
🚀 Possible Improvements and Extensions
Advanced Validations
Implement: Date-of-birth validation, predefined species, dropdown genders.
Benefit: Greater data consistency.
Owner Relationships
Implement: Many-to-One relationship with the clients table.
Benefit: Complete management of pets and owners.
Medical History
Implement: Related table for veterinary visits.
Benefit: Complete clinical history system.