/plugins/Tagging/class.tagmodel.php

https://github.com/lcapaldo/Garden · PHP · 99 lines · 70 code · 13 blank · 16 comment · 8 complexity · 49627ba289c125fc134c8568a6161100 MD5 · raw file

  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Vanilla Forums Inc.
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
  8. Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
  9. */
  10. class TagModel extends Gdn_Model {
  11. public function __construct($Name = '') {
  12. parent::__construct('Tag');
  13. }
  14. public function Save($FormPostValues, $Settings = FALSE) {
  15. // Get the ID of an existing tag with the same name.
  16. $ExistingTag = $this->GetWhere(array('Name' => $FormPostValues['Name'], 'TagID <>' => GetValue('TagID', $FormPostValues)))->FirstRow(DATASET_TYPE_ARRAY);
  17. if ($ExistingTag) {
  18. if (!GetValue('TagID', $FormPostValues))
  19. return $ExistingTag['TagID'];
  20. // This tag will be merged with the existing one.
  21. $Px = $this->Database->DatabasePrefix;
  22. $FromID = $FormPostValues['TagID'];
  23. $ToID = $ExistingTag['TagID'];
  24. try {
  25. $this->Database->BeginTransaction();
  26. // Delete all of the overlapping tags.
  27. $Sql = "delete tg.*
  28. from {$Px}TagDiscussion tg
  29. join {$Px}TagDiscussion tg2
  30. on tg.DiscussionID = tg2.DiscussionID
  31. and tg.TagID = :FromID and tg2.TagID = :ToID";
  32. $this->Database->Query($Sql, array(':FromID' => $FromID, ':ToID' => $ToID));
  33. // Update the tagged discussions.
  34. $Sql = "update {$Px}TagDiscussion
  35. set TagID = :ToID
  36. where TagID = :FromID";
  37. $this->Database->Query($Sql, array(':FromID' => $FromID, ':ToID' => $ToID));
  38. // Update the counts.
  39. $Sql = "update {$Px}Tag t
  40. set CountDiscussions = (
  41. select count(DiscussionID)
  42. from {$Px}TagDiscussion td
  43. where td.TagID = t.TagID)
  44. where t.TagID = :ToID";
  45. $this->Database->Query($Sql, array(':ToID' => $ToID));
  46. // Delete the old tag.
  47. $Sql = "delete from {$Px}Tag where TagID = :FromID";
  48. $this->Database->Query($Sql, array(':FromID' => $FromID));
  49. $this->Database->CommitTransaction();
  50. } catch (Exception $Ex) {
  51. $this->Database->RollbackTransaction();
  52. throw $Ex;
  53. }
  54. } else {
  55. parent::Save($FormPostValues, $Settings);
  56. }
  57. }
  58. public static function ValidateTag($Tag) {
  59. // Tags can't contain whitespace.
  60. if (preg_match('`\s`', $Tag))
  61. return FALSE;
  62. return TRUE;
  63. }
  64. public static function ValidateTags($Tags) {
  65. if (is_string($Tags))
  66. $Tags = self::SplitTags($Tags);
  67. foreach ($Tags as $Tag) {
  68. if (!self::ValidateTag($Tag))
  69. return FALSE;
  70. }
  71. return TRUE;
  72. }
  73. public static function SplitTags($TagsString) {
  74. $Tags = preg_split('`[\s]`', $TagsString);
  75. // Trim each tag.
  76. foreach ($Tags as $Index => $Tag) {
  77. $Tag = trim($Tag);
  78. if (!$Tag)
  79. unset($Tags[$Index]);
  80. else
  81. $Tags[$Index] = $Tag;
  82. }
  83. $Tags = array_unique($Tags);
  84. return $Tags;
  85. }
  86. }