Modified:
/framework/trunk/app/core/SwitchYard_Config.class.php
/framework/trunk/app/core/SwitchYard_Controller.class.php
/framework/trunk/app/core/SwitchYard_Doctrine.class.php
/framework/trunk/app/core/SwitchYard_Doctrine_Pager_Layout.class.php
/framework/trunk/app/core/SwitchYard_Form.class.php
/framework/trunk/index.php
=======================================
--- /framework/trunk/app/core/SwitchYard_Config.class.php Thu Oct 1
20:17:39 2009
+++ /framework/trunk/app/core/SwitchYard_Config.class.php Mon Oct 5
18:52:47 2009
@@ -96,11 +96,7 @@
*/
public static function value_to_boolean($value)
{
- $value = strtolower($value);
- if ($value == 'y' or $value == 'yes' or $value == 't' or $value
== 'true' or $value == 1) {
- return true;
- }
- return false;
+ return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? true : false ;
}
}
=======================================
--- /framework/trunk/app/core/SwitchYard_Controller.class.php Thu Jun 18
18:29:32 2009
+++ /framework/trunk/app/core/SwitchYard_Controller.class.php Mon Oct 5
18:52:47 2009
@@ -3,9 +3,52 @@
abstract class SwitchYard_Controller
{
+ protected $model = null;
+ protected $nice_name = null;
+ private $pager = null;
private $params = null;
+ public $table = null;
+ public $table_name = null;
+ public $primary_key = null;
public $view = null;
+ public function __construct()
+ {
+ if ($this->model) {
+ $this->table = Doctrine::getTable($this->model);
+ $this->table_name = $this->table->getTableName();
+ $this->nice_name = str_replace('_', ' ', $this->table_name);
+ $this->primary_key = $this->table_name.'_id';
+ }
+ }
+
+ /**
+ * This is a universal delete method for any data type. If things need to
be cascaded or files need to be deleted,
+ * then that should be handled in the delete() method of the model in
question.
+ *
+ * @param array $params
+ */
+ public function admin_delete($params = array())
+ {
+
+ extract($params['named']);
+
+ $obj = SwitchYard_DBUtil::fetch_object($this->model,
${$this->primary_key});
+ // delete a file if necessary
+ if ($this->table->hasColumn('filename')) {
+ if (file_exists(SwitchYard_Util::filePath($this->model,
$obj->filename))) {
+ unlink(SwitchYard_Util::filePath($this->model, $obj->filename));
+ }
+ }
+
+ // only delete if we have a live object
+ if (is_object($obj)) $obj->delete();
+
+ $return_to =
($this->table_name != '') ? '/admin/'.$this->table_name.'/list' :
$_REQUEST['return_to'] ;
+ SwitchYard::jumpTo($return_to);
+
+ }
+
public function error_page($params = array())
{
if (empty($this->view->vars->heading))
@@ -14,6 +57,131 @@
$this->view->set('message', "The page you requested was not found.");
return $this->view->render();
}
+
+ /**
+ * Fetches a row from the database as an associative array (hash). Can be
called two ways:
+ *
+ * 1. AppController::fetch_array($model, $id, $force_404)
+ * 2. AppController::fetch_array($id, $force_404)
+ *
+ * @return array
+ */
+ public function fetch_array()
+ {
+ $args = func_get_args();
+ if (preg_match('/\D+/', $args[0]) and $args[0] != 'new') {
+ $model = $args[0];
+ $id = $args[1];
+ $force_404 = (isset($args[2])) ? (bool)$args[2] : false ;
+ } else {
+ $model = $this->model;
+ $id = $args[0];
+ $force_404 = (isset($args[1])) ? (bool)$args[1] : false ;
+ }
+ if ($id == 'new') return new $model();
+ return SwitchYard_DBUtil::fetch_array($model, $id, $force_404);
+ }
+
+ /**
+ * Fetchs a row from the database as an object.
+ *
+ * 1. AppController::fetch_object($model, $id, $force_404)
+ * 2. AppController::fetch_object($id, $force_404)
+ *
+ * @return string
+ */
+ public function fetch_object()
+ {
+ $args = func_get_args();
+ if (preg_match('/\D+/', $args[0]) and $args[0] != 'new') {
+ $model = $args[0];
+ $id = $args[1];
+ $force_404 = (isset($args[2])) ? (bool)$args[2] : false ;
+ } else {
+ $model = $this->model;
+ $id = $args[0];
+ $force_404 = (isset($args[1])) ? (bool)$args[1] : false ;
+ }
+ if ($id == 'new') return new $model();
+ return SwitchYard_DBUtil::fetch_object($model, $id, $force_404);
+ }
+
+ /**
+ * Fetches a resultset from the database as an array of associative
arrays (hashes) or objects.
+ *
+ * @param string $pager_link
+ * @param string $callback [default: null]
+ * @param bool $as_array [default: false]
+ * @return array
+ */
+ public function fetch_set($pager_link, $callback = null, $as_array =
false)
+ {
+
+ $q = Doctrine_Query::create()->from($this->model);
+ if ($as_array === true) $q->setHydrationMode(Doctrine::HYDRATE_ARRAY);
+ // callback to do further setup on the Doctrine_Query object
+ if (method_exists($this, $callback)) call_user_func_array(array($this,
$callback), array(&$q));
+
+ $set = array();
+ $this->pager = new Doctrine_Pager($q, $_REQUEST['pn'], $_REQUEST['pp']);
+ foreach ($this->pager->execute() as $row) $set[] = $row;
+
+ $this->view->set('pagination',
SwitchYard_Doctrine::pagerLayout($this->pager,
SwitchYard::link($pager_link)));
+
+ return $set;
+
+ }
+
+ public function file_exists()
+ {
+ return (!empty($this->object->filename) and
file_exists($this->file_path($object->filename))) ? true : false ;
+ }
+
+ public function file_path($file_name)
+ {
+ return
SY_DATADIR.'/models/'.strtolower($this->model).'/'.$this->object->filename;
+ }
+
+ public function file_url($file_name)
+ {
+ return str_replace('//', '/',
SY_HTTPDIR.'/d/'.strtolower($this->model).'/'.$this->object->filename);
+ }
+
+ public function save_file($file_key)
+ {
+ // return false if we didn't save
+ if (!is_uploaded_file($_FILES[$file_key]['tmp_name'])) return false;
+ // (try to) get the file's extension
+ preg_match('/\.(.+)$/', $_FILES[$file_key]['name'], $matches);
+ $extension = $matches[1];
+ // make sure the dir exists for this model; if not, create it
+ $file_path = SY_DATADIR.'/models/'.strtolower($this->model);
+ if (!file_exists($file_path)) {
+ mkdir($file_path);
+ chmod($file_path, 0777);
+ }
+ if (!empty($object->filename) and
file_exists($file_path.'/'.$this->object->filename)) {
+ unlink($file_path.'/'.$this->object->filename);
+ }
+ // create a tmp name for this file & move it
+ $file_name = md5(time().$model).'.'.$extension;
+ move_uploaded_file($_FILES[$file_key]['tmp_name'],
$file_path.'/'.$file_name);
+ if (is_object($object)) {
+ $this->object->filename = $file_name;
+ $this->object->save();
+ }
+ return $file_name;
+ }
+
+ public function image_tag($params = array())
+ {
+ if (!$this->file_exists($this->object)) return null;
+ $image_url = $this->file_url($this->object->filename);
+ $class = $params['class'];
+ $id = $params['id'];
+ $name = $params['name'];
+ return "<img src=\"{$image_url}\" class=\"{$class}\" id=\"{$id}\"
alt=\"{$name}\" />";
+ }
}
=======================================
--- /framework/trunk/app/core/SwitchYard_Doctrine.class.php Fri Oct 2
04:54:28 2009
+++ /framework/trunk/app/core/SwitchYard_Doctrine.class.php Mon Oct 5
18:52:47 2009
@@ -29,13 +29,13 @@
* @param int $chunk
* @return string
*/
- public static function pagerLayout($pager, $link, $chunk = 5)
+ public static function pagerLayout($pager, $link, $chunk = 5, $options =
array())
{
$pager_range = $pager->getRange('Sliding', array('chunk' => $chunk));
$pager_layout = new SwitchYard_Doctrine_Pager_Layout($pager,
$pager_range, $link.'?pn={%page_number}');
$pager_layout->setTemplate(' <a href="{%url}">{%page}</a> ');
$pager_layout->setSelectedTemplate(' <strong>{%page}</strong> ');
- return $pager_layout->display(array(), true);
+ return $pager_layout->display($options, true);
}
}
=======================================
--- /framework/trunk/app/core/SwitchYard_Doctrine_Pager_Layout.class.php
Mon Jul 13 12:44:47 2009
+++ /framework/trunk/app/core/SwitchYard_Doctrine_Pager_Layout.class.php
Mon Oct 5 18:52:47 2009
@@ -5,16 +5,27 @@
public function display($options = array())
{
+ $show_first_link = true;
+ $show_last_link = true;
+ $first_mask = '«« First';
+ $prev_mask = '« Prev';
+ $next_mask = 'Next »';
+ $last_mask = 'Last »»';
+
+ extract($options);
+
$pager = $this->getPager();
$str = '';
// First page
- $this->addMaskReplacement('page', '«« First', true);
- $options['page_number'] = $pager->getFirstPage();
- $str .= $this->processPage($options);
+ if ($show_first_link) {
+ $this->addMaskReplacement('page', $first_mask, true);
+ $options['page_number'] = $pager->getFirstPage();
+ $str .= $this->processPage($options);
+ }
// Previous page
- $this->addMaskReplacement('page', '« Prev', true);
+ $this->addMaskReplacement('page', $prev_mask, true);
$options['page_number'] = $pager->getPreviousPage();
$str .= $this->processPage($options);
@@ -23,14 +34,16 @@
$str .= parent::display($options, true);
// Next page
- $this->addMaskReplacement('page', 'Next »', true);
+ $this->addMaskReplacement('page', $next_mask, true);
$options['page_number'] = $pager->getNextPage();
$str .= $this->processPage($options);
// Last page
- $this->addMaskReplacement('page', 'Last »»', true);
- $options['page_number'] = $pager->getLastPage();
- $str .= $this->processPage($options);
+ if ($show_last_link) {
+ $this->addMaskReplacement('page', $last_mask, true);
+ $options['page_number'] = $pager->getLastPage();
+ $str .= $this->processPage($options);
+ }
// check to see if we have hrefs ... if not, return an empty string so
we don't have useless pagination
if (!strstr($str, 'href="')) $str = null;
=======================================
--- /framework/trunk/app/core/SwitchYard_Form.class.php Thu Oct 1 20:17:39
2009
+++ /framework/trunk/app/core/SwitchYard_Form.class.php Mon Oct 5 18:52:47
2009
@@ -123,7 +123,7 @@
$fld->setDefaultVal($col['default']);
// if there are any field-specific instructions for this column, handle
them
- if (isset($fld_config) and is_array($fld_config) and
is_array($fld_config[$col['name']]))
+ if (isset($fld_config) and is_array($fld_config) and
isset($fld_config[$col['name']]) and is_array($fld_config[$col['name']]))
$fld->setFieldParams($fld_config[$col['name']]);
// set the validators
@@ -154,7 +154,7 @@
public function getCols() { return
SwitchYard::$doctrineConn->import->listTableColumns($this->table_name); }
- public function saveObject(&$object, $input)
+ public function saveObject($object, $input)
{
if (!is_object($object)) return false;
@@ -172,7 +172,7 @@
}
$object->$col['name'] = $value;
} else {
- if (!is_null($input[$col['name']])) {
+ if (isset($input[$col['name']]) and !is_null($input[$col['name']])) {
$object->$col['name'] = $input[$col['name']];
}
}
=======================================
--- /framework/trunk/index.php Fri Oct 2 04:54:28 2009
+++ /framework/trunk/index.php Mon Oct 5 18:52:47 2009
@@ -3,15 +3,16 @@
/**
* SwitchYard Master Controller Script
* @author Jeremy Clifton <j.cl...@4-8-4.com>
- * @version 3.1.8
+ * @version 3.1.9
* @copyright 2005-2009 4-8-4 Software Works, LLC
* @license BSD
*/
-error_reporting(E_ALL);
+//error_reporting(E_ALL);
+error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING & ~E_DEPRECATED);
ini_set('display_errors', true);
-define('SY_VERSION', '3.1.8');
+define('SY_VERSION', '3.1.9');
require 'app/core/bootstrap.php';
SwitchYard::$router = new SwitchYard_Router();
SwitchYard::$router->dispatch();