You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.2 KiB

  1. <?php
  2. namespace db;
  3. use \ORM;
  4. function random_hash() {
  5. $len = 32;
  6. $alpha_numeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  7. return substr(str_shuffle(str_repeat($alpha_numeric, $len)), 0, $len);
  8. }
  9. function now() {
  10. return date('Y-m-d H:i:s');
  11. }
  12. function set_updated(&$record) {
  13. $record->date_updated = now();
  14. }
  15. function find_or_create($table, $where, $defaults, $autosave=false) {
  16. $item = ORM::for_table($table);
  17. // Where is an associative array of key/val combos
  18. foreach($where as $c=>$v) {
  19. $item = $item->where($c, $v);
  20. }
  21. $item = $item->find_one();
  22. if(!$item) {
  23. $item = ORM::for_table($table)->create();
  24. $item->date_created = now();
  25. foreach($defaults as $k=>$v) {
  26. $item->{$k} = $v;
  27. }
  28. foreach($where as $k=>$v) {
  29. $item->{$k} = $v;
  30. }
  31. if($autosave)
  32. $item->save();
  33. }
  34. return $item;
  35. }
  36. function feed_from_url($url) {
  37. return ORM::for_table('feeds')->where('feed_url', $url)->find_one();
  38. }
  39. function get_by_id($table, $id) {
  40. return ORM::for_table($table)->where('id', $id)->find_one();
  41. }
  42. function get_by_col($table, $col, $val) {
  43. return ORM::for_table($table)->where($col, $val)->find_one();
  44. }