File_to_post.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Table Definition for file_to_post
  18. *
  19. * @copyright 2008, 2009 StatusNet, Inc.
  20. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  21. */
  22. defined('GNUSOCIAL') || die();
  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; // timestamp() 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' => 'timestamp', 'not null' => true, '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_to_post_post_id_idx' => array('post_id'),
  48. ),
  49. );
  50. }
  51. public static function processNew(File $file, Notice $notice)
  52. {
  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. public 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. public 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. }