From: Andrey Kutejko Date: Wed, 3 Sep 2014 05:54:27 +0000 (+0300) Subject: cleanup X-Git-Tag: 0.6~148 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=031d9a611c71cd516a449b6d79eec58f2e10d9aa;p=ipf.git cleanup --- diff --git a/ipf.php b/ipf.php index 96378fc..57d8f96 100644 --- a/ipf.php +++ b/ipf.php @@ -17,6 +17,8 @@ final class IPF 'dir_permission' => 0777, 'file_permission' => 0666, 'time_zone' => 'America/Toronto', + 'tmp' => '/tmp', + 'admin_title' => 'IPF Administration', ); private static function applySettings($settings) @@ -30,46 +32,32 @@ final class IPF $settings_file = IPF::$settings['project_path'].DIRECTORY_SEPARATOR.'settings.php'; IPF::$settings['settings_file'] = $settings_file; - if (file_exists($settings_file)) - IPF::applySettings(require $settings_file); - else - throw new IPF_Exception_Settings('Configuration file does not exist: '.$settings_file); + if (!file_exists($settings_file)) + die('Configuration file does not exist: '.$settings_file); + + IPF::applySettings(require $settings_file); $settings_local_file = IPF::$settings['project_path'].DIRECTORY_SEPARATOR.'settings_local.php'; if (file_exists($settings_local_file)) IPF::applySettings(require $settings_local_file); - if (!isset(IPF::$settings['dsn']) && !isset(IPF::$settings['database'])) - throw new IPF_Exception_Settings('Please specify database parameters or DSN in settings file'); - - if (isset(IPF::$settings['database']) && !is_array(IPF::$settings['database'])) - throw new IPF_Exception_Settings('Database must be array with keys: driver, host, port (optional), database, username, password, options (optional)'); + if (!isset(IPF::$settings['database'])) + die('Please specify database parameters in settings file'); - if (isset(IPF::$settings['dsn']) && !is_string(IPF::$settings['dsn'])) - throw new IPF_Exception_Settings('DSN must be string'); + if (!is_array(IPF::$settings['database'])) + die('Database must be array with keys: driver, host, port (optional), database, username, password'); - if (!isset(IPF::$settings['tmp'])) - IPF::$settings['tmp'] = '/tmp'; - else - if (!is_string(IPF::$settings['tmp'])) - throw new IPF_Exception_Settings('TMP must be string'); + if (!is_string(IPF::$settings['tmp'])) + die('TMP must be string'); if (!isset(IPF::$settings['applications'])) - throw new IPF_Exception_Settings('Please specify application list'); - if (!is_array(IPF::$settings['applications'])) - throw new IPF_Exception_Settings('applications must be array of string'); - - if (!isset(IPF::$settings['admin_title'])){ - IPF::$settings['admin_title'] = 'IPF Administration'; - } + die('Please specify application list'); - if (!isset(IPF::$settings['tiny_mce_url'])){ - IPF::$settings['tiny_mce_url'] = IPF::$settings['static_url'] . 'admin/tiny_mce/'; - } + if (!is_array(IPF::$settings['applications'])) + die('applications must be array of string'); - if (!isset(IPF::$settings['urls'])){ - throw new IPF_Exception_Settings('Specify site url routes'); - } + if (!isset(IPF::$settings['urls'])) + die('Specify site url routes'); } private static function requestedFileExists() @@ -88,12 +76,9 @@ final class IPF IPF::$settings['project_path'] = $project_path; IPF::$settings['document_root'] = $document_root; - try { - IPF::loadSettings(); - date_default_timezone_set(IPF::$settings['time_zone']); - } catch (IPF_Exception_Settings $e) { - die('Setting Error: '.$e->getMessage()."\n"); - } + IPF::loadSettings(); + date_default_timezone_set(IPF::$settings['time_zone']); + return true; } diff --git a/ipf/exception/form.php b/ipf/exception/form.php deleted file mode 100644 index ab8a8ec..0000000 --- a/ipf/exception/form.php +++ /dev/null @@ -1,3 +0,0 @@ -data = $data; - $this->is_bound = true; - } - $this->initial = \PFF\Arr::get($extra, 'initial', array()); - $this->initFields($extra); - } - - abstract protected function initFields($extra=array()); - - function addPrefix($field_name) - { - if ('' !== $this->prefix) { - return $this->prefix.$field_name; - } - return $field_name; - } - - function isValid() - { - if (!$this->is_bound) - return false; - - if ($this->is_valid !== null) - return $this->is_valid; - - $this->cleaned_data = array(); - $this->errors = array(); - $form_methods = get_class_methods($this); - - foreach ($this->fields as $name => $field) { - $value = $this->field($name)->value(); - try { - $value = $field->clean($value); - $this->cleaned_data[$name] = $value; - if (in_array('clean_'.$name, $form_methods)) { - $m = 'clean_'.$name; - $value = $this->$m(); - $this->cleaned_data[$name] = $value; - } - } catch (IPF_Exception_Form $e) { - if (!isset($this->errors[$name])) - $this->errors[$name] = array(); - $this->errors[$name][] = $e->getMessage(); - - if (isset($this->cleaned_data[$name])) { - unset($this->cleaned_data[$name]); - } - } - } - - if (empty($this->errors)) { - try { - $this->cleaned_data = $this->clean(); - } catch (IPF_Exception_Form $e) { - if (!isset($this->errors['__all__'])) $this->errors['__all__'] = array(); - $this->errors['__all__'][] = $e->getMessage(); - } - } - - if (empty($this->errors)) { - $this->is_valid = true; - return true; - } else { - // as some errors, we do not have cleaned data available. - $this->cleaned_data = array(); - $this->is_valid = false; - return false; - } - } - - public function clean() - { - return $this->cleaned_data; - } - - public function initial($name) - { - return \PFF\Arr::get($this->initial, $name, null); - } - - public function hiddenFields() - { - $hidden = array(); - foreach ($this->fields as $name => $field) - if ($field->widget->is_hidden) - $hidden[] = $name; - return $hidden; - } - - public function render_top_errors($raw=false) - { - $errors = IPF_Form::renderErrorsAsHTML($this->get_top_errors()); - if ($raw) - return $errors; - else - return $this->unescape($errors); - } - - public function get_top_errors() - { - return \PFF\Arr::get($this->errors, '__all__', array()); - } - - public function renderLayout($layout, $raw=false) - { - foreach ($this->field_groups as $field_group) { - if (!$field_group['fields']) - throw new IPF_Exception('Empty field group.'); - foreach ($field_group['fields'] as $field_name) - if (!array_key_exists($field_name, $this->fields)) - throw new IPF_Exception('Unknown field "' . $field_name . '".'); - } - - $groups = array(); - foreach ($this->field_groups as $field_group) { - $_fields = array(); - foreach ($field_group['fields'] as $field_name) - $_fields[$field_name] = $this->fields[$field_name]; - $groups[] = array( - 'fields' => $_fields, - 'label' => @$field_group['label'], - ); - } - - if (!count($groups)) { - $groups = array(array('fields' => $this->fields)); - } - - $output = $layout->startForm($this); - foreach ($groups as $group) { - $groupLabel = \PFF\Arr::get($group, 'label', ''); - $output .= $layout->startGroup($groupLabel); - - foreach ($group['fields'] as $name=>$field) { - if ($field->widget->is_hidden) - continue; - - $bf = $this->field($name); - - $output .= $layout->field($bf); - } - $output .= $layout->endGroup($groupLabel); - } - $output .= $layout->endForm($this); - - if (!$raw) - $output = $this->unescape($output); - - return $output; - } - - public function extra_js() - { - $extra_js = array(); - foreach ($this->fields as $name => $field) - $extra_js = array_merge($extra_js, $field->widget->extra_js()); - return array_unique($extra_js); - } - - public function render_p($raw=false) - { - return $form->renderLayout(new IPF_Form_ParagraphLayout, $raw); - } - - public function render_ul($raw=false) - { - return $form->renderLayout(new IPF_Form_ListLayout, $raw); - } - - public function render_table($raw=false) - { - return $form->renderLayout(new IPF_Form_TableLayout, $raw); - } - - public function render_admin($raw=false) - { - return $form->renderLayout(new IPF_Admin_Form_Layout, $raw); - } - - function __get($prop) - { - if (!in_array($prop, array('render_p', 'render_ul', 'render_table', 'render_top_errors', 'get_top_errors'))) { - return $this->$prop; - } - return $this->$prop(); - } - - public function field($key) - { - return new IPF_Form_BoundField($this, $this->fields[$key], $key); - } - - public function current() - { - $name = key($this->fields); - return $this->field($name); - } - - public function key() - { - return key($this->fields); - } - - public function next() - { - next($this->fields); - } - - public function rewind() - { - reset($this->fields); - } - - public function valid() - { - return (false !== current($this->fields)); - } - - public static function renderErrorsAsHTML($errors) - { - if (!count($errors)) - return ''; - $ul = Tag::ul(array('class' => 'errorlist')); - foreach ($errors as $err) - $ul->append(Tag::li(null, $err)); - return $ul->html(); - } - - public function unescape($html) - { - // Do nothing - return $html; - } -} - diff --git a/ipf/form/exception.php b/ipf/form/exception.php new file mode 100644 index 0000000..ab8a8ec --- /dev/null +++ b/ipf/form/exception.php @@ -0,0 +1,3 @@ +data = $data; + $this->is_bound = true; + } + $this->initial = \PFF\Arr::get($extra, 'initial', array()); + $this->initFields($extra); + } + + abstract protected function initFields($extra=array()); + + function addPrefix($field_name) + { + if ('' !== $this->prefix) { + return $this->prefix.$field_name; + } + return $field_name; + } + + function isValid() + { + if (!$this->is_bound) + return false; + + if ($this->is_valid !== null) + return $this->is_valid; + + $this->cleaned_data = array(); + $this->errors = array(); + $form_methods = get_class_methods($this); + + foreach ($this->fields as $name => $field) { + $value = $this->field($name)->value(); + try { + $value = $field->clean($value); + $this->cleaned_data[$name] = $value; + if (in_array('clean_'.$name, $form_methods)) { + $m = 'clean_'.$name; + $value = $this->$m(); + $this->cleaned_data[$name] = $value; + } + } catch (IPF_Exception_Form $e) { + if (!isset($this->errors[$name])) + $this->errors[$name] = array(); + $this->errors[$name][] = $e->getMessage(); + + if (isset($this->cleaned_data[$name])) { + unset($this->cleaned_data[$name]); + } + } + } + + if (empty($this->errors)) { + try { + $this->cleaned_data = $this->clean(); + } catch (IPF_Exception_Form $e) { + if (!isset($this->errors['__all__'])) $this->errors['__all__'] = array(); + $this->errors['__all__'][] = $e->getMessage(); + } + } + + if (empty($this->errors)) { + $this->is_valid = true; + return true; + } else { + // as some errors, we do not have cleaned data available. + $this->cleaned_data = array(); + $this->is_valid = false; + return false; + } + } + + public function clean() + { + return $this->cleaned_data; + } + + public function initial($name) + { + return \PFF\Arr::get($this->initial, $name, null); + } + + public function hiddenFields() + { + $hidden = array(); + foreach ($this->fields as $name => $field) + if ($field->widget->is_hidden) + $hidden[] = $name; + return $hidden; + } + + public function render_top_errors($raw=false) + { + $errors = IPF_Form::renderErrorsAsHTML($this->get_top_errors()); + if ($raw) + return $errors; + else + return $this->unescape($errors); + } + + public function get_top_errors() + { + return \PFF\Arr::get($this->errors, '__all__', array()); + } + + public function renderLayout($layout, $raw=false) + { + foreach ($this->field_groups as $field_group) { + if (!$field_group['fields']) + throw new IPF_Exception('Empty field group.'); + foreach ($field_group['fields'] as $field_name) + if (!array_key_exists($field_name, $this->fields)) + throw new IPF_Exception('Unknown field "' . $field_name . '".'); + } + + $groups = array(); + foreach ($this->field_groups as $field_group) { + $_fields = array(); + foreach ($field_group['fields'] as $field_name) + $_fields[$field_name] = $this->fields[$field_name]; + $groups[] = array( + 'fields' => $_fields, + 'label' => @$field_group['label'], + ); + } + + if (!count($groups)) { + $groups = array(array('fields' => $this->fields)); + } + + $output = $layout->startForm($this); + foreach ($groups as $group) { + $groupLabel = \PFF\Arr::get($group, 'label', ''); + $output .= $layout->startGroup($groupLabel); + + foreach ($group['fields'] as $name=>$field) { + if ($field->widget->is_hidden) + continue; + + $bf = $this->field($name); + + $output .= $layout->field($bf); + } + $output .= $layout->endGroup($groupLabel); + } + $output .= $layout->endForm($this); + + if (!$raw) + $output = $this->unescape($output); + + return $output; + } + + public function extra_js() + { + $extra_js = array(); + foreach ($this->fields as $name => $field) + $extra_js = array_merge($extra_js, $field->widget->extra_js()); + return array_unique($extra_js); + } + + public function render_p($raw=false) + { + return $form->renderLayout(new IPF_Form_ParagraphLayout, $raw); + } + + public function render_ul($raw=false) + { + return $form->renderLayout(new IPF_Form_ListLayout, $raw); + } + + public function render_table($raw=false) + { + return $form->renderLayout(new IPF_Form_TableLayout, $raw); + } + + public function render_admin($raw=false) + { + return $form->renderLayout(new IPF_Admin_Form_Layout, $raw); + } + + function __get($prop) + { + if (!in_array($prop, array('render_p', 'render_ul', 'render_table', 'render_top_errors', 'get_top_errors'))) { + return $this->$prop; + } + return $this->$prop(); + } + + public function field($key) + { + return new IPF_Form_BoundField($this, $this->fields[$key], $key); + } + + public function current() + { + $name = key($this->fields); + return $this->field($name); + } + + public function key() + { + return key($this->fields); + } + + public function next() + { + next($this->fields); + } + + public function rewind() + { + reset($this->fields); + } + + public function valid() + { + return (false !== current($this->fields)); + } + + public static function renderErrorsAsHTML($errors) + { + if (!count($errors)) + return ''; + $ul = Tag::ul(array('class' => 'errorlist')); + foreach ($errors as $err) + $ul->append(Tag::li(null, $err)); + return $ul->html(); + } + + public function unescape($html) + { + // Do nothing + return $html; + } +} + diff --git a/ipf/form/widget/htmlinput.php b/ipf/form/widget/htmlinput.php index b255a53..197d998 100644 --- a/ipf/form/widget/htmlinput.php +++ b/ipf/form/widget/htmlinput.php @@ -13,7 +13,10 @@ class IPF_Form_Widget_HTMLInput extends IPF_Form_Widget public function __construct($attrs=array()) { - $this->tinymce_url = \PFF\Arr::pop($attrs, 'tinymce_url'); + $this->tinymce_url = \PFF\Arr::pop($attrs, 'tinymce_url', + IPF::get('tiny_mce_url', + IPF::get('static_url').'admin/tiny_mce/')); + $this->mode = \PFF\Arr::pop($attrs, 'mode', 'textareas'); $this->theme = \PFF\Arr::pop($attrs, 'theme', 'simple'); $this->include_tinymce = \PFF\Arr::pop($attrs, 'include_tinymce', true); @@ -51,7 +54,7 @@ class IPF_Form_Widget_HTMLInput extends IPF_Form_Widget public function extra_js() { return array( - '', + '', '