User_urlshortener_prefs.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, 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('STATUSNET') && !defined('LACONICA')) {
  20. exit(1);
  21. }
  22. class User_urlshortener_prefs extends Managed_DataObject
  23. {
  24. ###START_AUTOCODE
  25. /* the code below is auto generated do not remove the above tag */
  26. public $__table = 'user_urlshortener_prefs'; // table name
  27. public $user_id; // int(4) primary_key not_null
  28. public $urlshorteningservice; // varchar(50) default_ur1.ca
  29. public $maxurllength; // int(4) not_null
  30. public $maxnoticelength; // int(4) not_null
  31. public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
  32. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  33. /* the code above is auto generated do not remove the tag below */
  34. ###END_AUTOCODE
  35. public static function schemaDef()
  36. {
  37. return array(
  38. 'fields' => array(
  39. 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user'),
  40. 'urlshorteningservice' => array('type' => 'varchar', 'length' => 50, 'default' => 'internal', 'description' => 'service to use for auto-shortening URLs'),
  41. 'maxurllength' => array('type' => 'int', 'not null' => true, 'description' => 'urls greater than this length will be shortened, 0 = always, null = never'),
  42. 'maxnoticelength' => array('type' => 'int', 'not null' => true, 'description' => 'notices with content greater than this value will have all urls shortened, 0 = always, -1 = only if notice text is longer than max allowed'),
  43. 'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
  44. 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
  45. ),
  46. 'primary key' => array('user_id'),
  47. 'foreign keys' => array(
  48. 'user_urlshortener_prefs_user_id_fkey' => array('user', array('user_id' => 'id')),
  49. ),
  50. );
  51. }
  52. static function maxUrlLength($user)
  53. {
  54. $def = common_config('url', 'maxurllength');
  55. $prefs = self::getPrefs($user);
  56. if (empty($prefs)) {
  57. return $def;
  58. } else {
  59. return $prefs->maxurllength;
  60. }
  61. }
  62. static function maxNoticeLength($user)
  63. {
  64. $def = common_config('url', 'maxnoticelength');
  65. if ($def == -1) {
  66. /*
  67. * maxContent==0 means infinite length,
  68. * but maxNoticeLength==0 means "always shorten"
  69. * so if maxContent==0 we must set this to -1
  70. */
  71. $def = Notice::maxContent() ?: -1;
  72. }
  73. $prefs = self::getPrefs($user);
  74. if (empty($prefs)) {
  75. return $def;
  76. } else {
  77. return $prefs->maxnoticelength;
  78. }
  79. }
  80. static function urlShorteningService($user)
  81. {
  82. $def = common_config('url', 'shortener');
  83. $prefs = self::getPrefs($user);
  84. if (empty($prefs)) {
  85. if (!empty($user)) {
  86. return $user->urlshorteningservice;
  87. } else {
  88. return $def;
  89. }
  90. } else {
  91. return $prefs->urlshorteningservice;
  92. }
  93. }
  94. static function getPrefs($user)
  95. {
  96. if (empty($user)) {
  97. return null;
  98. }
  99. $prefs = User_urlshortener_prefs::getKV('user_id', $user->id);
  100. return $prefs;
  101. }
  102. }