Jump to content

Simple Module for Session messages


Harmster
 Share

Recommended Posts

Hey,

I've made this really simple session module that lets to add messages to the session and render it.

Here's the complete php file:

<?php
class SessionMessage extends Process implements Module, ConfigurableModule {

    public $messages = array();

    public static function getModuleInfo() {

        return array(
            'title' => 'SessionMessage',
            'version' => 101,
            'summary' => 'An example module used for demonstration purposes. See the /site/modules/Helloworld.module file for details.',
            'href' => 'http://www.processwire.com',
            'singular' => true,
            'autoload' => true,
        );
    }
    public function init() {
        $this->setFuel('sessionMessage', $this);
    }
    static public function getModuleConfigInputfields(array $data){
        $modules = Wire::getFuel('modules');
        $fields = new InputfieldWrapper();
        $field = $modules->get("InputfieldText");
        $field->attr('name+id', 'error_class');
        $field->attr('value', $data['error_class']);
        $field->label = "Error class";
        $field->description = 'Enter your error class for error handling.';
        $fields->append($field);
        $field = $modules->get("InputfieldText");
        $field->attr('name+id', 'success_class');
        $field->attr('value', $data['success_class']);
        $field->label = "Success class";
        $field->description = 'Enter your error class for success handling.';
        $fields->append($field);
        $field = $modules->get("InputfieldText");
        $field->attr('name+id', 'alert_class');
        $field->attr('value', $data['alert_class']);
        $field->label = "Alert class";
        $field->description = 'Enter your error class for alert handling.';
        $fields->append($field);
        return $fields;
    }
    //void
    public function add($type, $message){
        //types could be error, alert or success
        array_push($this->messages, array($type => $message));
        $this->session->set('messages', $this->messages);
    }
    public function render($clean=true){
        $html = "";
        $this->messages = $this->session->get("messages");
        foreach($this->messages as $array){
            foreach($array as $type => $message){
                $class = $type.'_class';
                $html .= '<div class="' . $this->$class . '">';
                $html .= $message;
                $html .'</div>';
            }
        }
        $this->session->messages = $this->messages;
        if($clean == true){
            unset($this->messages);
            $this->session->set('messages', $this->messages);
        }
        return $html;
    }

}

Installation:

1) Install the module

2) Give classes to each of the default fields:

  • error
  • success
  • alert

Usage:

To add a message use:

$sessionMessage->add('error', 'This is an error message');

To display all messages:

echo $sessionMessage->render();

If you don't want the module to erase the messages after being displayed:

echo $sessionMessage->render(false);

I am currently using this in combination with Zurb Foundation

Hope it helps someone.

-Harmster

  • Like 1
Link to comment
Share on other sites

Harmster,

Thanks for the module :)

Just wanted to note that Pw has built in function for setting session messages:

// Set message for next request
$session->message('this message is accessible in the next request');

// or Error...
$session->error('error message');

And check for messages could be done like this:

if (count(wire('notices'))) {
  foreach (wire('notices') as $notice) {
    echo $notice->text;
    if ($notice instanceof NoticeError) echo "was error notice";
  }
}

Edit: Just saw that you extend Process. This is only needed for Modules that execute a process in the admin of Pw. I think this is not the case for your module, so extending WireData would be enough.

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

Nice module you've put together there Yannick. Only thing I want to mention is that ProcessWire already has a $log API variable (see here) in the dev branch. The syntax of the PW $log API variable is very similar to yours, though it also supports writing to custom logs, but lacks the render() method that you have.  

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...