PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Core/Form/Tag.php

https://github.com/michaelmcandrew/vaw
PHP | 215 lines | 135 code | 23 blank | 57 comment | 29 complexity | 9696402fddc000dffab77ae840f5e087 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2011
  31. * $Id$
  32. *
  33. */
  34. /**
  35. * This class generates form element for free tag widget
  36. *
  37. */
  38. class CRM_Core_Form_Tag
  39. {
  40. public $_entityTagValues;
  41. /**
  42. * Function to build tag widget if correct parent is passed
  43. *
  44. * @param object $form form object
  45. * @param string $parentName parent name ( tag name)
  46. * @param string $entityTable entitytable 'eg: civicrm_contact'
  47. * @param int $entityId entityid 'eg: contact id'
  48. *
  49. * @return void
  50. * @access public
  51. * @static
  52. */
  53. static function buildQuickForm( &$form, $parentNames, $entityTable, $entityId = null, $skipTagCreate = false,
  54. $skipEntityAction = false, $searchMode = false ) {
  55. $tagset = $form->_entityTagValues = array( );
  56. $mode = null;
  57. foreach( $parentNames as &$parentNameItem ) {
  58. // get the parent id for tag list input for keyword
  59. $parentId = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_Tag', $parentNameItem, 'id', 'name' );
  60. // check if parent exists
  61. $entityTags = array( );
  62. if ( $parentId ) {
  63. $tagsetItem = 'parentId_'.$parentId;
  64. $tagset[$tagsetItem]['parentName'] = $parentNameItem;
  65. $tagset[$tagsetItem]['parentID' ] = $parentId;
  66. //tokeninput url
  67. $qparams = "parentId={$parentId}";
  68. if ( $searchMode ) {
  69. $qparams .= '&search=1';
  70. }
  71. $tagUrl = CRM_Utils_System::url( 'civicrm/ajax/taglist',
  72. $qparams,
  73. false, null, false );
  74. $tagset[$tagsetItem]['tagUrl' ] = $tagUrl;
  75. $tagset[$tagsetItem]['entityTable' ] = $entityTable;
  76. $tagset[$tagsetItem]['skipTagCreate' ] = $skipTagCreate;
  77. $tagset[$tagsetItem]['skipEntityAction'] = $skipEntityAction;
  78. switch ( $entityTable ) {
  79. case 'civicrm_activity':
  80. $tagsetElementName = "activity_taglist";
  81. $mode = 'activity';
  82. break;
  83. case 'civicrm_case' :
  84. $tagsetElementName = "case_taglist";
  85. $mode = 'case';
  86. break;
  87. default:
  88. $tagsetElementName = "contact_taglist";
  89. $mode = 'contact';
  90. }
  91. $tagset[$tagsetItem]['tagsetElementName' ] = $tagsetElementName;
  92. $form->add( 'text', "{$tagsetElementName}[{$parentId}]", null );
  93. if ( $entityId ) {
  94. $tagset[$tagsetItem]['entityId'] = $entityId;
  95. require_once 'CRM/Core/BAO/EntityTag.php';
  96. $entityTags = CRM_Core_BAO_EntityTag::getChildEntityTags( $parentId, $entityId, $entityTable );
  97. } else {
  98. switch ( $entityTable ) {
  99. case 'civicrm_activity':
  100. if ( !empty( $form->_submitValues['activity_taglist'] ) &&
  101. CRM_Utils_Array::value( $parentId, $form->_submitValues['activity_taglist']) ) {
  102. $allTags = CRM_Core_Pseudoconstant::tag( );
  103. $tagIds = explode( ',', $form->_submitValues['activity_taglist'][$parentId] );
  104. foreach( $tagIds as $tagId ) {
  105. if ( is_numeric( $tagId ) ) {
  106. $tagName = $allTags[$tagId];
  107. } else {
  108. $tagName = $tagId;
  109. }
  110. $entityTags[$tagId] = array( 'id' => $tagId,
  111. 'name' => $tagName );
  112. }
  113. }
  114. break;
  115. case 'civicrm_case':
  116. if ( !empty( $form->_submitValues['case_taglist'] ) &&
  117. CRM_Utils_Array::value( $parentId, $form->_submitValues['case_taglist']) ) {
  118. $allTags = CRM_Core_Pseudoconstant::tag( );
  119. $tagIds = explode( ',', $form->_submitValues['case_taglist'][$parentId] );
  120. foreach( $tagIds as $tagId ) {
  121. if ( is_numeric( $tagId ) ) {
  122. $tagName = $allTags[$tagId];
  123. } else {
  124. $tagName = $tagId;
  125. }
  126. $entityTags[$tagId] = array( 'id' => $tagId,
  127. 'name' => $tagName );
  128. }
  129. }
  130. break;
  131. default:
  132. if ( !empty($form->_formValues['contact_tags']) ) {
  133. require_once 'CRM/Core/BAO/Tag.php';
  134. $contactTags = CRM_Core_BAO_Tag::getTagsUsedFor( 'civicrm_contact', true, false, $parentId );
  135. foreach( array_keys($form->_formValues['contact_tags']) as $tagId ) {
  136. if ( CRM_Utils_Array::value($tagId, $contactTags) ) {
  137. $tagName = $tagId;
  138. if ( is_numeric($tagId) ) $tagName = $contactTags[$tagId];
  139. $entityTags[$tagId] = array( 'id' => $tagId,
  140. 'name' => $tagName );
  141. }
  142. }
  143. }
  144. }
  145. }
  146. if ( !empty( $entityTags ) ) {
  147. // assign as simple array for display in smarty
  148. $tagset[$tagsetItem]['entityTagsArray'] = $entityTags;
  149. // assign as json for js widget
  150. $tagset[$tagsetItem]['entityTags'] = json_encode( array_values( $entityTags ) );
  151. if ( !empty( $form->_entityTagValues ) ) {
  152. $form->_entityTagValues = CRM_Utils_Array::crmArrayMerge( $entityTags, $form->_entityTagValues );
  153. } else {
  154. $form->_entityTagValues = $entityTags;
  155. }
  156. }
  157. }
  158. }
  159. if ( !empty( $tagset ) ) {
  160. $form->assign( "tagsetInfo_$mode", $tagset );
  161. }
  162. }
  163. /**
  164. * Function to save entity tags when it is not save used AJAX
  165. *
  166. */
  167. static function postProcess( &$params, $entityId, $entityTable = 'civicrm_contact', &$form ) {
  168. foreach( $params as $value ) {
  169. if ( !$value ) {
  170. continue;
  171. }
  172. $tagsIDs = explode( ',', $value );
  173. $insertValues = array( );
  174. $insertSQL = null;
  175. if ( !empty( $tagsIDs ) ) {
  176. foreach( $tagsIDs as $tagId ) {
  177. if ( is_numeric( $tagId ) ) {
  178. if ( $form->_action != CRM_Core_Action::UPDATE ) {
  179. $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) ";
  180. } else if ( !array_key_exists( $tagId, $form->_entityTagValues ) ) {
  181. $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) ";
  182. }
  183. }
  184. }
  185. if ( !empty( $insertValues ) ) {
  186. $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table ) VALUES '. implode( ', ', $insertValues ) . ';';
  187. CRM_Core_DAO::executeQuery( $insertSQL );
  188. }
  189. }
  190. }
  191. }
  192. }