Atilla Ördög

Personal site of Atilla Ördög AKA Gadratil

Category: Helper Classes

PHP Infinity tree

inline

I created a new library to handle infinite depth trees. I came up with the idea and the necessity because I always struggled to find a good library that can be simply loaded and used for infinite depth category trees, whole sub-node movements, easy node movements and such.

Also my main concern was how the tree gets stored in the database. I came up with a new idea based on MPTT, that stores the data in linear style, not using up much extra space and does not require recursive functionality in the storage. The tree itself is built up by a recursive function that builds the tree using N iterations, no overhead.

screenshot

So, the whole thing is quick, runs smoothly and deals with only the tree, nothing else.

You would ask: how can it be implemented?

This library simply adds a PHP interface to infinite depth trees. That’s it. But since it uses namespaces and replaceable adapters, it can be easily implemented anywhere.

So, let’s see the library:

A fully working example and the full documentation can be found at:

PHP Infinity Tree example and documentation

You can purchase the library here:

PHP Infinity Tree on Gadratil Programming

If you have any questions, just ask. Also, if you need implementation, I would gladly help you.

Advertisement

PHP Image Handler

I wrote about the image handling library I made. The small code snippet I wrote was poor and hard to understand.

I optimized the code and created example and documentation for my Image Handler PHP library.

Now it can be found on Gadratil Programming in the shop section.

Make your life easier using this library by adding it to any custom framework or CMS.

PHP Image Handler

 

Small car make and model handler

I just made a small package that contains a fair amount of car makes and models. The whole thing consists of two files, one is the database and the other is the handling class.

The good thing about this is that the data can be expanded and it is very easy to use.

The idea popped into my mind when I  wanted to make a car wash management application and needed autocomplete car makes and models.

You can buy this product from here:

http://codecanyon.net/item/car-makes-and-models/10960058

A very basic live preview is on:

http://carmodelsandmakes.byethost7.com/

And the documentation to it is on:

http://carmodelsandmakes.byethost7.com/documentation/

Enjoy!

 

Server Messaging made easy

I will talk in this article about a library I made for making easy server side messaging.

It is called ServerMessage and is a library for creating and sending messages on server. By default it saves the messages in DB, but the storage can be changed by implementing the storage interface and passing the new storage when creating the instance.

Also it has a very basic validation that can also be changed in the way described above.

It is a very flexible library, it can be used for a lot of things and in a lot of ways.
A functionality I am proud of is the filtering, currently it has two filter options added, one filters out emails and the other filters out urls. Other filters can be added, too, by implementing the filter interface and passing the filter when filtering.

A very basic example of usage:

include('autoload.php');

// Instantiate the class using explicit values
$message = new ServerMessage(
array(
'db' => array(
'server' => 'localhost',
'user' => 'root',
'pass' => '',
'database' => 'test',
'table_name' => 'messages'
)
)
);

// Check if storage exists and create it if does not
if ( !$message->storage_exists() )
{
$message->install_storage();
}

// Create message
$message->set_subject('Testing message abc@bcd.com');
$message->set_body('Body of the message, also containing filterable things like http://www.filterurl.dev or abcd@efgh.dev');
$message->set_sender(1, 'user');
$message->set_reciever(1, 'support');
$message->set_meta(array('meta_key' => 'meta_value'));
$message->send();

// Now the message is also set in the inner container, thus we can change one thing and send the message again.
$message->set_reciever(2, 'support');
$message->send();

// You can also update the message
$message->set_subject('Changed');
$message->update();

// To reset inner message, you can call
$message->reset_inner_message();

// Filtering messages is easy like
$message->filter_message();

It can be used in a lot of ways. Here is the full list of functions with docx descriptions for reference:

/**
* Constructor.
* @param array $config Can overwrite default config upon creation
* If no storage class is provided, the config default will be used
* @param ServerMessageInterfacesStorage $storage
* @param ServerMessageInterfacesValidation $validation The validation class that validates the message, can be set externally
*/
public function __construct(Array $config = array(), StorageInterface $storage = null, ValidationInterface $validation = null)

public function set_subject($subject = '')

public function set_body($body = '')

public function set_sender($sender_id = 0, $sender_type = '')

public function set_reciever($reciever_id = 0, $reciever_type = '')

public function set_meta($meta)

/**
* Set the status of one or more messages
* @param array|int $id
* @param int $status Must be one from the predefined statuses
* @return boolean
*/
public function change_status($id, $status)

/**
* Set one or more messages to read or unread
* @param array|int $id
* @param boolean $read True or false
* @return boolean
*/
public function set_read($id, $read)

/**
* After the parts of the message have been set, saves to storage
* If validation is set to true in config, validates before sending
* @return boolean
*/
public function send()

/**
* Updates the loaded message in the storage
* It will validate the message if set in config
* @return boolean
*/
public function update()

/**
* If sending was not successful, we get the validation errors
* @return array
*/
public function get_validation_errors()

/**
* Gets the inbox of a given object
* @param int $obj_id The id of the reciever
* @param string $obj_type The type of the reciever
* @param int $status Optional, taken in consideration if not null
* @param int $limit
* @param int $offset
* @return array Returns an array with message objects
*/
public function get_inbox($obj_id, $obj_type, $status = null, $limit = null, $offset = null)

/**
* Gets the number of messages in inbox
* @param int $obj_id The id of the reciever
* @param string $obj_type The type of the reciever
* @param int $status (Optional) Status filter
* @param int $read (Optional) Read filter
* @return int
*/
public function get_total_inbox($obj_id, $obj_type, $status = null, $read = null)

/**
* Gets the outbox of a given object
* @param int $obj_id The id of the sender
* @param string $obj_type The type of the sender
* @param int $status Optional, taken in consideration if not null
* @param int $limit
* @param int $offset
* @return array Returns an array with message objects
*/
public function get_outbox($obj_id, $obj_type, $status = null, $limit = null, $offset = null)

/**
* Gets the number of unapproved messages
* @param int $obj_id The id of the reciever
* @param string $obj_type The type of the reciever
* @param int $status (Optional) Status filter
* @return int
*/
public function get_total_outbox($obj_id, $obj_type, $status = null)

/**
* Get all the messages, pagination dependent
* @param int $limit
* @param int $offset
* @return array
*/
public function get_all($limit = null, $offset = null)

/**
* Get the total number of messages
*/
public function get_total_messages()

/**
* Get a single message by ID or the current message set
* @param int $message_id If null, returns current message
* @return ServerMessageEntityMessage
*/
public function get_single($message_id = null)

/**
* Deletes one or more messages
* @param array | int $message_id
* @return boolean
*/
public function delete_message($message_id)

/**
* Checks if storage exists and can be used
* @return boolean
*/
public function storage_exists()

/**
* Creates storage if that does not exist
*/
public function install_storage()

/**
* Clear out the storage created for messages
*/
public function remove_storage()

/**
* Filters the given message
* @param ServerMessageEntityMessage $message Optional, if not set, the inner message will be used
* @param array $filters Optional, can be used to add filters (Ex. array('facebook' => new FacebookFilter()))
* The filters need to extend the filter interface
* @param boolean $subject_only Filter only the subject
* @param boolean $delete_found Remove the found matches from the message
* @param boolean $save If true, saves the message back to storage (used when deleting threats)
* @return array Returns an associative array with the filtered message and the found matches
*/
public function filter_message(MessageEntity $message = null, Array $filters = array(), $subject_only = false, $delete_found = false, $save = false)

/**
* Return the inner statuses set in config
*/
public function get_statuses()

/**
* We need to reset our message entity sometimes, so we do that with this function
*/
public function reset_inner_message()

The source code can be found here:

https://github.com/atillaordog/ServerMessage

Easy Pagination helper

One of the first things I wrote when I started working in PHP was a nice little Pagination helper class.

It is very easy to use and made my work easier many times.

So here it is a code sample of how to use it:

// First we have to get the total elements

// Create the limit, offset and other useful data first
$pag = Pagination::paginate($total_nr, $current_page, 20);

// Here we get our data using limit, offset

// Build the pagination
$pagination = new Pagination(array(
'total' => $total_nr, // The nmber of all elements
'items_per_page' => $pag['pagination'], // How many items will there be on the page
'base_url' => $url,  // The base url used, this can contain $_GET elements too, they will not be overwritten
'current_page' => $pag['current'], // Current page we are on
'links_to_show' => 3, // How many links to show between the 3 dots
'page_name' => 'sm_page', // Name of the $_GET parameter to use
'first_string' => '<<', // String that is used for the FIRST link
'prev_string' => '<', // string used for the PREVIOUS link
'dots_string' => '...', // String used for the dots between numbers
'next_string' => '>', // String used for the NEXT link
'last_string' => '>>' // String used for the LAST link
));

// Generate the array of pagination links
$pagination_links = $pagination->render();

You can also set the mentioned variables after initialization like:

$pagination->base_url = '';

Note that you have to set all variables before calling render()

 

It can be found here:

https://github.com/atillaordog/Pagination