File_to_post.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * Table Definition for file_to_post
  22. */
  23. class File_to_post extends Managed_DataObject
  24. {
  25. ###START_AUTOCODE
  26. /* the code below is auto generated do not remove the above tag */
  27. public $__table = 'file_to_post'; // table name
  28. public $file_id; // int(4) primary_key not_null
  29. public $post_id; // int(4) primary_key not_null
  30. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  31. /* the code above is auto generated do not remove the tag below */
  32. ###END_AUTOCODE
  33. public static function schemaDef()
  34. {
  35. return array(
  36. 'fields' => array(
  37. 'file_id' => array('type' => 'int', 'not null' => true, 'description' => 'id of URL/file'),
  38. 'post_id' => array('type' => 'int', 'not null' => true, 'description' => 'id of the notice it belongs to'),
  39. 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
  40. ),
  41. 'primary key' => array('file_id', 'post_id'),
  42. 'foreign keys' => array(
  43. 'file_to_post_file_id_fkey' => array('file', array('file_id' => 'id')),
  44. 'file_to_post_post_id_fkey' => array('notice', array('post_id' => 'id')),
  45. ),
  46. 'indexes' => array(
  47. 'file_id_idx' => array('file_id'),
  48. 'post_id_idx' => array('post_id'),
  49. ),
  50. );
  51. }
  52. static function processNew(File $file, Notice $notice) {
  53. static $seen = array();
  54. $file_id = $file->getID();
  55. $notice_id = $notice->getID();
  56. if (!array_key_exists($notice_id, $seen)) {
  57. $seen[$notice_id] = array();
  58. }
  59. if (empty($seen[$notice_id]) || !in_array($file_id, $seen[$notice_id])) {
  60. try {
  61. $f2p = File_to_post::getByPK(array('post_id' => $notice_id,
  62. 'file_id' => $file_id));
  63. } catch (NoResultException $e) {
  64. $f2p = new File_to_post;
  65. $f2p->file_id = $file_id;
  66. $f2p->post_id = $notice_id;
  67. $f2p->insert();
  68. $file->blowCache();
  69. }
  70. $seen[$notice_id][] = $file_id;
  71. }
  72. }
  73. static function getNoticeIDsByFile(File $file)
  74. {
  75. $f2p = new File_to_post();
  76. $f2p->selectAdd();
  77. $f2p->selectAdd('post_id');
  78. $f2p->file_id = $file->getID();
  79. $ids = array();
  80. if (!$f2p->find()) {
  81. throw new NoResultException($f2p);
  82. }
  83. return $f2p->fetchAll('post_id');
  84. }
  85. function delete($useWhere=false)
  86. {
  87. try {
  88. $f = File::getByID($this->file_id);
  89. $f->blowCache();
  90. } catch (NoResultException $e) {
  91. // ...alright, that's weird, but no File to delete anyway.
  92. }
  93. return parent::delete($useWhere);
  94. }
  95. }