]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Attention.php
Opps, left-overs from merge ...
[quix0rs-gnu-social.git] / classes / Attention.php
1 <?php
2 /*
3  * GNU social - a federating social network
4  * Copyright (C) 2014, Free Software Foundation, 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
20 class Attention extends Managed_DataObject
21 {
22     public $__table = 'attention';  // table name
23     public $notice_id;              // int(4) primary_key not_null
24     public $profile_id;             // int(4) primary_key not_null
25     public $reason;                 // varchar(191)   not 255 because utf8mb4 takes more space
26     public $created;                // datetime()   not_null
27     public $modified;               // timestamp   not_null default_CURRENT_TIMESTAMP
28
29     public static function schemaDef()
30     {
31         return array(
32             'description' => 'Notice attentions to profiles (that are not a mention and not result of a subscription)',
33             'fields' => array(
34                 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice_id to give attention'),
35                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile_id for feed receiver'),
36                 'reason' => array('type' => 'varchar', 'length' => 191, 'description' => 'Optional reason why this was brought to the attention of profile_id'),
37                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
38                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
39             ),
40             'primary key' => array('notice_id', 'profile_id'),
41             'foreign keys' => array(
42                 'attention_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
43                 'attention_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
44             ),
45             'indexes' => array(
46                 'attention_notice_id_idx' => array('notice_id'),
47                 'attention_profile_id_idx' => array('profile_id'),
48             ),
49         );
50     }
51
52     public static function saveNew(Notice $notice, Profile $target, $reason=null)
53     {
54         try {
55             $att = Attention::getByKeys(['notice_id'=>$notice->getID(), 'profile_id'=>$target->getID()]);
56             throw new AlreadyFulfilledException('Attention already exists with reason: '.var_export($att->reason,true));
57         } catch (NoResultException $e) {
58             $att = new Attention();
59         
60             $att->notice_id = $notice->getID();
61             $att->profile_id = $target->getID();
62             $att->reason = $reason;
63             $att->created = common_sql_now();
64             $result = $att->insert();
65
66             if ($result === false) {
67                 throw new Exception('Failed Attention::saveNew for notice id=='.$notice->getID().' target id=='.$target->getID().', reason=="'.$reason.'"');
68             }
69         }
70         return $att;
71     }
72 }