Browse files

Include Setting Class and files

  • Loading branch information...
1 parent c987681 commit f0d20195531a484e99d2f0cc752a0ab282b41bcc @draco2003 draco2003 committed Nov 18, 2011
Showing with 287 additions and 0 deletions.
  1. +68 −0 inc/classes/Setting.php
  2. +53 −0 inc/views/add_edit_setting.php
  3. +46 −0 inc/views/list_settings.php
  4. +120 −0 setting.php
View
68 inc/classes/Setting.php
@@ -0,0 +1,68 @@
+<?
+class Setting extends fActiveRecord
+{
+ protected function configure()
+ {
+ }
+ static function findAll($filter)
+ {
+
+ return fRecordSet::build(
+ __CLASS__,
+ $filter,
+ array()
+ );
+ }
+
+ static public function getSystem($setting_name) {
+ return Setting::getSetting('system',$setting_name);
+ }
+
+ static public function getUser($setting_name,$user_id=NULL) {
+ return Setting::getSetting('user',$setting_name,$user_id);
+ }
+
+ static public function getSetting($type='system',$setting_name=NULL,$user_id=NULL) {
+ if (!is_null($setting_name)) {
+ if (is_numeric($user_id) && $type == 'user') {
+ $setting = Setting::findAll(array('type' => $type,'name' => $setting_name,'owner_id' => $user_id));
+ //$value = $setting->getValue();
+ } elseif ($type == 'system') {
+ $setting = Setting::findAll(array('type=' => $type,'name=' => $setting_name,'owner_id=' => '0'));
+ // $value = $setting->getValue();
+ }
+ return $setting;
+ } else {
+ return false;
+ }
+ }
+
+ static public function makeURL($type, $setting_type=NULL,$setting_name=NULL,$user_id=NULL)
+ {
+ if (is_object($setting_type)) {
+ $setting_name = $setting_type->getName();
+ $setting_type = 'system';
+ }
+ if (is_null($setting_type)) {
+ $setting_type = 'system';
+ }
+ if (!is_null($user_id)) {
+ $user_id_query = '&user_id=' . $user_id;
+ } else {
+ $user_id_query = '';
+ }
+ switch ($type)
+ {
+ case 'list':
+ return 'setting.php?action=list&setting_type=' . $setting_type . $user_id_query;
+ case 'add':
+ return 'setting.php?action=add&setting_name=' . $setting_name . '&setting_type=' . $setting_type . $user_id_query;
+ case 'edit':
+ return 'setting.php?action=edit&setting_name=' . $setting_name . '&setting_type=' . $setting_type . $user_id_query;
+ case 'delete':
+ return 'setting.php?action=delete&setting_name=' . $setting_name . '&setting_type=' . $setting_type . $user_id_query;
+ }
+ }
+
+
+}
View
53 inc/views/add_edit_setting.php
@@ -0,0 +1,53 @@
+<?
+$page_title = ($action == 'add' ? 'Override setting' : 'Edit Setting');
+$tmpl->set('title', $page_title);
+$breadcrumbs[] = array('name' => 'Settings', 'url' => Setting::makeURL('list'),'active' => false);
+$breadcrumbs[] = array('name' => $page_title, 'url' => fURL::getWithQueryString(),'active'=> true);
+//$tmpl->set('breadcrumbs',$breadcrumbs);
+$tmpl->place('header');
+$query_string = '';
+if (isset($setting_name)) {
+ $query_string = "&setting_name=$setting_name";
+}
+if (isset($user_id)) {
+ $query_string .="&user_id=$user_id";
+}
+if (isset($setting_type)) {
+ $query_string .="&setting_type=$setting_type";
+}
+?>
+ <div class="row">
+ <div class="span6">
+ <form action="<?=fURL::get(); ?>?action=<?=$action.$query_string; ?>" method="post">
+ <div class="main" id="main">
+ <fieldset>
+ <div class="clearfix">
+ <label for="line-friendly_name">Name<em>*</em></label>
+ <div class="input">
+ <?=$setting->encodeFriendlyName(); ?>
+ </div>
+ </div><!-- /clearfix -->
+ <div class="clearfix">
+ <label for="line-value">Value<em>*</em></label>
+ <div class="input">
+ <input id="line-value" class="span3" type="text" size="30" name="value" value="<?=$setting->encodeValue(); ?>" />
+ </div>
+ </div><!-- /clearfix -->
+ <div class="actions">
+ <input class="btn primary" type="submit" value="Save" />
+ <? if($action == 'edit') { ?>
+ <input class="btn" type="submit" name="action::delete" value="Delete" />
+ <? } ?>
+ <div class="required"><em>*</em> Required field</div>
+ <input type="hidden" name="token" value="<?=fRequest::generateCSRFToken(); ?>" />
+ </div>
+ </fieldset>
+ </div>
+ </form>
+ </div>
+ <div class="span10">
+ </div>
+ </div>
+</div>
+<?php
+$tmpl->place('footer');
View
46 inc/views/list_settings.php
@@ -0,0 +1,46 @@
+<?php
+$tmpl->set('title', 'Self Service Alerts based on Graphite metrics');
+$active_tab_alerts = " class=active";
+$tmpl->set('breadcrumbs',$breadcrumbs);
+$tmpl->place('header');
+
+try {
+ ?>
+<table class="zebra-striped">
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Value</th>
+ <th>Default Value</th>
+ </tr></thead><tbody>
+ <?php
+ $first = TRUE;
+ foreach ($list_plugin_settings as $setting_name => $setting) {
+ ?>
+ <tr>
+ <td><?=$setting['friendly_name']; ?></td>
+ <td><?=(isset($setting['value']) ? $setting['value'] : 'Default'); ?></td>
+ <td><?=$setting['default']; ?></td>
+ <td><?php
+ if (!isset($owner_id)) {
+ $owner_id = NULL;
+ }
+ if (isset($setting['value'])) {
+ echo '<a href="' . Setting::makeURL('edit',$setting_type,$setting_name,$owner_id) . '">Update</a> | ';
+ echo '<a href="' . Setting::makeURL('delete',$setting_type,$setting_name,$owner_id) . '">Delete</a>';
+ } else {
+ echo '<a href="' . Setting::makeURL('add',$setting_type,$setting_name,$owner_id) . '">Override</a>';
+ } ?>
+ </td>
+ </tr>
+ <?php } ?>
+ </tbody></table>
+ <?
+} catch (fEmptySetException $e) {
+ ?>
+ <p class="info">No settings? uh-oh</p>
+ <?php
+}
+?>
+</div>
+<?php $tmpl->place('footer') ?>
View
120 setting.php
@@ -0,0 +1,120 @@
+<?php
+include 'inc/init.php';
+
+fAuthorization::requireLoggedIn();
+
+fRequest::overrideAction();
+
+$action = fRequest::getValid('action', array('list', 'add', 'edit', 'delete', 'view'));
+
+$setting_name = fRequest::get('setting_name', 'string');
+$setting_type = fRequest::getValid('setting_type',array('system','user'));
+$user_id = fRequest::get('user_id','integer');
+
+if ($setting_type == 'user') {
+ if ($user_id > 0) {
+ $owner_id = $user_id;
+ } else {
+ $owner_id = fSession::get('user_id');
+ }
+} else {
+ $owner_id = 0;
+}
+
+if ('delete' == $action) {
+ $class_name = 'Setting';
+ try {
+ $obj = new Setting(array('name' => $setting_name,'owner_id' => $owner_id));
+ $delete_text = 'Are you sure you want to delete this setting : <strong>' . $obj->getFriendlyName() . '</strong>?';
+ if (fRequest::isPost()) {
+ fRequest::validateCSRFToken(fRequest::get('token'));
+ $obj->delete();
+ fMessaging::create('success', fURL::get(),
+ 'The setting ' . $obj->getFriendlyName() . ' was successfully deleted');
+ fURL::redirect(Setting::makeUrl('list',$setting_type,NULL,$owner_id));
+ }
+ } catch (fNotFoundException $e) {
+ fMessaging::create('error', fURL::get(),
+ 'The setting requested could not be found');
+ fURL::redirect(Setting::makeUrl('list'));
+ } catch (fExpectedException $e) {
+ fMessaging::create('error', fURL::get(), $e->getMessage());
+ }
+
+ include VIEW_PATH . '/delete.php';
+
+// --------------------------------- //
+} elseif ('edit' == $action) {
+ try {
+ $setting = new Setting(array('name' => $setting_name,'owner_id' => $owner_id));
+ if (fRequest::isPost()) {
+ $setting->populate();
+ fRequest::validateCSRFToken(fRequest::get('token'));
+ $setting->store();
+
+ fMessaging::create('affected', fURL::get(), $setting->getFriendlyName());
+ fMessaging::create('success', fURL::get(),
+ 'The setting ' . $setting->getFriendlyName(). ' was successfully updated');
+ fURL::redirect(Setting::makeURL('list',$setting_type,NULL,$owner_id));
+ }
+ } catch (fNotFoundException $e) {
+ fMessaging::create('error', fURL::get(),
+ 'The Setting requested, ' . fHTML::encode($setting_name) . ', could not be found');
+ fURL::redirect(Setting::makeUrl('list'));
+ } catch (fExpectedException $e) {
+ fMessaging::create('error', fURL::get(), $e->getMessage());
+ }
+
+ include VIEW_PATH . '/add_edit_setting.php';
+
+// --------------------------------- //
+} elseif ('add' == $action) {
+ $setting = new Setting();
+ if ('user' == $setting_type) {
+ $list_plugin_settings = $plugin_user_settings;
+ } else {
+ $list_plugin_settings = $plugin_settings;
+ }
+ if (!array_key_exists($setting_name,$list_plugin_settings)) {
+ $setting_name = '';
+ }
+ $setting->setFriendlyName($list_plugin_settings[$setting_name]['friendly_name']);
+ $setting->setName($setting_name);
+ $setting->setPlugin('email');
+ if ($setting_type == 'user') {
+ $setting->setOwnerId($user_id);
+ $setting->setType('user');
+ } else {
+ $setting->setOwnerId(0);
+ }
+ if (fRequest::isPost()) {
+ try {
+ $setting->populate();
+ fRequest::validateCSRFToken(fRequest::get('token'));
+ $setting->store();
+ $setting_url = Setting::makeUrl('list',$setting_type);
+ fMessaging::create('affected', fURL::get());
+ fMessaging::create('success', fURL::get(),
+ 'The setting ' . $setting->getFriendlyName() . ' was successfully created');
+ fURL::redirect($setting_url);
+ } catch (fExpectedException $e) {
+ fMessaging::create('error', fURL::get(), $e->getMessage());
+ }
+ } else {
+ $setting->setValue($list_plugin_settings[$setting_name]['default']);
+ }
+ include VIEW_PATH . '/add_edit_setting.php';
+
+} else {
+ if ('user' == $setting_type) {
+ $current_plugin_user_settings = Setting::findAll(array('type=' => 'user','owner_id=' => $owner_id));
+ foreach ($current_plugin_user_settings as $user_setting) {
+ $plugin_user_settings[$user_setting->getName()]['value'] = $user_setting->getValue();
+ }
+ $list_plugin_settings = $plugin_user_settings;
+ } else {
+ $list_plugin_settings = $plugin_settings;
+ }
+include VIEW_PATH . '/list_settings.php';
+
+}

0 comments on commit f0d2019

Please sign in to comment.