/application/plugins/wikilinks/init.php

https://github.com/dbernar1/Project-Pier · PHP · 221 lines · 152 code · 27 blank · 42 comment · 37 complexity · 6e50f2c13ccc07c13f4d6fb157b0a3fe MD5 · raw file

  1. <?php
  2. add_filter('project_description', 'wikilinks_links');
  3. add_filter('all_messages_message_text', 'wikilinks_links');
  4. add_filter('message_text', 'wikilinks_links');
  5. add_filter('message_additional_text', 'wikilinks_links');
  6. add_filter('task_list_description', 'wikilinks_links');
  7. add_filter('open_task_text', 'wikilinks_links');
  8. add_filter('completed_task_text', 'wikilinks_links');
  9. add_filter('ticket_description', 'wikilinks_links');
  10. add_filter('ticket_change_comment', 'wikilinks_links');
  11. add_filter('milestone_description', 'wikilinks_links');
  12. add_filter('comment_text', 'wikilinks_links');
  13. add_filter('file_description', 'wikilinks_links');
  14. add_filter('form_description', 'wikilinks_links');
  15. add_filter('pageattachment_text', 'wikilinks_links');
  16. add_filter('wiki_text', 'wikilinks_links');
  17. function wikilinks_activate() {
  18. }
  19. function wikilinks_links($content) {
  20. $content = preg_replace_callback('/\[message:([0-9]*)\]/', 'replace_message_link_callback', $content);
  21. $content = preg_replace_callback('/\[task_list:([0-9]*)\]/', 'replace_task_list_link_callback', $content);
  22. $content = preg_replace_callback('/\[ticket:([0-9]*)\]/', 'replace_ticket_link_callback', $content);
  23. $content = preg_replace_callback('/\[milestone:([0-9]*)\]/', 'replace_milestone_link_callback', $content);
  24. $content = preg_replace_callback('/\[file:([0-9]*)\]/', 'replace_file_link_callback', $content);
  25. $content = preg_replace_callback('/\[user:([0-9]*)\]/', 'replace_user_link_callback', $content);
  26. $content = preg_replace_callback('/\[link:([0-9]*)\]/', 'replace_link_link_callback', $content);
  27. return $content;
  28. } // wikilinks_links
  29. /**
  30. * Call back function for message link
  31. *
  32. * @param mixed $matches
  33. * @return
  34. */
  35. function replace_message_link_callback($matches) {
  36. if (count($matches) < 2){
  37. return null;
  38. } // if
  39. if (!logged_user()->isMemberOfOwnerCompany()) {
  40. $object = ProjectMessages::findOne(array(
  41. 'conditions' => array('`id` = ? AND `project_id` = ? AND `is_private` = 0 ', $matches[1], active_project()->getId())));
  42. } else {
  43. $object = ProjectMessages::findOne(array(
  44. 'conditions' => array('`id` = ? AND `project_id` = ?', $matches[1], active_project()->getId())));
  45. } // if
  46. if (!($object instanceof ProjectMessage)) {
  47. return '<del>'.lang('invalid reference', $matches[0]).'</del>';
  48. } else {
  49. return '<a href="'.externalUrl($object->getViewUrl()).'" title="'.lang('message').'">'.$object->getTitle().'</a>';
  50. } // if
  51. } // replace_message_link_callback
  52. /**
  53. * Call back function for task list link
  54. *
  55. * @param mixed $matches
  56. * @return
  57. */
  58. function replace_task_list_link_callback($matches) {
  59. if (count($matches) < 2){
  60. return null;
  61. } // if
  62. if (!logged_user()->isMemberOfOwnerCompany()) {
  63. $object = ProjectTaskLists::findOne(array(
  64. 'conditions' => array('`id` = ? AND `project_id` = ? AND `is_private` = 0 ', $matches[1], active_project()->getId())));
  65. } else {
  66. $object = ProjectTaskLists::findOne(array(
  67. 'conditions' => array('`id` = ? AND `project_id` = ?', $matches[1], active_project()->getId())));
  68. } // if
  69. if (!($object instanceof ProjectTaskList)) {
  70. return '<del>'.lang('invalid reference', $matches[0]).'</del>';
  71. } else {
  72. return '<a href="'.externalUrl($object->getViewUrl()).'" title="'.lang('task list').'">'.$object->getName().'</a>';
  73. } // if
  74. } // replace_task_list_link_callback
  75. /**
  76. * Call back function for ticket link
  77. *
  78. * @param mixed $matches
  79. * @return
  80. */
  81. function replace_ticket_link_callback($matches) {
  82. if (!plugin_active('tickets')) return null;
  83. if (count($matches) < 2){
  84. return null;
  85. } // if
  86. if (!logged_user()->isMemberOfOwnerCompany()) {
  87. $object = ProjectTickets::findOne(array(
  88. 'conditions' => array('`id` = ? AND `project_id` = ? AND `is_private` = 0 ', $matches[1], active_project()->getId())));
  89. } else {
  90. $object = ProjectTickets::findOne(array(
  91. 'conditions' => array('`id` = ? AND `project_id` = ?', $matches[1], active_project()->getId())));
  92. } // if
  93. if (!($object instanceof ProjectTicket)) {
  94. return '<del>'.lang('invalid reference', $matches[0]).'</del>';
  95. } else {
  96. return '<a href="'.externalUrl($object->getViewUrl()).'" title="'.lang('ticket').'">'.$object->getTitle().'</a>';
  97. } // if
  98. } // replace_ticket_link_callback
  99. /**
  100. * Call back function for milestone link
  101. *
  102. * @param mixed $matches
  103. * @return
  104. */
  105. function replace_milestone_link_callback($matches) {
  106. if (count($matches) < 2){
  107. return null;
  108. } // if
  109. if (!logged_user()->isMemberOfOwnerCompany()) {
  110. $object = ProjectMilestones::findOne(array(
  111. 'conditions' => array('`id` = ? AND `project_id` = ? AND `is_private` = 0 ', $matches[1], active_project()->getId())));
  112. } else {
  113. $object = ProjectMilestones::findOne(array(
  114. 'conditions' => array('`id` = ? AND `project_id` = ?', $matches[1], active_project()->getId())));
  115. } // if
  116. if (!($object instanceof ProjectMilestone)) {
  117. return '<del>'.lang('invalid reference', $matches[0]).'</del>';
  118. } else {
  119. return '<a href="'.externalUrl($object->getViewUrl()).'" title="'.lang('milestone').'">'.$object->getName().'</a>';
  120. } // if
  121. } // replace_milestone_link_callback
  122. /**
  123. * Call back function for file link
  124. *
  125. * @param mixed $matches
  126. * @return
  127. */
  128. function replace_file_link_callback($matches) {
  129. if (count($matches) < 2){
  130. return null;
  131. } // if
  132. if (!logged_user()->isMemberOfOwnerCompany()) {
  133. $object = ProjectFiles::findOne(array(
  134. 'conditions' => array('`id` = ? AND `project_id` = ? AND `is_private` = 0 ', $matches[1], active_project()->getId())));
  135. } else {
  136. $object = ProjectFiles::findOne(array(
  137. 'conditions' => array('`id` = ? AND `project_id` = ?', $matches[1], active_project()->getId())));
  138. } // if
  139. if (!($object instanceof ProjectFile)) {
  140. return '<del>'.lang('invalid reference', $matches[0]).'</del>';
  141. } else {
  142. if ($object->getFileType()->getIsImage()) {
  143. return '<img src="'.externalUrl($object->getDownloadUrl()).'" alt="'.lang('file').'" style="max-width:100%; max-height:100%;" />';
  144. }
  145. return '<a href="'.externalUrl($object->getViewUrl()).'" title="'.lang('file').'">'.$object->getFilename().'</a>';
  146. } // if
  147. } // replace_user_link_callback
  148. /**
  149. * Call back function for user link
  150. *
  151. * @param mixed $matches
  152. * @return
  153. */
  154. function replace_user_link_callback($matches) {
  155. if (count($matches) < 2){
  156. return null;
  157. } // if
  158. if (!logged_user()->isMemberOfOwnerCompany()) {
  159. $object = Users::findOne(array(
  160. 'conditions' => array('`id` = ? ', $matches[1] )));
  161. } else {
  162. $object = Users::findOne(array(
  163. 'conditions' => array('`id` = ? ', $matches[1] )));
  164. } // if
  165. if (!($object instanceof User)) {
  166. return '<del>'.lang('invalid reference', $matches[0]).'</del>';
  167. } else {
  168. return '<a href="'.externalUrl($object->getCardUrl()).'" title="'.lang('user').'">'.$object->getObjectName().'</a>';
  169. } // if
  170. } // replace_user_link_callback
  171. /**
  172. * Call back function for user link
  173. *
  174. * @param mixed $matches
  175. * @return
  176. */
  177. function replace_link_link_callback($matches) {
  178. if (count($matches) < 2){
  179. return null;
  180. } // if
  181. if (!logged_user()->isMemberOfOwnerCompany()) {
  182. $object = ProjectLinks::findOne(array(
  183. 'conditions' => array('`id` = ? AND `project_id` = ? AND `is_private` = 0 ', $matches[1], active_project()->getId())));
  184. } else {
  185. $object = ProjectLinks::findOne(array(
  186. 'conditions' => array('`id` = ? AND `project_id` = ?', $matches[1], active_project()->getId())));
  187. } // if
  188. if (!($object instanceof ProjectLink)) {
  189. return '<del>'.lang('invalid reference', $matches[0]).'</del>';
  190. } else {
  191. return '<a href="'.externalUrl($object->getViewUrl()).'" title="'.lang('link').'">'.$object->getObjectName().'</a>';
  192. } // if
  193. } // replace_link_link_callback
  194. ?>