BasecsSettings.class.php 3.65 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php

/**
 * BasecsSettings
 *
 * @package
 * @version $id$
 * @copyright 2006-2007 Brent Shaffer
 * @author Brent Shaffer <bshaffer@centresource.com>
 * @license See LICENSE that came packaged with this software
 */
class BasecsSettings
{
  static  $cache = false;
    
  static function getDefaultUploadPath()
  {
    return 'uploads/setting';
  }

  static function isAuthenticated($user = null)
  {
    if (!$user)
    {
      $user = sfContext::getInstance()->getUser();
    }

    $authMethod = sfConfig::get('app_csSettingsPlugin_authMethod');
    $authCredential = sfConfig::get('app_csSettingsPlugin_authCredential');

    $hasAccess = false;
    if ($authMethod)
    {
      $hasAccess = $user->$authMethod();
    }
    if (!$hasAccess && $authCredential)
    {
      $hasAccess = $user->hasCredential($authCredential);
    }

    return $hasAccess;
  }

  /**
   * get
   * Returns the string value of a particular setting.
   *
   * @param string $setting
   * @static
   * @access public
   * @return string
   */
  static function get($setting, $default = null)
  {
    // Pull from cached settings array
    $cache = self::buildCache('settings_array');
    
    if($cache->has('settings_array_'.$setting))
    {
      return unserialize($cache->get('settings_array_'.$setting));
    }

    //Look in app.ymls for setting
    return sfConfig::get('app_'.self::settingize($setting), $default);
  }
  
  static function buildCache($type)
  {
    $cache_handler = self::getCache();

    if(!$cache_handler->get($type.'_cs_cache_is_built', false))
    {
      foreach (Doctrine::getTable('csSetting')->findAll() as $setting)
      {
        if($type == 'settings_array')
        {
          $cache_handler->set($type.'_'.$setting['slug'], serialize($setting->getValue()));
          $cache_handler->set($type.'_'.$setting['name'], serialize($setting->getValue()));
        }
        elseif( $type == 'settings_object')
        {
          $cache_handler->set($type.'_'.$setting['slug'], serialize($setting->toArray()));
          $cache_handler->set($type.'_'.$setting['name'], serialize($setting->toArray()));
        }
      }
      
      $cache_handler->set($type.'_cs_cache_is_built', true);
    }
    
    return $cache_handler;
  }

  /**
   * getSetting
   * pulls the csSetting object for a given setting
   *
   * @param string $setting
   * @static
   * @access public
   * @return object csSetting
   */
  static function getSetting($setting)
  {

    if(strlen(trim($setting)) == 0)
    {
      throw new sfException('[f6Settings::getSetting] invalid name');
    }
    
    $cache = self::buildCache('settings_object');
    
    if ($cache->has('settings_object_'.$setting))
    {
      $ret = new csSetting();
      $ret->fromArray(unserialize($cache->get('settings_object_'.$setting, serialize(array()))));
      
      $value = unserialize($cache->get('settings_object_'.$setting));
      if(!is_array($value))
      {
        return null;
      }
      
      $ret->toArray($value);
      $ret->assignIdentifier($value['id']);
      
      return $ret;
    }

    return null;
  }

  static public function clearSettingsCache()
  {
    
    self::getCache()->clean(); 
  }

  static public function getCache()
  {
    if(!self::$cache)
    {
      $cache_settings = sfConfig::get('app_csSettingsPlugin_cache', array(
        'class'   => 'sfNoCache',
        'options' => array()
      ));
      
      $class    = $cache_settings['class'];
      $options  = $cache_settings['options'];

      self::$cache = new $class($options);
    }
    
    return self::$cache;
  }

  static public function settingize($anystring)
  {
    return str_replace('-', '_', Doctrine_Inflector::urlize(trim($anystring)));
  }
}