X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FCore%2FForm%2FTag.php;h=f7ffb32f57adb7825b987843948f000a18ed1e66;hb=64e59809d10c3ff342718a28f42983ec217dc282;hp=7263d9371bf3966f35036205c23c004ec7ec48f2;hpb=53128a92d19ec8f0526b48a4a0c044bb0d671d84;p=civicrm-core.git diff --git a/CRM/Core/Form/Tag.php b/CRM/Core/Form/Tag.php index 7263d9371b..05731dff18 100644 --- a/CRM/Core/Form/Tag.php +++ b/CRM/Core/Form/Tag.php @@ -1,9 +1,9 @@ _entityTagValues = array(); $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 - $entityTags = array(); if ($parentId) { - $tagsetItem = 'parentId_' . $parentId; - $tagset[$tagsetItem]['parentName'] = $parentNameItem; + $tagsetItem = $tagsetElementName . 'parentId_' . $parentId; $tagset[$tagsetItem]['parentID'] = $parentId; - //tokeninput url - $qparams = "parentId={$parentId}"; - - if ($searchMode) { - $qparams .= '&search=1'; - } - - $tagUrl = CRM_Utils_System::url('civicrm/ajax/taglist', $qparams, FALSE, NULL, FALSE); - - $tagset[$tagsetItem]['tagUrl'] = $tagUrl; - $tagset[$tagsetItem]['entityTable'] = $entityTable; - $tagset[$tagsetItem]['skipTagCreate'] = $skipTagCreate; - $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction; - - 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->add('text', "{$tagsetElementName}[{$parentId}]", NULL); - } + + $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; $entityTags = CRM_Core_BAO_EntityTag::getChildEntityTags($parentId, $entityId, $entityTable); - } - else { - - switch ($entityTable) { - case 'civicrm_activity': - if (!empty($form->_submitValues['activity_taglist']) && !empty($form->_submitValues['activity_taglist'][$parentId])) { - $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); - $tagIds = explode(',', $form->_submitValues['activity_taglist'][$parentId]); - foreach ($tagIds as $tagId) { - if (is_numeric($tagId)) { - $tagName = $allTags[$tagId]; - } - else { - $tagName = $tagId; - } - $entityTags[$tagId] = array( - 'id' => $tagId, - 'name' => $tagName, - ); - } - } - break; - - case 'civicrm_case': - if (!empty($form->_submitValues['case_taglist']) && !empty($form->_submitValues['case_taglist'][$parentId])) { - $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); - $tagIds = explode(',', $form->_submitValues['case_taglist'][$parentId]); - foreach ($tagIds as $tagId) { - if (is_numeric($tagId)) { - $tagName = $allTags[$tagId]; - } - else { - $tagName = $tagId; - } - $entityTags[$tagId] = array( - 'id' => $tagId, - 'name' => $tagName, - ); - } - } - break; - case 'civicrm_file': - $numAttachments = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments'); - for ($i = 1; $i <= $numAttachments; $i++) { - $tagset[$i] = $tagset[$tagsetItem]; - $tagset[$i]['tagsetElementName'] = "attachment_taglist_$i"; - $form->add('text', "attachment_taglist_{$i}[{$parentId}]", NULL); - if (!empty($form->_submitValues["attachment_taglist_$i"]) && !empty($form->_submitValues["attachment_taglist_$i"][$parentId])) { - $allTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE)); - $tagIds = explode(',', $form->_submitValues["attachment_taglist_$i"][$parentId]); - foreach ($tagIds as $tagId) { - if (is_numeric($tagId)) { - $tagName = $allTags[$tagId]; - } - else { - $tagName = $tagId; - } - $entityTags[$tagId] = array( - 'id' => $tagId, - 'name' => $tagName, - ); - } - } - } - unset($tagset[$tagsetItem]); - break; - - default: - if (!empty($form->_formValues['contact_tags'])) { - $contactTags = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', TRUE, FALSE, $parentId); - - foreach (array_keys($form->_formValues['contact_tags']) as $tagId) { - if (!empty($contactTags[$tagId])) { - $tagName = $tagId; - if (is_numeric($tagId)) { - $tagName = $contactTags[$tagId]; - } - - $entityTags[$tagId] = array( - 'id' => $tagId, - 'name' => $tagName, - ); - } - } - } + if ($entityTags) { + $form->setDefaults(array("{$tagsetElementName}[{$parentId}]" => implode(',', array_keys($entityTags)))); } } - - if (!empty($entityTags)) { - // assign as simple array for display in smarty - $tagset[$tagsetItem]['entityTagsArray'] = $entityTags; - // assign as json for js widget - $tagset[$tagsetItem]['entityTags'] = json_encode(array_values($entityTags)); - - if (!empty($form->_entityTagValues)) { - $form->_entityTagValues = CRM_Utils_Array::crmArrayMerge($entityTags, $form->_entityTagValues); - } - else { - $form->_entityTagValues = $entityTags; - } + else { + $skipEntityAction = TRUE; } + $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction; } } @@ -223,18 +104,24 @@ class CRM_Core_Form_Tag { // assign current tagsets which is used in postProcess $form->_tagsetInfo = $tagset; $form->assign("tagsetType", $mode); - $form->assign("tagsetInfo_$mode", $tagset); + // Merge this tagset info with possibly existing info in the template + $tagsetInfo = (array) $form->get_template_vars("tagsetInfo"); + 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 @@ -253,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] = ''; } @@ -264,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; } } @@ -300,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);