All pastes #1809743 Raw Edit

Age plugin for StatusNet

public php v1 · immutable
#1809743 ·published 2010-02-25 15:24 UTC
rendered paste body
<?phpclass AgePlugin extends Plugin{    function onCheckSchema() {        $schema = Schema::get();        $schema->ensureTable('extra_fields',                             array(new ColumnDef('user_id', 'integer',                                                 null, false, 'MUL'),                                   new ColumnDef('age', 'integer')));        return true;    }    function onEndRegistrationFormData($action)    {        $this->_addAgeInput($action);        return true;    }    function onStartRegistrationTry($action)    {        return $this->_validateAge($action);    }    function onEndRegistrationTry($action)    {        $age = $action->trimmed('user_age');        $user = common_current_user();        $this->_saveAge($user, $age);        return true;    }    function onEndProfileFormData($action)    {        $age = $action->trimmed('user_age');        if (empty($age)) {            $user = common_current_user();            $extra = Extra_fields::staticGet('user_id', $user->id);            if (!empty($extra)) {                $age = $extra->age;            }        }        $this->_addAgeInput($action, $age);        return true;    }    function onStartProfileSaveForm($action)    {        return $this->_validateAge($action);    }    function onEndProfileSaveForm($action)    {        $age = $action->trimmed('user_age');        $user = common_current_user();        $this->_saveAge($user, $age);    }    function onEndProfileListItemProfileElements($listitem)    {        $extra = Extra_fields::staticGet('user_id', $listitem->profile->id);        if (!empty($extra)) {            $listitem->out->element('p', 'age', sprintf(_('%d years old'), $extra->age));        }        return true;    }    function onEndProfilePageProfileElements(&$out, $profile)    {        $extra = Extra_fields::staticGet('user_id', $profile->id);        if (!empty($extra)) {            $out->element('p', 'age', sprintf(_('%d years old'), $extra->age));        }        return true;    }    function onUserDeleteRelated($user, &$related) {        $related[] = 'extra_fields';        return true;    }    function _addAgeInput($action, $age=null)    {        $action->elementStart('li');        $action->input('user_age', _('Age'), $age, _('Your age in years'));        $action->elementEnd('li');    }    function _validateAge($action)    {        $age = $action->trimmed('user_age');        if (!is_numeric($age)) {            throw new ClientException(_('Age has to be a number'));        }        return true;    }    function _saveAge($user, $age)    {        $extra = Extra_fields::staticGet('user_id', $user->id);        if (empty($extra)) {            $extra = new Extra_fields();            $extra->user_id = $user->id;            $extra->age     = $age;            $extra->insert();        } else {            $orig = clone($extra);            $extra->age = $age;            $extra->update();        }    }}class Extra_fields extends Memcached_DataObject{    public $__table = 'extra_fields';                     // table name    public $user_id;                         // int(4)   not_null    public $age;    /* Static get */    function staticGet($k,$v=null)    { return Memcached_DataObject::staticGet('Extra_fields',$k,$v); }    function table()    {        $db = $this->getDatabaseConnection();        return array('user_id'   => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,                     'age'   => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL);    }    function keys()    {        return array('user_id' => 'K');    }}