@J.Dre
I think it's good idea.
----------------------------------
This is beta version of Account class. Any ideas?
In class is static function:
addField($name)
so you can add field to load every account load, for example you can add column 'flag':
Account::addField('flag')
it will load column 'flag' always when you load new account by class Account, not like in POT where you have to use $account->getCustomField('flag') [it uses 1 SQL query every execute, my class not ]
This is Table class:
and this is example of use in page script:
I think it's good idea.
----------------------------------
This is beta version of Account class. Any ideas?
PHP:
class Account
{
const BY_ID = 1;
const BY_NAME = 2;
private $SQL;
private $data = array('id' => null, 'name' => null, 'password' => null, 'salt' => null, 'premdays' => null, 'lastday' => null, 'email' => null, 'key' => null, 'blocked' => null, 'warnings' => null, 'group_id' => null, 'page_access' => null, 'page_lastday' => null, 'email_new' => null, 'email_new_time' => null, 'rlname' => null, 'location' => null, 'created' => null, 'email_code' => null, 'next_email' => null, 'premium_points' => null, 'nickname' => null, 'avatar' => null, 'about_me' => null, 'vote' => null, 'flag' => null);
private static $fields = array('id', 'name', 'password', 'premdays', 'lastday', 'email', 'key', 'group_id', 'rlname', 'location', 'created', 'premium_points');
public static function addField($name)
{
if(!in_array($name, self::$fields))
self::$fields[] = $name;
}
public static function removeField($name)
{
if(in_array($name, self::$fields))
unset(self::$fields[$name]);
}
public static function getFieldsList()
{
return self::$fields;
}
public function __construct($search_text = null, $search_by = self::BY_ID)
{
$this->SQL = Website::getInstance()->getDBHandle();
if($search_text != null)
$this->load($search_text, $search_by);
}
public function load($search_text, $search_by = self::BY_ID)
{
if($search_by == self::BY_ID)
$search_string = $this->SQL->fieldName('id') . ' = ' . $this->SQL->quote($search_text);
elseif($search_by == self::BY_NAME)
$search_string = $this->SQL->fieldName('name') . ' = ' . $this->SQL->quote($search_text);
else
new Error_Critic('', 'Wrong account search_by type.');
$fieldsArray = array();
foreach(self::$fields as $fieldName)
$fieldsArray[$fieldName] = $this->SQL->fieldName($fieldName);
$this->data = $this->SQL->query('SELECT ' . implode(', ', $fieldsArray) . ' FROM ' . $this->SQL->fieldName('accounts') . ' WHERE ' . $search_string)->fetch();
}
public function loadById($id)
{
return $this->load($id, self::BY_ID);
}
public function loadByName($name)
{
return $this->load($name, self::BY_NAME);
}
public function save($forceInsert = false)
{
if(!isset($this->data['id']) || $forceInsert)
{
$keys = array();
$values = array();
foreach(self::$fields as $key)
if($key != 'id')
{
$keys[] = $this->SQL->fieldName($key);
$values[] = $this->SQL->quote($this->data[$key]);
}
$this->SQL->query('INSERT INTO `accounts` (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $values) . ')');
}
else
{
$updates = array();
foreach(self::$fields as $key)
if($key != 'id')
$updates[] = $this->SQL->fieldName($key) . ' = ' . $this->SQL->quote($this->data[$key]);
$this->SQL->query('UPDATE `accounts` SET ' . implode(', ', $updates) . ' WHERE ' . $this->SQL->fieldName('id') . ' = ' . $this->SQL->quote($this->data['id']));
}
}
public function isLoaded()
{
return isset($this->data['id']);
}
public function setID($value){$this->data['id'] = $value;}
public function getID(){return $this->data['id'];}
public function setName($value){$this->data['name'] = $value;}
public function getName(){return $this->data['name'];}
public function setPassword($value){$this->data['password'] = $value;}
public function getPassword(){return $this->data['password'];}
public function setPremDays($value){$this->data['premdays'] = $value;}
public function getPremDays(){return $this->data['premdays'];}
public function setLastDay($value){$this->data['lastday'] = $value;}
public function getLastDay(){return $this->data['lastday'];}
public function setMail($value){$this->data['email'] = $value;}
public function getMail(){return $this->data['email'];}
public function setKey($value){$this->data['key'] = $value;}
public function getKey(){return $this->data['key'];}
public function setGroupId($value){$this->data['group_id'] = $value;}
public function getGroupId(){return $this->data['group_id'];}
public function setRLName($value){$this->data['rlname'] = $value;}
public function getRLName(){return $this->data['rlname'];}
public function setLocation($value){$this->data['location'] = $value;}
public function getLocation(){return $this->data['location'];}
public function setCreated($value){$this->data['created'] = $value;}
public function getCreated(){return $this->data['created'];}
public function setPremiumPoints($value){$this->data['premium_points'] = $value;}
public function getPremiumPoints(){return $this->data['premium_points'];}
}
addField($name)
so you can add field to load every account load, for example you can add column 'flag':
Account::addField('flag')
it will load column 'flag' always when you load new account by class Account, not like in POT where you have to use $account->getCustomField('flag') [it uses 1 SQL query every execute, my class not ]
This is Table class:
PHP:
<?PHP
class Table extends Errors
{
private $id = '';
private $name = '';
private $title = '';
private $class = '';
private $rows = array();
private $styles = array();
private $scripts = array();
public function __construct($id = null, $name = null, $title = null, $class = null, $rows = null, $styles = null, $scripts = null)
{
if($id != null)
$this->id = $id;
if($name != null)
$this->name = $name;
if($title != null)
$this->title = $title;
if($class != null)
$this->class = $class;
if($rows != null)
$this->rows = $rows;
if($styles != null)
$this->styles = $styles;
if($scripts != null)
$this->scripts = $scripts;
}
public function setId($value)
{
$this->id = $value;
}
public function setName($value)
{
$this->name = $value;
}
public function setTitle($value)
{
$this->title = $value;
}
public function setClass($value)
{
$this->class = $value;
}
public function setRows($value)
{
$this->rows = $value;
}
public function setStyles($value)
{
$this->styles = $value;
}
public function setStyle($key, $value)
{
$this->styles[$key] = $value;
}
public function setScripts($value)
{
$this->scripts = $value;
}
public function addRow(TableRow $row)
{
$this->rows[] = $row;
}
public function getTableString()
{
$ret = '<table';
if($this->id != '')
$ret .= ' id="' . $this->id . '"';
if($this->name != '')
$ret .= ' name="' . $this->name . '"';
if($this->class != '')
$ret .= ' class="' . $this->class . '"';
$styles = array();
if(count($this->styles) > 0)
foreach($this->styles as $key => $value)
$styles = $key . ':' . $value;
if(count($styles) > 0)
$ret .= ' style="' . implode(';', $styles) . '"';
if($this->title != '')
$ret .= ' title="' . $this->title . '"';
if(count($this->scripts) > 0)
foreach($this->scripts as $event => $script)
$ret .= ' ' . $event . '="' . $script . '"';
$ret .= '>';
if(count($this->rows) > 0)
foreach($this->rows as $row)
$ret .= $row->getRowString();
$ret .= '</table>';
return $ret;
}
public function __toString()
{
return $this->getTableString();
}
}
class TableRow extends Errors
{
private $content = '';
private $id = '';
private $name = '';
private $title = '';
private $rowspan = 0;
private $colspan = 0;
private $class = '';
private $cells = array();
private $styles = array();
private $scripts = array();
public function __construct($content = null, $id = null, $name = null, $title = null, $rowspan = null, $colspan = null, $class = null, $cells = null, $styles = null, $scripts = null)
{
if($content != null)
$this->content = $content;
if($id != null)
$this->id = $id;
if($name != null)
$this->name = $name;
if($title != null)
$this->title = $title;
if($rowspan != null)
$this->rowspan = $rowspan;
if($colspan != null)
$this->colspan = $colspan;
if($class != null)
$this->class = $class;
if($cells != null)
$this->cells = $cells;
if($styles != null)
$this->styles = $styles;
if($scripts != null)
$this->scripts = $scripts;
}
public function getContent()
{
return $this->content;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getTitle()
{
return $this->title;
}
public function getRowspan()
{
return $this->rowspan;
}
public function getColspan()
{
return $this->colspan;
}
public function getClass()
{
return $this->class;
}
public function getCells()
{
return $this->cells;
}
public function getStyles()
{
return $this->styles;
}
public function getStyle($key)
{
return $this->styles[$key];
}
public function getScripts()
{
return $this->scripts;
}
public function setContent($value)
{
$this->content = $value;
}
public function setId($value)
{
$this->id = $value;
}
public function setName($value)
{
$this->name = $value;
}
public function setTitle($value)
{
$this->title = $value;
}
public function setRowspan($value)
{
$this->rowspan = $value;
}
public function setColspan($value)
{
$this->colspan = $value;
}
public function setClass($value)
{
$this->class = $value;
}
public function setCells($value)
{
$this->cells = $value;
}
public function setStyles($value)
{
$this->styles = $value;
}
public function setStyle($key, $value)
{
$this->styles[$key] = $value;
}
public function setScripts($value)
{
$this->scripts = $value;
}
public function addCell(TableCell $cell)
{
$this->cells[] = $cell;
}
public function getRowString()
{
$ret = '<tr';
if($this->id != '')
$ret .= ' id="' . $this->id . '"';
if($this->name != '')
$ret .= ' name="' . $this->name . '"';
if($this->class != '')
$ret .= ' class="' . $this->class . '"';
$styles = array();
if(count($this->styles) > 0)
foreach($this->styles as $key => $value)
$styles = $key . ':' . $value;
if(count($styles) > 0)
$ret .= ' style="' . implode(';', $styles) . '"';
if($this->title != '')
$ret .= ' title="' . $this->title . '"';
if($this->rowspan > 0)
$ret .= ' rowspan="' . $this->rowspan . '"';
if($this->colspan > 0)
$ret .= ' colspan="' . $this->colspan . '"';
if(count($this->scripts) > 0)
foreach($this->scripts as $event => $script)
$ret .= ' ' . $event . '="' . $script . '"';
$ret .= '>';
if($this->content == '')
{
if(count($this->cells) > 0)
foreach($this->cells as $cell)
$ret .= $cell->getCellString();
}
else
$ret .= $this->content;
$ret .= '</tr>';
return $ret;
}
public function __toString()
{
return $this->getRowString();
}
}
class TableCell extends Errors
{
private $content = '';
private $id = '';
private $name = '';
private $title = '';
private $rowspan = 0;
private $colspan = 0;
private $class = '';
private $styles = array();
private $scripts = array();
public function __construct($content = null, $class = null, $styles = null, $id = null, $name = null, $title = null, $rowspan = null, $colspan = null, $scripts = null)
{
if($content != null)
$this->content = $content;
if($id != null)
$this->id = $id;
if($name != null)
$this->name = $name;
if($title != null)
$this->title = $title;
if($rowspan != null)
$this->rowspan = $rowspan;
if($colspan != null)
$this->colspan = $colspan;
if($class != null)
$this->class = $class;
if($styles != null)
$this->styles = $styles;
if($scripts != null)
$this->scripts = $scripts;
}
public function getContent()
{
return $this->content;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getTitle()
{
return $this->title;
}
public function getRowspan()
{
return $this->rowspan;
}
public function getColspan()
{
return $this->colspan;
}
public function getClass()
{
return $this->class;
}
public function getStyles()
{
return $this->styles;
}
public function getStyle($key)
{
return $this->styles[$key];
}
public function getScripts()
{
return $this->scripts;
}
public function setContent($value)
{
$this->content = $value;
}
public function setId($value)
{
$this->id = $value;
}
public function setName($value)
{
$this->name = $value;
}
public function setTitle($value)
{
$this->title = $value;
}
public function setRowspan($value)
{
$this->rowspan = $value;
}
public function setColspan($value)
{
$this->colspan = $value;
}
public function setClass($value)
{
$this->class = $value;
}
public function setStyles($value)
{
$this->styles = $value;
}
public function setStyle($key, $value)
{
$this->styles[$key] = $value;
}
public function setScripts($value)
{
$this->scripts = $value;
}
public function getCellString()
{
$ret = '<td';
if($this->id != '')
$ret .= ' id="' . $this->id . '"';
if($this->name != '')
$ret .= ' name="' . $this->name . '"';
if($this->class != '')
$ret .= ' class="' . $this->class . '"';
$styles = array();
if(count($this->styles) > 0)
foreach($this->styles as $key => $value)
$styles = $key . ':' . $value;
if(count($styles) > 0)
$ret .= ' style="' . implode(';', $styles) . '"';
if($this->title != '')
$ret .= ' title="' . $this->title . '"';
if($this->rowspan > 0)
$ret .= ' rowspan="' . $this->rowspan . '"';
if($this->colspan > 0)
$ret .= ' colspan="' . $this->colspan . '"';
if(count($this->scripts) > 0)
foreach($this->scripts as $event => $script)
$ret .= ' ' . $event . '="' . $script . '"';
$ret .= '>';
$ret .= $this->content;
$ret .= '</td>';
return $ret;
}
public function __toString()
{
return $this->getCellString();
}
}
?>
PHP:
<?PHP
$table = new Table();
$row = new TableRow();
$row->addCell(new TableCell('first cell'));
$row->addCell(new TableCell('second cell', 'myClass'));
$table->addRow($row);
echo $table;
?>
Last edited by a moderator: