bookmark.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * Form for adding a new bookmark
  18. *
  19. * @category Bookmark
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2010 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Form to add a new bookmark
  28. *
  29. * @category Bookmark
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @copyright 2010 StatusNet, Inc.
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class BookmarkForm extends Form
  36. {
  37. private $_title = null;
  38. private $_url = null;
  39. private $_tags = null;
  40. private $_description = null;
  41. private $_thumbnail = null;
  42. /**
  43. * Construct a bookmark form
  44. *
  45. * @param HTMLOutputter $out output channel
  46. * @param string $title Title of the bookmark
  47. * @param string $url URL of the bookmark
  48. * @param string $tags Tags to show
  49. * @param string $description Description of the bookmark
  50. *
  51. * @return void
  52. */
  53. public function __construct(
  54. $out = null,
  55. $title = null,
  56. $url = null,
  57. $tags = null,
  58. $description = null,
  59. $thumbnail = null
  60. ) {
  61. parent::__construct($out);
  62. $this->_title = $title;
  63. $this->_url = $url;
  64. $this->_tags = $tags;
  65. $this->_description = $description;
  66. $this->_thumbnail = $thumbnail;
  67. }
  68. /**
  69. * ID of the form
  70. *
  71. * @return int ID of the form
  72. */
  73. public function id()
  74. {
  75. return 'form_new_bookmark';
  76. }
  77. /**
  78. * class of the form
  79. *
  80. * @return string class of the form
  81. */
  82. public function formClass()
  83. {
  84. return 'form_settings ajax-notice';
  85. }
  86. /**
  87. * Action of the form
  88. *
  89. * @return string URL of the action
  90. */
  91. public function action()
  92. {
  93. return common_local_url('newbookmark');
  94. }
  95. /**
  96. * Data elements of the form
  97. *
  98. * @return void
  99. */
  100. public function formData()
  101. {
  102. $this->out->elementStart('fieldset', array('id' => 'new_bookmark_data'));
  103. $this->out->elementStart('ul', 'form_data');
  104. $this->li();
  105. $this->out->input(
  106. 'bookmark-url',
  107. // TRANS: Field label on form for adding a new bookmark.
  108. _m('LABEL', 'URL'),
  109. $this->_url,
  110. null,
  111. 'url',
  112. true // HTML5 "required" attribute
  113. );
  114. $this->unli();
  115. if (!empty($this->_thumbnail)) {
  116. [$width, $height] = $this->scaleImage(
  117. $this->_thumbnail->width,
  118. $this->_thumbnail->height
  119. );
  120. $this->out->element('img', [
  121. 'src' => $this->_thumbnail->getUrl(),
  122. 'class' => 'bookmarkform-thumbnail',
  123. 'width' => $width,
  124. 'height' => $height,
  125. ]);
  126. }
  127. $this->li();
  128. $this->out->input(
  129. 'bookmark-title',
  130. // TRANS: Field label on form for adding a new bookmark.
  131. _m('LABEL', 'Title'),
  132. $this->_title,
  133. null,
  134. 'title',
  135. true // HTML5 "required" attribute
  136. );
  137. $this->unli();
  138. $this->li();
  139. $this->out->textarea(
  140. 'bookmark-description',
  141. // TRANS: Field label on form for adding a new bookmark.
  142. _m('LABEL', 'Notes'),
  143. $this->_description,
  144. null,
  145. 'description'
  146. );
  147. $this->unli();
  148. $this->li();
  149. $this->out->input(
  150. 'bookmark-tags',
  151. // TRANS: Field label on form for adding a new bookmark.
  152. _m('LABEL', 'Tags'),
  153. ($this->_tags ? implode(',', $this->_tags) : null),
  154. // TRANS: Field title on form for adding a new bookmark.
  155. _m('Comma- or space-separated list of tags.'),
  156. 'tags'
  157. );
  158. $this->unli();
  159. $this->out->elementEnd('ul');
  160. $toWidget = new ToSelector(
  161. $this->out,
  162. common_current_user(),
  163. null
  164. );
  165. $toWidget->show();
  166. $this->out->elementEnd('fieldset');
  167. }
  168. /**
  169. * Action elements
  170. *
  171. * @return void
  172. */
  173. public function formActions()
  174. {
  175. // TRANS: Button text for action to save a new bookmark.
  176. $this->out->submit('bookmark-submit', _m('BUTTON', 'Save'), 'submit', 'submit');
  177. }
  178. public function scaleImage($width, $height)
  179. {
  180. $maxwidth = common_config('thumbnail', 'width');
  181. $maxheight = common_config('thumbnail', 'height');
  182. if ($width > $height && $width > $maxwidth) {
  183. $height = (int) ((((float)$maxwidth)/(float)($width))*(float)$height);
  184. $width = $maxwidth;
  185. } elseif ($height > $maxheight) {
  186. $width = (int) ((((float)$maxheight)/(float)($height))*(float)$width);
  187. $height = $maxheight;
  188. }
  189. return array($width, $height);
  190. }
  191. }