InDefero

Sign in or create your account | Project List | Help

InDefero Git Source Tree

Root/src/IDF/Conf.php

1<?php
2/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4# ***** BEGIN LICENSE BLOCK *****
5# This file is part of InDefero, an open source project management application.
6# Copyright (C) 2008 Céondo Ltd and contributors.
7#
8# InDefero is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# InDefero is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21#
22# ***** END LICENSE BLOCK ***** */
23
24/**
25 * Configuration of a project.
26 *
27 * It is just storing a list of key/value
28 * pairs. We can that way store quite a lot of data.
29 */
30class IDF_Conf extends Pluf_Model
31{
32    public $_model = __CLASS__;
33    public $datacache = null;
34    public $f = null;
35    protected $_project = null;
36
37    function init()
38    {
39        $this->_a['table'] = 'idf_conf';
40        $this->_a['model'] = __CLASS__;
41        $this->_a['cols'] = array(
42                             // It is mandatory to have an "id" column.
43                            'id' =>
44                            array(
45                                  'type' => 'Pluf_DB_Field_Sequence',
46                                  //It is automatically added.
47                                  'blank' => true,
48                                  ),
49                            'project' =>
50                            array(
51                                  'type' => 'Pluf_DB_Field_Foreignkey',
52                                  'model' => 'IDF_Project',
53                                  'blank' => false,
54                                  'verbose' => __('project'),
55                                  ),
56                            'vkey' =>
57                            array(
58                                  'type' => 'Pluf_DB_Field_Varchar',
59                                  'blank' => false,
60                                  'size' => 50,
61                                  'verbose' => __('key'),
62                                  ),
63                            'vdesc' =>
64                            array(
65                                  'type' => 'Pluf_DB_Field_Text',
66                                  'blank' => false,
67                                  'verbose' => __('value'),
68                                  ),
69                            );
70        $this->_a['idx'] = array('project_vkey_idx' =>
71                                 array(
72                                       'col' => 'project, vkey',
73                                       'type' => 'unique',
74                                       ),
75                                 );
76        $this->f = new IDF_Config_DataProxy($this);
77    }
78
79    function setProject($project)
80    {
81        $this->datacache = null;
82        $this->_project = $project;
83    }
84
85    function initCache()
86    {
87        $this->datacache = new ArrayObject();
88        $sql = new Pluf_SQL('project=%s', $this->_project->id);
89        foreach ($this->getList(array('filter' => $sql->gen())) as $val) {
90            $this->datacache[$val->vkey] = $val->vdesc;
91        }
92    }
93
94    /**
95     * FIXME: This is not efficient when setting a large number of
96     * values in a loop.
97     */
98    function setVal($key, $value)
99    {
100        if (!is_null($this->getVal($key, null))
101            and $value == $this->getVal($key)) {
102            return;
103        }
104        $this->delVal($key, false);
105        $conf = new IDF_Conf();
106        $conf->project = $this->_project;
107        $conf->vkey = $key;
108        $conf->vdesc = $value;
109        $conf->create();
110        $this->initCache();
111    }
112
113    function getVal($key, $default='')
114    {
115        if ($this->datacache === null) {
116            $this->initCache();
117        }
118        return (isset($this->datacache[$key])) ? $this->datacache[$key] : $default;
119    }
120
121    function delVal($key, $initcache=true)
122    {
123        $gconf = new IDF_Conf();
124        $sql = new Pluf_SQL('vkey=%s AND project=%s', array($key, $this->_project->id));
125        foreach ($gconf->getList(array('filter' => $sql->gen())) as $c) {
126            $c->delete();
127        }
128        if ($initcache) {
129            $this->initCache();
130        }
131    }
132}
133

Archive Download this file

Branches:
dev
develop
master
newdiff
svn

Tags:
v1.0