Advertisement
TeNNoX

TagsAndGroups.php

Aug 28th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.82 KB | None | 0 0
  1. <?php
  2. /*
  3.  +--------------------------------------------------------------------+
  4.  | CiviCRM version 4.4                                                |
  5.  +--------------------------------------------------------------------+
  6.  | Copyright CiviCRM LLC (c) 2004-2013                                |
  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.  *
  30.  * @package CRM
  31.  * @copyright CiviCRM LLC (c) 2004-2013
  32.  * $Id$
  33.  *
  34.  */
  35. class CRM_Contact_Form_Edit_TagsAndGroups {
  36.  
  37.     /**
  38.      * constant to determine which forms we are generating
  39.      *
  40.      * Used by both profile and edit contact
  41.      */
  42.     CONST GROUP = 1, TAG = 2, ALL = 3;
  43.  
  44.     /**
  45.      * This function is to build form elements
  46.      * params object $form object of the form
  47.      *
  48.      * @param Object  $form        the form object that we are operating on
  49.      * @param int     $contactId   contact id
  50.      * @param int     $type        what components are we interested in
  51.      * @param boolean $visibility  visibility of the field
  52.      * @param string  $groupName   if used for building group block
  53.      * @param string  $tagName     if used for building tag block
  54.      * @param string  $fieldName   this is used in batch profile(i.e to build multiple blocks)
  55.      *
  56.      * @static
  57.      * @access public
  58.      */
  59.     static function buildQuickForm(&$form,
  60.     $contactId = 0,
  61.     $type = self::ALL,
  62.     $visibility = FALSE,
  63.     $isRequired = NULL,
  64.     $groupName = 'Group(s)',
  65.     $tagName = 'Tag(s)',
  66.     $fieldName = NULL,
  67.     $groupElementType = 'checkbox'
  68.     ) {
  69.         if (!isset($form->_tagGroup)) {
  70.             $form->_tagGroup = array();
  71.         }
  72.  
  73.         // NYSS 5670
  74.         if (!$contactId && !empty($form->_contactId)) {
  75.             $contactId = $form->_contactId;
  76.         }
  77.  
  78.         $type = (int) $type;
  79.         if ($type & self::GROUP) {
  80.  
  81.             $fName = 'group';
  82.             if ($fieldName) {
  83.                 $fName = $fieldName;
  84.             }
  85.  
  86.             $groupID = isset($form->_grid) ? $form->_grid : NULL;
  87.             if ($groupID && $visibility) {
  88.                 $ids = array($groupID => $groupID);
  89.             }
  90.             else {
  91.                 if ($visibility) {
  92.                     $group = CRM_Core_PseudoConstant::allGroup();
  93.                 }
  94.                 else {
  95.                     $group = CRM_Core_PseudoConstant::group();
  96.                 }
  97.                 $ids = $group;
  98.             }
  99.  
  100.             if ($groupID || !empty($group)) {
  101.                 $groups = CRM_Contact_BAO_Group::getGroupsHierarchy($ids);
  102.  
  103.                 $attributes['skiplabel'] = TRUE;
  104.                 $elements = array();
  105.                 $groupsOptions = array();
  106.                 foreach ($groups as $id => $group) {
  107.                     // make sure that this group has public visibility
  108.                     if ($visibility &&
  109.                     $group['visibility'] == 'User and User Admin Only'
  110.                     ) {
  111.                         continue;
  112.                     }
  113.  
  114.                     if ($groupElementType == 'crmasmSelect') {
  115.                         $groupsOptions[$id] = $group['title'];
  116.                     }
  117.                     else {
  118.                         $form->_tagGroup[$fName][$id]['description'] = $group['description'];
  119.                         $elements[] = &$form->addElement('advcheckbox', $id, NULL, $group['title'], $attributes);
  120.                     }
  121.                 }
  122.  
  123.                 if ($groupElementType == 'crmasmSelect' && !empty($groupsOptions)) {
  124.                     $form->add('select', $fName, ts('%1', array(1 => $groupName)), $groupsOptions, FALSE,
  125.                     array('id' => $fName, 'multiple' => 'multiple', 'title' => ts('- select -'))
  126.                     );
  127.                     $form->assign('groupCount', count($groupsOptions));
  128.                 }
  129.  
  130.                 if ($groupElementType == 'checkbox' && !empty($elements)) {
  131.                     $form->addGroup($elements, $fName, $groupName, '&nbsp;<br />');
  132.                     $form->assign('groupCount', count($elements));
  133.                     if ($isRequired) {
  134.                         $form->addRule($fName, ts('%1 is a required field.', array(1 => $groupName)), 'required');
  135.                     }
  136.                 }
  137.                 $form->assign('groupElementType', $groupElementType);
  138.             }
  139.         }
  140.  
  141.         if ($type & self::TAG) {
  142.             CRM_Core_Resources::singleton()->addScriptUrl('http://10.0.0.2/crm/sites/all/modules/civicrm/packages/jquery/plugins/jstree/jquery.jstree.js?napuqq', 10, 'html-header');
  143.  
  144.             $fName = 'tag';
  145.             if ($fieldName) {
  146.                 $fName = $fieldName;
  147.             }
  148.             $form->_tagGroup[$fName] = 1;
  149.  
  150.             if (!empty($elements)) {
  151.                 $form->addGroup($elements, $fName, $tagName, '<br />');
  152.                 $form->assign('tagCount', count($elements));
  153.             }
  154.  
  155.             // get the list of all the categories
  156.             $allTag = CRM_Core_BAO_Tag::getTagsUsedFor();
  157.  
  158.             // need to append the array with the " checked " if contact is tagged with the tag
  159.             $tagChk = array();
  160.             foreach ($allTag as $tagID => $varValue) {
  161.                 $tagAttribute = array(
  162.               'onclick' => "return changeRowColor(\"rowidtag_$tagID\")",
  163.               'id' => "tag_{$tagID}",
  164.                 );
  165.  
  166.                 $tagChk[$tagID] = $form->createElement('checkbox', $tagID, '', '', $tagAttribute);
  167.             }
  168.  
  169.             if (!empty($tagChk)) {
  170.                 $form->addGroup($elements, $fName, $tagName, '<br />');
  171.                 $form->assign('tagCount', count($elements));
  172.             }
  173.             //$form->addGroup($tagChk, 'tagList', NULL, NULL, TRUE);
  174.  
  175.             $tags = new CRM_Core_BAO_Tag();
  176.             $tree = $tags->getTree('civicrm_contact', TRUE);
  177.             $form->assign('tree', $tree);
  178.             $form->assign('tag', $allTag);
  179.  
  180.             $form->assign('entityID', $contactId);
  181.             $form->assign('entityTable', 'civicrm_contact');
  182.  
  183.             if ($isRequired) {
  184.                 $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
  185.             }
  186.  
  187.             // build tag widget
  188.             $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
  189.             CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, TRUE, TRUE);
  190.  
  191.  
  192.             /*$this->addButtons(array(
  193.              array(
  194.              'type' => 'next',
  195.              'name' => ts('Update Tags'),
  196.              'isDefault' => TRUE,
  197.              ),
  198.              array(
  199.              'type' => 'cancel',
  200.              'name' => ts('Cancel'),
  201.              ),
  202.              ));*/
  203.  
  204.  
  205.  
  206.             /*$fName = 'tag';
  207.              if ($fieldName) {
  208.              $fName = $fieldName;
  209.              }
  210.              $form->_tagGroup[$fName] = 1;
  211.              $elements = array();
  212.              $tag = CRM_Core_BAO_Tag::getTags();
  213.  
  214.              foreach ($tag as $id => $name) {
  215.              $elements[] = $form->createElement('checkbox', $id, NULL, $name);
  216.              }
  217.              if (!empty($elements)) {
  218.              $form->addGroup($elements, $fName, $tagName, '<br />');
  219.              $form->assign('tagCount', count($elements));
  220.              }
  221.  
  222.              if ($isRequired) {
  223.              $form->addRule($fName, ts('%1 is a required field.', array(1 => $tagName)), 'required');
  224.              }
  225.  
  226.              // build tag widget
  227.              $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
  228.  
  229.              CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', $contactId, TRUE, TRUE);*/
  230.         }
  231.         $form->assign('tagGroup', $form->_tagGroup);
  232.     }
  233.  
  234.     /**
  235.      * set defaults for relevant form elements
  236.      *
  237.      * @param int    $id        the contact id
  238.      * @param array  $defaults  the defaults array to store the values in
  239.      * @param int    $type      what components are we interested in
  240.      * @param string $fieldName this is used in batch profile(i.e to build multiple blocks)
  241.      *
  242.      * @return void
  243.      * @access public
  244.      * @static
  245.      */
  246.     static function setDefaults($id, &$defaults, $type = self::ALL, $fieldName = NULL, $groupElementType = 'checkbox') {
  247.         $type = (int ) $type;
  248.         if ($type & self::GROUP) {
  249.             $fName = 'group';
  250.             if ($fieldName) {
  251.                 $fName = $fieldName;
  252.             }
  253.  
  254.             $contactGroup = CRM_Contact_BAO_GroupContact::getContactGroup($id, 'Added', NULL, FALSE, TRUE);
  255.             if ($contactGroup) {
  256.                 foreach ($contactGroup as $group) {
  257.                     if ($groupElementType == 'crmasmSelect') {
  258.                         $defaults[$fName][] = $group['group_id'];
  259.                     }
  260.                     else {
  261.                         $defaults[$fName . '[' . $group['group_id'] . ']'] = 1;
  262.                     }
  263.                 }
  264.             }
  265.         }
  266.  
  267.         if ($type & self::TAG) {
  268.             $fName = 'tag';
  269.             if ($fieldName) {
  270.                 $fName = $fieldName;
  271.             }
  272.  
  273.             $contactTag = CRM_Core_BAO_EntityTag::getTag($id);
  274.             if ($contactTag) {
  275.                 foreach ($contactTag as $tag) {
  276.                     $defaults[$fName . '[' . $tag . ']'] = 1;
  277.                 }
  278.             }
  279.         }
  280.     }
  281.  
  282.     /**
  283.      * This function sets the default values for the form. Note that in edit/view mode
  284.      * the default values are retrieved from the database
  285.      *
  286.      * @access public
  287.      *
  288.      * @return None
  289.      */
  290.     public static function setDefaultValues(&$form, &$defaults) {
  291.         $contactEditOptions = $form->get('contactEditOptions');
  292.  
  293.         if ($form->_action & CRM_Core_Action::ADD) {
  294.             if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
  295.                 // set group and tag defaults if any
  296.                 if ($form->_gid) {
  297.                     $defaults['group'][$form->_gid] = 1;
  298.                 }
  299.                 if ($form->_tid) {
  300.                     $defaults['tag'][$form->_tid] = 1;
  301.                 }
  302.             }
  303.         }
  304.         else {
  305.             if (array_key_exists('TagsAndGroups', $contactEditOptions)) {
  306.                 // set the group and tag ids
  307.                 $groupElementType = 'checkbox';
  308.                 if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
  309.                     $groupElementType = 'crmasmSelect';
  310.                 }
  311.                 self::setDefaults($form->_contactId, $defaults, self::ALL, NULL, $groupElementType);
  312.             }
  313.         }
  314.     }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement