<?php
|
|
namespace db;
|
|
use \ORM;
|
|
|
|
function random_hash() {
|
|
$len = 32;
|
|
$alpha_numeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
return substr(str_shuffle(str_repeat($alpha_numeric, $len)), 0, $len);
|
|
}
|
|
|
|
function now() {
|
|
return date('Y-m-d H:i:s');
|
|
}
|
|
|
|
function set_updated(&$record) {
|
|
$record->date_updated = now();
|
|
}
|
|
|
|
function find_or_create($table, $where, $defaults, $autosave=false) {
|
|
$item = ORM::for_table($table);
|
|
|
|
// Where is an associative array of key/val combos
|
|
foreach($where as $c=>$v) {
|
|
$item = $item->where($c, $v);
|
|
}
|
|
|
|
$item = $item->find_one();
|
|
|
|
if(!$item) {
|
|
$item = ORM::for_table($table)->create();
|
|
$item->date_created = now();
|
|
foreach($defaults as $k=>$v) {
|
|
$item->{$k} = $v;
|
|
}
|
|
foreach($where as $k=>$v) {
|
|
$item->{$k} = $v;
|
|
}
|
|
if($autosave)
|
|
$item->save();
|
|
}
|
|
return $item;
|
|
}
|
|
|