Skip to content
Permalink
Browse files

Put the tagging plugin into core and add an option to enable or disab…

…le tagging in the Tagging settings
  • Loading branch information
DaazKu committed Mar 6, 2017
1 parent e6a217e commit 5be7c8a79784d9ab099b9f651210a240ddcca616
@@ -300,12 +300,13 @@ public function dashboardNavModule_init_handler($sender) {
->addLinkIf('Garden.Settings.Manage', t('Registration'), '/dashboard/settings/registration', 'users.registration', '', $sort)

->addGroup(t('Forum Settings'), 'forum', '', ['after' => 'users'])
->addLinkIf('Garden.Settings.Manage', t('Tagging'), 'settings/tagging', 'forum.tagging', $sort)
->addGroup(t('Reputation'), 'reputation', '', ['after' => 'forum'])
->addGroup(t('Addons'), 'add-ons', '', ['after' => 'reputation'])
->addLinkIf('Garden.Settings.Manage', t('Social Connect'), '/social/manage', 'add-ons.social', '', $sort)
->addLinkIf('Garden.Settings.Manage', t('Plugins'), '/dashboard/settings/plugins', 'add-ons.plugins', '', $sort)
->addLinkIf('Garden.Settings.Manage', t('Applications'), '/dashboard/settings/applications', 'add-ons.applications', '', $sort)
->addLinkIf('Garden.Settings.Manage', t('Locales'), '/dashboard/settings/locales', 'add-ons.locales', '', $sort)
->addLinkIf('Garden.Settings.Manage', t('Social Connect'), '/social/manage', 'add-ons.social', '', $sort)
->addLinkIf('Garden.Settings.Manage', t('Plugins'), '/dashboard/settings/plugins', 'add-ons.plugins', '', $sort)
->addLinkIf('Garden.Settings.Manage', t('Applications'), '/dashboard/settings/applications', 'add-ons.applications', '', $sort)
->addLinkIf('Garden.Settings.Manage', t('Locales'), '/dashboard/settings/locales', 'add-ons.locales', '', $sort)

->addGroup(t('Site Settings'), 'site-settings', '', ['after' => 'reputation'])
->addLinkIf('Garden.Settings.Manage', t('Outgoing Email'), '/dashboard/settings/email', 'site-settings.email', '', $sort)
@@ -340,6 +341,214 @@ public function settingsController_render_before($sender) {
}
}

/**
* List all tags and allow searching
*
* @param SettingsController $Sender
*/
public function settingsController_tagging_create($Sender, $Search = null, $Type = null, $Page = null) {
$Sender->title('Tagging');
$Sender->setHighlightRoute('settings/tagging');
$Sender->addJSFile('tagadmin.js', 'plugins/Tagging');
$SQL = Gdn::sql();

/** @var Gdn_Form $form */
$form = $Sender->Form;

if ($form->authenticatedPostBack()) {
$formValue = (bool)$form->getFormValue('Vanilla.Tagging.EnableUI');
saveToConfig('Vanilla.Tagging.EnableUI', $formValue);
}

// Get all tag types
$TagModel = TagModel::instance();
$TagTypes = $TagModel->getTagTypes();


list($Offset, $Limit) = offsetLimit($Page, 100);
$Sender->setData('_Limit', $Limit);

if ($Search) {
$SQL->like('Name', $Search, 'right');
}

$queryType = $Type;

if (strtolower($Type) == 'all' || $Search || $Type === null) {
$queryType = false;
$Type = '';
}

// This type doesn't actually exist, but it will represent the
// blank types in the column.
if (strtolower($Type) == 'tags') {
$queryType = '';
}

if (!$Search && ($queryType !== false)) {
$SQL->where('Type', $queryType);
}

$TagTypes = array_change_key_case($TagTypes, CASE_LOWER);

// Store type for view
$TagType = (!empty($Type))
? $Type
: 'All';
$Sender->setData('_TagType', $TagType);

// Store tag types
$Sender->setData('_TagTypes', $TagTypes);

// Determine if new tags can be added for the current type.
$CanAddTags = (!empty($TagTypes[$Type]['addtag']) && $TagTypes[$Type]['addtag'])
? 1
: 0;
$CanAddTags &= CheckPermission('Vanilla.Tagging.Add');

$Sender->setData('_CanAddTags', $CanAddTags);

$Data = $SQL
->select('t.*')
->from('Tag t')
->orderBy('t.CountDiscussions', 'desc')
->limit($Limit, $Offset)
->get()->resultArray();

$Sender->setData('Tags', $Data);

if ($Search) {
$SQL->like('Name', $Search, 'right');
}

// Make sure search uses its own search type, so results appear
// in their own tab.
$Sender->Form->Action = url('/settings/tagging/?type='.$TagType);

// Search results pagination will mess up a bit, so don't provide a type
// in the count.
$RecordCountWhere = array('Type' => $queryType);
if ($queryType === false) {
$RecordCountWhere = [];
}
if ($Search) {
$RecordCountWhere = array();
}

$Sender->setData('RecordCount', $SQL->getCount('Tag', $RecordCountWhere));


$Sender->render('tagging');
}

/**
*
*
* @param $Sender
* @return mixed
* @throws Exception
*/
public function settingsController_tags_create($Sender, $action) {
$Sender->permission('Garden.Settings.Manage');
switch($action) {
case 'delete':
$TagID = val(1, $Sender->RequestArgs);
$TagModel = new TagModel();
$Tag = $TagModel->getID($TagID, DATASET_TYPE_ARRAY);
if ($Sender->Form->authenticatedPostBack()) {
// Delete tag & tag relations.
$SQL = Gdn::sql();
$SQL->delete('TagDiscussion', array('TagID' => $TagID));
$SQL->delete('Tag', array('TagID' => $TagID));

$Sender->informMessage(formatString(t('<b>{Name}</b> deleted.'), $Tag));
$Sender->jsonTarget("#Tag_{$Tag['TagID']}", null, 'Remove');
}

$Sender->render('blank', 'utility', 'dashboard');
break;
case 'edit':
$Sender->setHighlightRoute('settings/tagging');
$Sender->title(t('Edit Tag'));
$TagID = val(1, $Sender->RequestArgs);

// Set the model on the form.
$TagModel = new TagModel;
$Sender->Form->setModel($TagModel);
$Tag = $TagModel->getID($TagID);
$Sender->Form->setData($Tag);

// Make sure the form knows which item we are editing.
$Sender->Form->addHidden('TagID', $TagID);

if ($Sender->Form->authenticatedPostBack()) {
// Make sure the tag is valid
$TagData = $Sender->Form->getFormValue('Name');
if (!TagModel::validateTag($TagData)) {
$Sender->Form->addError('@'.t('ValidateTag', 'Tags cannot contain commas.'));
}

// Make sure that the tag name is not already in use.
if ($TagModel->getWhere(array('TagID <>' => $TagID, 'Name' => $TagData))->numRows() > 0) {
$Sender->setData('MergeTagVisible', true);
if (!$Sender->Form->getFormValue('MergeTag')) {
$Sender->Form->addError('The specified tag name is already in use.');
}
}

if ($Sender->Form->Save()) {
$Sender->informMessage(t('Your changes have been saved.'));
}
}

$Sender->render('tags');
break;
case 'add':
default:
$Sender->setHighlightRoute('settings/tagging');
$Sender->title('Add Tag');

// Set the model on the form.
$TagModel = new TagModel;
$Sender->Form->setModel($TagModel);

// Add types if allowed to add tags for it, and not '' or 'tags', which
// are the same.
$TagType = Gdn::request()->get('type');
if (strtolower($TagType) != 'tags'
&& $TagModel->canAddTagForType($TagType)
) {
$Sender->Form->addHidden('Type', $TagType, true);
}

if ($Sender->Form->authenticatedPostBack()) {
// Make sure the tag is valid
$TagName = $Sender->Form->getFormValue('Name');
if (!TagModel::validateTag($TagName)) {
$Sender->Form->addError('@'.t('ValidateTag', 'Tags cannot contain commas.'));
}

$TagType = $Sender->Form->getFormValue('Type');
if (!$TagModel->canAddTagForType($TagType)) {
$Sender->Form->addError('@'.t('ValidateTagType', 'That type does not accept manually adding new tags.'));
}

// Make sure that the tag name is not already in use.
if ($TagModel->getWhere(array('Name' => $TagName))->numRows() > 0) {
$Sender->Form->addError('The specified tag name is already in use.');
}

$Saved = $Sender->Form->save();
if ($Saved) {
$Sender->informMessage(t('Your changes have been saved.'));
}
}

$Sender->render('tags');
break;
}
}

/**
* Set P3P header because IE won't allow cookies thru the iFrame without it.
*
@@ -0,0 +1,156 @@
<?php if (!defined('APPLICATION')) exit(); ?>
<?php
$TagType = $this->data('_TagType');
$TagTypes = $this->data('_TagTypes');
$CanAddTags = $this->data('_CanAddTags');
/** @var Gdn_Form $form */
$form = $this->Form;

$desc = t('Tags are keywords that users can assign to discussions to help categorize their question with similar questions.');
helpAsset(sprintf(t('About %s'), t('Tagging')), $desc);

if (strtolower($TagType) == 'all' || strtolower($TagType) == 'tags') {
// Only show add button if filter type supports adding new tags.
echo heading(t($this->data('Title')), t('Add Tag'), '/settings/tags/add?type=Tag', 'js-modal btn btn-primary');
} else {
echo heading(t($this->data('Title')));
}

echo subheading(t('Settings'));
echo $form->open(['method' => 'post', 'class' => 'full-border']);
echo $form->errors(); ?>
<div class="form-group">
<div class="label-wrap-wide">
<?php echo $form->label(t('Enable Tagging'), 'Vanilla.Tagging.EnableUI'); ?>
<?php echo '<div class="info">'.t('Enable tagging on your forum.').'</div>' ?>
</div>
<div class="input-wrap-right">
<?php
$options = c('Vanilla.Tagging.EnableUI') ? ['checked'] : [];
echo $form->toggle('Vanilla.Tagging.EnableUI', '', $options); ?>
</div>
</div>
<?php
echo $form->close('Save');

echo subheading(t('Tags'));
?>
<div class="toolbar flex-wrap">
<div class="search-wrap input-wrap toolbar-main">
<?php
$info = sprintf(t('%s tag(s) found.'), $this->data('RecordCount'));
$placeholder = t('Search for a tag.', 'Search for all or part of a tag.');
$form->Method = 'get';
echo $form->searchForm('search', '/settings/tagging', ['placeholder' => $placeholder], $info);
?>
</div>
<div class="btn-group">
<?php foreach ($TagTypes as $TagTypeName => $TagMeta): ?>
<?php
$TagName = ($TagMeta['key'] == '' || strtolower($TagMeta['key']) == 'tags')
? 'Tags'
: $TagTypeName;

$TagName = (!empty($TagMeta['plural']))
? $TagMeta['plural']
: $TagName;

if ($TagMeta['key'] == '') {
$TagMeta['key'] = (!empty($TagMeta['plural']))
? $TagMeta['plural']
: $TagMeta['key'];
}

$CurrentTab = '';
if (strtolower($TagType) == strtolower($TagMeta['key'])
|| (!empty($TagMeta['plural']) && strtolower($TagType) == strtolower($TagMeta['plural']))
) {
$CurrentTab = 'active';
}

$TabUrl = url('/settings/tagging/?type='.strtolower($TagMeta['key']));
?>
<a href="<?php echo $TabUrl; ?>" class="<?php echo $CurrentTab; ?> btn btn-secondary">
<?php echo ucwords(strtolower($TagName)); ?>
</a>
<?php endforeach; ?>
</div>
<?php PagerModule::write(array('Sender' => $this, 'View' => 'pager-dashboard')); ?>
</div>
<div class="table-wrap">
<table class="Tags table-data js-tj">
<thead>
<tr>
<th class="column-md"><?php echo t('Tag') ?></th>
<th><?php echo t('Created By') ?></th>
<th><?php echo t('Type') ?></th>
<th class="column-md"><?php echo t('Date Added'); ?></th>
<th class="column-xs"><?php echo t('Count'); ?></th>
<?php if ($CanAddTags) { ?>
<th class="column-sm"></th>
<?php } ?>
</tr>
</thead>
<?php
$Session = Gdn::session();
$TagCount = $this->data('RecordCount');
$Tags = $this->data('Tags'); ?>
<tbody>
<?php
foreach ($Tags as $Tag) {
$CssClass = 'TagAdmin';
$Title = '';
$Special = FALSE;
$type = val('Type', $Tag);
if (empty($type)) {
$type = t('Tag');
}
$userModel = new UserModel();
$createdBy = $userModel->getID(val('InsertUserID', $Tag));
$dateInserted = Gdn_Format::date(val('DateInserted', $Tag), '%e %b %Y');
$count = val('CountDiscussions', $Tag, 0);
$displayName = TagFullName($Tag);

if (val('Type', $Tag)) {
$Special = TRUE;
$CssClass .= " Tag-Special Tag-{$Tag['Type']}";
$Title = t('This is a special tag.');
}

?>
<tr id="<?php echo "Tag_{$Tag['TagID']}"; ?>" class="<?php echo $CssClass; ?>"
title="<?php echo $Title; ?>">
<td class="tag-name">
<?php echo anchor(htmlspecialchars($displayName), '/discussions/tagged/'.val('Name', $Tag)); ?>
</td>
<td class="created-by">
<?php echo userAnchor($createdBy); ?>
</td>
<td class="type">
<?php echo $type; ?>
</td>
<td class="date">
<?php echo $dateInserted; ?>
</td>
<td class="count">
<?php echo $count; ?>
</td>
<?php if ($CanAddTags) { ?>
<td class="options">
<div class="btn-group">
<?php
if (!$Special) {
echo anchor(dashboardSymbol('edit'), "/settings/tags/edit/{$Tag['TagID']}", 'js-modal btn btn-icon', ['aria-label' => t('Edit'), 'title' => t('Edit')]);
echo anchor(dashboardSymbol('delete'), "/settings/tags/delete/{$Tag['TagID']}", 'js-modal-confirm btn btn-icon', ['aria-label' => t('Delete'), 'title' => t('Delete'), 'data-body' => sprintf(t('Are you sure you want to delete this %s?'), t('tag'))]);
}
?>
</div>
</td>
<?php } ?>
</tr>
<?php
}
?>
</tbody>
</table>
</div>

0 comments on commit 5be7c8a

Please sign in to comment.
You can’t perform that action at this time.