Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added labels to create/edit issue forms

  • Loading branch information...
commit c3e029247dfa930a6e8b2810b30c162f819c4fef 1 parent a1ac483
Evan Coury EvanDotPro authored
6 application/modules/default/forms/Issue/Base.php
View
@@ -50,9 +50,13 @@ public function init()
));
$milestones = new Issues_Form_Element_MilestoneSelect('milestones');
- $milestones->setLabel('Milestones');
+ $milestones->setLabel($this->translate('milestones'));
$this->addElement($milestones);
+ $labels = new Issues_Form_Element_LabelSelect('labels');
+ $labels->setLabel($this->translate('labels'));
+ $this->addElement($labels);
+
$this->addSubForm(new Default_Form_Permission_Base(), 'permissions');
$this->addElement('submit', 'submit', array(
9 application/modules/default/forms/Issue/Edit.php
View
@@ -18,11 +18,20 @@ public function setDefaultValues(Default_Model_Issue $issue)
$milestoneIds[] = $i->getMilestoneId();
}
+ $labels = Zend_Registry::get('Default_DiContainer')
+ ->getLabelService()
+ ->getLabelsByIssue($issue);
+ $labelIds = array();
+ foreach ($labels as $i) {
+ $labelIds[] = $i->getLabelId();
+ }
+
$data = array(
'title' => $issue->getTitle(),
'project' => $issue->getProject()->getProjectId(),
'description' => $issue->getDescription(),
'milestones' => $milestoneIds,
+ 'labels' => $labelIds,
'status' => $issue->getStatus()
);
10 application/modules/default/models/Issue.php
View
@@ -345,10 +345,18 @@ public function getLabels()
*/
public function setLabels($labels)
{
- $this->_labels = Zend_Registry::get('Default_DiContainer')->getLabelService()->getLabelDetect($labels);
+ if (is_null($labels)) return $this;
+ $labels = Zend_Registry::get('Default_DiContainer')->getLabelService()->getLabelDetect($labels);
+ $this->_labels = is_array($labels) ? $labels : array($labels);
return $this;
}
+ public function hasLabel($label)
+ {
+ $label = Zend_Registry::get('Default_DiContainer')->getLabelService()->getLabelDetect($label);
+ return in_array($label, $this->_labels);
+ }
+
public function getHistory()
{
return Zend_Registry::get('Default_DiContainer')->getIssueService()->getHistory($this);
31 application/modules/default/models/mappers/Issue.php
View
@@ -169,7 +169,7 @@ public function updateLastUpdate($issue)
return $db->update($this->getTableName(), $data, $db->quoteInto('issue_id = ?', $issue->getIssueId()));
}
- public function addLabelToIssue(Default_Model_Issue $issue, Default_Model_Label $label)
+ public function addLabelToIssue(Default_Model_Issue $issue, Default_Model_Label $label, $audit = true)
{
$data = array(
'issue_id' => $issue->getIssueId(),
@@ -180,7 +180,9 @@ public function addLabelToIssue(Default_Model_Issue $issue, Default_Model_Label
try {
$db->insert('issue_label_linker', $data);
- $this->auditTrail($issue, 'add-label', '', '', $label->getLabelId());
+ if ($audit) {
+ $this->auditTrail($issue, 'add-label', '', '', $label->getLabelId());
+ }
} catch (Exception $e) {
// probably a duplicate key
}
@@ -278,6 +280,31 @@ public function getIssuesByMilestone($milestone, $status = null)
return $this->_rowsToModels($rows);
}
+ public function updateIssueLabels($issue, $labels, $audit = false)
+ {
+ if (!$issue instanceof Default_Model_Issue) return false;
+ $read = $this->getReadAdapter();
+ $write = $this->getWriteAdapter();
+ $issueService = Zend_Registry::get('Default_DiContainer')->getIssueService();
+ $labelService = Zend_Registry::get('Default_DiContainer')->getLabelService();
+ // Add any new labels
+ $issue->setLabels($labelService->getLabelsByIssue($issue));
+ foreach ($labels as $label) {
+ if (!$issue->hasLabel($label)) {
+ $issueService->addLabelToIssue($issue, $label);
+ }
+ }
+ // Delete any removed labels
+ $oldLabels = $issue->getLabels();
+ $issue->setLabels($labels);
+ foreach ($oldLabels as $label) {
+ if (!$issue->hasLabel($label)) {
+ $issueService->removeLabelFromIssue($issue, $label);
+ }
+ }
+ return true;
+ }
+
public function updateIssueMilestones($issue, $milestones, $audit = false)
{
$read = $this->getReadAdapter();
4 application/modules/default/models/mappers/Label.php
View
@@ -24,10 +24,10 @@ public function getAllLabels($counts = false, $project = false)
{
$db = $this->getReadAdapter();
$sql = $db->select()
- ->from(array('l' => $this->getTableName()));
+ ->from(array('l' => $this->getTableName()), 'l.*');
if ($counts === true) {
- $sql->joinLeft(array('ill'=>'issue_label_linker'), 'ill.label_id = l.label_id')
+ $sql->joinLeft(array('ill'=>'issue_label_linker'), 'ill.label_id = l.label_id', 'ill.issue_id')
->columns(array('count'=>'COUNT(ill.issue_id)'))
->group('l.label_id');
}
24 application/modules/default/services/Issue.php
View
@@ -84,23 +84,30 @@ public function createFromForm(Default_Form_Issue_Create $form)
->setCreatedBy(Zend_Auth::getInstance()->getIdentity())
->setAssignedTo($form->getValue('assigned_to'))
->setPrivate($permissions['private'] ? true : false);
- $return = $this->_mapper->save($issue);
+ $issue = $this->_mapper->save($issue);
$milestones = $form->getValue('milestones');
if ($milestones) {
foreach ($milestones as $i) {
Zend_Registry::get('Default_DiContainer')
->getMilestoneService()
- ->addIssueToMilestone($i, $return, false);
+ ->addIssueToMilestone($i, $issue, false);
+ }
+ }
+
+ $labels = $form->getValue('labels');
+ if ($labels) {
+ foreach ($labels as $i) {
+ $this->addLabelToIssue($issue, $i, false);
}
}
if ($permissions['private']) {
Zend_Registry::get('Default_DiContainer')->getAclService()
- ->addResourceRecord($permissions['roles'], 'issue', $return);
+ ->addResourceRecord($permissions['roles'], 'issue', $issue);
}
- return $return;
+ return $issue;
}
public function updateFromForm(Default_Form_Issue_Edit $form, $issueId)
@@ -122,6 +129,9 @@ public function updateFromForm(Default_Form_Issue_Edit $form, $issueId)
->setPrivate($form->getSubform('permissions')->getElement('private')->isChecked());
$result = $this->_mapper->save($issue);
+ $labels = $form->getValue('labels');
+ $this->_mapper->updateIssueLabels($issue, $labels, true);
+
$milestones = $form->getValue('milestones');
$this->_mapper->updateIssueMilestones($issue, $milestones, true);
@@ -168,7 +178,7 @@ public function canEditIssue(Default_Model_Issue $issue)
return false;
}
- public function addLabelToIssue($issue, $label)
+ public function addLabelToIssue($issue, $label, $audit = true)
{
if (!($issue instanceof Default_Model_Issue)) {
$issue = $this->_mapper->getIssueById($issue);
@@ -179,10 +189,10 @@ public function addLabelToIssue($issue, $label)
}
if (!($label instanceof Default_Model_Label)) {
- $label = Zend_Registry::get('Default_DiContainer')->getLabelService()->getLabelById($label);
+ $label = Zend_Registry::get('Default_DiContainer')->getLabelService()->getLabelDetect($label);
}
- $this->_mapper->addLabelToIssue($issue, $label);
+ $this->_mapper->addLabelToIssue($issue, $label, $audit);
}
public function removeLabelFromIssue($issue, $label)
3  application/modules/default/services/Label.php
View
@@ -77,9 +77,6 @@ public function getLabelDetect($label)
$label = $this->getLabelDetect(explode(' ',$label));
}
}
- if (is_array($label)) {
- return array_values($label);
- }
return $label;
}
}
10 library/Issues/Form/Element/LabelSelect.php
View
@@ -0,0 +1,10 @@
+<?php
+class Issues_Form_Element_LabelSelect extends Zend_Form_Element_Multiselect
+{
+ public function init()
+ {
+ $labels = Zend_Registry::get('Default_DiContainer')->getLabelService()
+ ->getLabelsForSelect();
+ $this->setMultiOptions($labels);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.