Merge pull request #4772 from jitendrapurohit/CRM-15750
[civicrm-core.git] / CRM / Core / Form / Tag.php
index cbe2f38b965d6fe884f1389010af99b275e6a384..05731dff180fc1425a89acaa1582df8aa3b7b3db 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -41,16 +41,15 @@ class CRM_Core_Form_Tag {
   public $_entityTagValues;
 
   /**
-   * Function to build tag widget if correct parent is passed
+   * Build tag widget if correct parent is passed
    *
-   * @param CRM_Core_Form  $form form object
-   * @param string  $parentNames parent name ( tag name)
-   * @param string  $entityTable entitytable 'eg: civicrm_contact'
-   * @param int     $entityId    entityid  'eg: contact id'
+   * @param CRM_Core_Form $form form object
+   * @param string $parentNames parent name ( tag name)
+   * @param string $entityTable entitytable 'eg: civicrm_contact'
+   * @param int $entityId entityid  'eg: contact id'
    * @param boolean $skipTagCreate true if tag need be created using ajax
    * @param boolean $skipEntityAction true if need to add entry in entry table via ajax
-   * @param boolean $searchMode true if widget is used in search eg: advanced search
-   * @param string  $tagsetElementName if you need to create tagsetlist with specific name
+   * @param string $tagsetElementName if you need to create tagsetlist with specific name
    *
    * @return void
    * @access public
@@ -62,53 +61,30 @@ class CRM_Core_Form_Tag {
     $form->assign("isTagset", FALSE);
     $mode = NULL;
 
-    foreach ($parentNames as & $parentNameItem) {
+    foreach ($parentNames as &$parentNameItem) {
       // get the parent id for tag list input for keyword
       $parentId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $parentNameItem, 'id', 'name');
 
       // check if parent exists
       if ($parentId) {
-        $tagsetItem = 'parentId_' . $parentId;
-        $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction;
+        $tagsetItem = $tagsetElementName . 'parentId_' . $parentId;
         $tagset[$tagsetItem]['parentID'] = $parentId;
 
-        // Fixme: these 3 variables may be unused
-        $tagset[$tagsetItem]['parentName'] = $parentNameItem;
-        $tagset[$tagsetItem]['entityTable'] = $entityTable;
-        $tagset[$tagsetItem]['skipTagCreate'] = $skipTagCreate;
-
-        switch ($entityTable) {
-          case 'civicrm_activity':
-            $tagsetElementName = "activity_taglist";
-            $mode = 'activity';
-            break;
-
-          case 'civicrm_case':
-            $tagsetElementName = "case_taglist";
-            $mode = 'case';
-            break;
-
-          case 'civicrm_file':
-            $mode = 'attachment';
-            break;
-
-          default:
-            $tagsetElementName = "contact_taglist";
-            $mode = 'contact';
+        list(, $mode) = explode('_', $entityTable);
+        if (!$tagsetElementName) {
+          $tagsetElementName = $mode . "_taglist";
         }
-
         $tagset[$tagsetItem]['tagsetElementName'] = $tagsetElementName;
-        if ($tagsetElementName) {
-          $form->addEntityRef("{$tagsetElementName}[{$parentId}]", $parentNameItem, array(
-            'entity' => 'tag',
-            'multiple' => TRUE,
-            'create' => !$skipTagCreate,
-            'api' => array('params' => array('parent_id' => $parentId)),
-            'data-entity_table' => $entityTable,
-            'data-entity_id' => $entityId,
-            'class' => "crm-$mode-tagset",
-          ));
-        }
+
+        $form->addEntityRef("{$tagsetElementName}[{$parentId}]", $parentNameItem, array(
+          'entity' => 'tag',
+          'multiple' => TRUE,
+          'create' => !$skipTagCreate,
+          'api' => array('params' => array('parent_id' => $parentId)),
+          'data-entity_table' => $entityTable,
+          'data-entity_id' => $entityId,
+          'class' => "crm-$mode-tagset",
+        ));
 
         if ($entityId) {
           $tagset[$tagsetItem]['entityId'] = $entityId;
@@ -120,6 +96,7 @@ class CRM_Core_Form_Tag {
         else {
           $skipEntityAction = TRUE;
         }
+        $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction;
       }
     }
 
@@ -127,20 +104,24 @@ class CRM_Core_Form_Tag {
       // assign current tagsets which is used in postProcess
       $form->_tagsetInfo = $tagset;
       $form->assign("tagsetType", $mode);
+      // Merge this tagset info with possibly existing info in the template
       $tagsetInfo = (array) $form->get_template_vars("tagsetInfo");
-      $tagsetInfo[$mode] = $tagset;
+      if (empty($tagsetInfo[$mode])) {
+        $tagsetInfo[$mode] = array();
+      }
+      $tagsetInfo[$mode] = array_merge($tagsetInfo[$mode], $tagset);
       $form->assign("tagsetInfo", $tagsetInfo);
       $form->assign("isTagset", TRUE);
     }
   }
 
   /**
-   * Function to save entity tags when it is not save used AJAX
+   * Save entity tags when it is not save used AJAX
    *
    * @param array   $params      associated array
    * @param int     $entityId    entity id, eg: contact id, activity id, case id, file id
    * @param string  $entityTable entity table
-   * @param object  $form        form object
+   * @param CRM_Core_Form  $form        form object
    *
    * @return void
    * @access public
@@ -159,7 +140,8 @@ class CRM_Core_Form_Tag {
       // in that case we create empty tagset params so that below logic works and tagset are
       // deleted correctly
       foreach ($form->_tagsetInfo as $tagsetName => $tagsetInfo) {
-        $tagsetId = substr($tagsetName, strlen('parentId_'));
+        $tagsetId = explode('parentId_', $tagsetName);
+        $tagsetId = $tagsetId[1];
         if (empty($params[$tagsetId])) {
           $params[$tagsetId] = '';
         }
@@ -170,31 +152,12 @@ class CRM_Core_Form_Tag {
     // are deleted we will have to set $params[tagset id] = '' which is done by above logic
     foreach ($params as $parentId => $value) {
       $newTagIds = array();
-      $realTagIds = array();
+      $tagIds = array();
 
       if ($value) {
-        $tagsIDs = explode(',', $value);
-        foreach ($tagsIDs as $tagId) {
-          if (!is_numeric($tagId)) {
-            // check if user has selected existing tag or is creating new tag
-            // this is done to allow numeric tags etc.
-            $tagValue = explode(':::', $tagId);
-
-            if (isset($tagValue[1]) && $tagValue[1] == 'value') {
-              $tagParams = array(
-                'name' => $tagValue[0],
-                'parent_id' => $parentId,
-              );
-              $tagObject = CRM_Core_BAO_Tag::add($tagParams, CRM_Core_DAO::$_nullArray);
-              $tagId = $tagObject->id;
-            }
-          }
-
-          $realTagIds[] = $tagId;
-          if ($form && $form->_action != CRM_Core_Action::UPDATE) {
-            $newTagIds[] = $tagId;
-          }
-          elseif (!array_key_exists($tagId, $existingTags)) {
+        $tagIds = explode(',', $value);
+        foreach ($tagIds as $tagId) {
+          if ($form && $form->_action != CRM_Core_Action::UPDATE || !array_key_exists($tagId, $existingTags)) {
             $newTagIds[] = $tagId;
           }
         }
@@ -206,8 +169,8 @@ class CRM_Core_Form_Tag {
                     WHERE civicrm_tag.id=civicrm_entity_tag.tag_id
                       AND civicrm_entity_tag.entity_table='{$entityTable}'
                       AND entity_id={$entityId} AND parent_id={$parentId}";
-      if (!empty($realTagIds)) {
-        $deleteSQL .= " AND tag_id NOT IN (" . implode(', ', $realTagIds) . ");";
+      if (!empty($tagIds)) {
+        $deleteSQL .= " AND tag_id NOT IN (" . implode(', ', $tagIds) . ");";
       }
 
       CRM_Core_DAO::executeQuery($deleteSQL);