BlogPlugin.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * A microapp to implement lite blogging
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Blog
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Blog plugin
  37. *
  38. * Many social systems have a way to write and share long-form texts with
  39. * your network. This microapp plugin lets users post blog entries.
  40. *
  41. * @category Blog
  42. * @package StatusNet
  43. * @author Evan Prodromou <evan@status.net>
  44. * @copyright 2011 StatusNet, Inc.
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  46. * @link http://status.net/
  47. */
  48. class BlogPlugin extends MicroAppPlugin
  49. {
  50. var $oldSaveNew = true;
  51. /**
  52. * Database schema setup
  53. *
  54. * @see Schema
  55. * @see ColumnDef
  56. *
  57. * @return boolean hook value; true means continue processing, false means stop.
  58. */
  59. function onCheckSchema()
  60. {
  61. $schema = Schema::get();
  62. $schema->ensureTable('blog_entry', Blog_entry::schemaDef());
  63. return true;
  64. }
  65. public function newFormAction(){
  66. return 'newblogentry';
  67. }
  68. /**
  69. * Map URLs to actions
  70. *
  71. * @param URLMapper $m path-to-action mapper
  72. *
  73. * @return boolean hook value; true means continue processing, false means stop.
  74. */
  75. public function onRouterInitialized(URLMapper $m)
  76. {
  77. $m->connect('blog/new',
  78. array('action' => 'newblogentry'));
  79. $m->connect('blog/:id',
  80. array('action' => 'showblogentry'),
  81. array('id' => UUID::REGEX));
  82. return true;
  83. }
  84. function onPluginVersion(&$versions)
  85. {
  86. $versions[] = array('name' => 'Blog',
  87. 'version' => GNUSOCIAL_VERSION,
  88. 'author' => 'Evan Prodromou',
  89. 'homepage' => 'http://status.net/wiki/Plugin:Blog',
  90. 'rawdescription' =>
  91. // TRANS: Plugin description.
  92. _m('Let users write and share long-form texts.'));
  93. return true;
  94. }
  95. function appTitle()
  96. {
  97. // TRANS: Blog application title.
  98. return _m('TITLE','Blog');
  99. }
  100. function tag()
  101. {
  102. return 'blog';
  103. }
  104. function types()
  105. {
  106. return array(Blog_entry::TYPE);
  107. }
  108. function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
  109. {
  110. if (count($activity->objects) != 1) {
  111. // TRANS: Exception thrown when there are too many activity objects.
  112. throw new ClientException(_m('Too many activity objects.'));
  113. }
  114. $entryObj = $activity->objects[0];
  115. if ($entryObj->type != Blog_entry::TYPE) {
  116. // TRANS: Exception thrown when blog plugin comes across a non-blog entry type object.
  117. throw new ClientException(_m('Wrong type for object.'));
  118. }
  119. $notice = null;
  120. switch ($activity->verb) {
  121. case ActivityVerb::POST:
  122. $notice = Blog_entry::saveNew($actor,
  123. $entryObj->title,
  124. $entryObj->content,
  125. $options);
  126. break;
  127. default:
  128. // TRANS: Exception thrown when blog plugin comes across a undefined verb.
  129. throw new ClientException(_m('Unknown verb for blog entries.'));
  130. }
  131. return $notice;
  132. }
  133. function activityObjectFromNotice(Notice $notice)
  134. {
  135. $entry = Blog_entry::fromNotice($notice);
  136. if (empty($entry)) {
  137. // TRANS: Exception thrown when requesting a non-existing blog entry for notice.
  138. throw new ClientException(sprintf(_m('No blog entry for notice %s.'),
  139. $notice->id));
  140. }
  141. return $entry->asActivityObject();
  142. }
  143. function entryForm($out)
  144. {
  145. return new BlogEntryForm($out);
  146. }
  147. function deleteRelated(Notice $notice)
  148. {
  149. if ($notice->object_type == Blog_entry::TYPE) {
  150. $entry = Blog_entry::fromNotice($notice);
  151. if (!empty($entry)) {
  152. $entry->delete();
  153. }
  154. }
  155. }
  156. function adaptNoticeListItem($nli)
  157. {
  158. $notice = $nli->notice;
  159. if ($notice->object_type == Blog_entry::TYPE) {
  160. return new BlogEntryListItem($nli);
  161. }
  162. return null;
  163. }
  164. function onEndShowScripts($action)
  165. {
  166. $action->script(common_path('plugins/TinyMCE/js/jquery.tinymce.js'));
  167. $action->inlineScript('var _tinymce_path = "'.common_path('plugins/TinyMCE/js/tiny_mce.js').'";'."\n".
  168. 'var _tinymce_placeholder = "'.common_path('plugins/TinyMCE/icons/placeholder.png').'";'."\n");
  169. $action->script($this->path('blog.js'));
  170. return true;
  171. }
  172. }