2017 © Pedro Peláez
 

library prefs

Store and retrieve user preferences

image

drey/prefs

Store and retrieve user preferences

  • Saturday, March 19, 2016
  • by drey
  • Repository
  • 1 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Prefs

Store and retrieve user preferences., (*1)

  1. Remember form default values according to the last submit (for instance, repeat the date limits or the selection of an account in a dropdown list, according to the last search).
  2. Customize the look and feel of a website.
  3. etc.

Usage

Database in file system

    use drey\Prefs\Factory;

    # Obtain a Prefs object
    # with file system storage and username
    $prefs = Factory::fileSystem('/path/to/directory','bob') 

    # set preference "color" for current user (bob)
    $prefs->set('color','red');

    # get color preference of current user
    $color = $prefs->get('color');

    # $color is set to 'red'

Database in a RDMB

Change the DB medium accordingly, using a pdo factory., (*2)

    use drey\Prefs\Factory;

    # obtain a PDO object
    $pdo = myConnect();

    # Obtain a Prefs object
    # with RDBM storage and username
    $prefs = Factory::pdo($pdo,'bob') 

    # set preference "color" for current user (bob)
    $prefs->set('color','red');

    # get color preference of current user
    $color = $prefs->get('color');

    # $color is set to 'red'

The table schema for MySQL should be (see directory sql/):, (*3)

        CREATE TABLE `prefs`  (
          `name` varchar(255) NOT NULL,
          `value` varchar(255) NOT NULL,
          `username` varchar(32) NOT NULL,
          PRIMARY KEY (`name`,`username`),
          KEY (`username`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Default values

If key is not found, a default value can be provided., (*4)

    $bird = $prefs->get('bird','eagle');

    # $bird is set to 'eagle' if key 'bird' not found

Specify preferences for other users

To assign/read the preferences of other users:, (*5)

    $prefs->set('fruit','lemmon','alice');
    # ...
    $prefs->get('fruit','pear','alice')

Global preferences

A global username could be used to handle preferences for all users. All users must agree on the global username (for instance '*')., (*6)

    # one user
    $prefs->set('closing_date',$form->closing_date,'*');

    (...)

    # other users
    $closing_date = $prefs->get('closing_date',date('Y-m-d'),'*');


Populate form fields:

Populate fields with default values if a field in our model has not any value and we are creating a new record., (*7)

    # before showing the form get the last value entered
    if ($model->isNewRecord && !$model->date) {
        $model->date = $prefs->get('invoice_date',date('Y-m-d'));
    }

    # after post, update value entered
    if (post) {
        $prefs->set('invoice_date',$model->date);
    }

Installing with composer

The package exists in Packagist repository as drey/prefs., (*8)

See drey/prefs., (*9)

The Versions

19/03 2016

dev-master

9999999-dev

Store and retrieve user preferences

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Demetrio Rey

19/03 2016

1.0.2

1.0.2.0

Store and retrieve user preferences

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Demetrio Rey

19/03 2016

1.0.1

1.0.1.0

Store and retrieve user preferences

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Demetrio Rey

04/05 2014

1.0.0

1.0.0.0

Store and retrieve user preferences

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Demetrio Rey