[ Index ]

Source Code Reference for V1.00

title

Body

[close]

/modules/public/ -> contact_selector.php (source)

   1  <?php /* $Id: contact_selector.php 135 2008-04-04 13:49:13Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/modules/public/contact_selector.php $ */
   2  //if (!defined('W2P_BASE_DIR')){

   3  //  die('You should not access this file directly.');

   4  //}

   5  
   6  $show_all = w2PgetParam($_REQUEST, 'show_all', 0);
   7  $company_id = w2PgetParam($_REQUEST, 'company_id', 0);
   8  $contact_id = w2PgetParam($_POST, 'contact_id', 0);
   9  $call_back = w2PgetParam($_GET, 'call_back', null);
  10  $contacts_submited = w2PgetParam($_POST, 'contacts_submited', 0);
  11  $selected_contacts_id = w2PgetParam($_GET, 'selected_contacts_id', '');
  12  if (w2PgetParam($_POST, 'selected_contacts_id')) {
  13      $selected_contacts_id = w2PgetParam($_POST, 'selected_contacts_id');
  14  }
  15  ?>
  16  <script language="javascript">
  17  // ECMA Script section Carsten Menke <menke@smp-synergie.de>

  18  function setContactIDs(method, querystring) {
  19      var URL = 'index.php?m=public&a=contact_selector';
  20      var field = document.getElementsByName('contact_id[]');
  21      var selected_contacts_id = document.frmContactSelect.selected_contacts_id;
  22      var tmp = new Array();
  23      
  24      if (method == 'GET' && querystring){
  25          URL += '&' + querystring;
  26      }
  27      
  28      var count = 0;
  29      for (i = 0, i_cmp = field.length; i < i_cmp; i++) {
  30          if (field[i].checked) {
  31              tmp[count++] = field[i].value;
  32          }
  33      }
  34      selected_contacts_id.value = tmp.join(',');
  35      
  36      if (method == 'GET') {
  37          URL +=  '&selected_contacts_id=' + selected_contacts_id.value;
  38          return URL;
  39      } else {
  40          return selected_contacts_id;
  41      }
  42  }
  43  </script>
  44  <?php
  45  
  46  function remove_invalid($arr) {
  47      $result = array();
  48      foreach ($arr as $val) {
  49          if (!empty($val) && trim($val) !== '') {
  50              $result[] = $val;
  51          }
  52      }
  53      return $result;
  54  }
  55  
  56  if ($contacts_submited == 1) {
  57      $call_back_string = !is_null($call_back) ? "window.opener.$call_back('$selected_contacts_id');" : '';
  58  ?>
  59  <script language="javascript">
  60      <?php echo $call_back_string ?>
  61      self.close();
  62  </script>
  63  <?php
  64  }
  65  
  66  // Remove any empty elements

  67  $contacts_id = remove_invalid(explode(',', $selected_contacts_id));
  68  $selected_contacts_id = implode(',', $contacts_id);
  69  
  70  require_once ($AppUI->getModuleClass('companies'));
  71  $oCpy = new CCompany();
  72  $aCpies = $oCpy->getAllowedRecords($AppUI->user_id, 'company_id, company_name', 'company_name');
  73  $aCpies_esc = array();
  74  foreach ($aCpies as $key => $company) {
  75      $aCpies_esc[$key] = db_escape($company);
  76  }
  77  
  78  require_once ($AppUI->getModuleClass('departments'));
  79  $oDpt = new CDepartment();
  80  $aDpts = $oDpt->getAllowedRecords($AppUI->user_id, 'dept_id, dept_name', 'dept_name');
  81  
  82  $q = new DBQuery;
  83  
  84  if (strlen($selected_contacts_id) > 0 && !$show_all && !$company_id) {
  85      $q->addTable('contacts');
  86      $q->addQuery('DISTINCT contact_company');
  87      $q->addWhere('contact_id IN (' . $selected_contacts_id . ')');
  88      $where = implode(',', $q->loadColumn());
  89      $q->clear();
  90      if (substr($where, 0, 1) == ',') {
  91          $where = '0' . $where;
  92      }
  93      $where = (($where) ? ('contact_company IN(' . $where . ')') : '');
  94      if (count($aDpts)) {
  95          $where_dept = '(contact_department = 0 OR (contact_department IN (' . implode(',', array_keys($aDpts)) . ')))';
  96      } else {
  97          $where_dept = '(contact_department = 0)';
  98      }
  99  } elseif (!$company_id) {
 100      //  Contacts from all allowed companies

 101      $where = '(contact_company = \'\' OR contact_company IS NULL OR contact_company = 0 OR (contact_company IN (\'' . implode('","', array_values($aCpies_esc)) . '\'))' . ' OR ( contact_company IN (\'' . implode('","', array_keys($aCpies_esc)) . '\')))';
 102      if (count($aDpts)) {
 103          $where_dept = '(contact_department = 0 OR (contact_department IN (' . implode(',', array_keys($aDpts)) . ')))';
 104      } else {
 105          $where_dept = '(contact_department = 0)';
 106      }
 107      $company_name = $AppUI->_('Allowed Companies');
 108  } else {
 109      // Contacts for this company only

 110      $q->addTable('companies', 'c');
 111      $q->addQuery('c.company_name');
 112      $q->addWhere('company_id = ' . (int)$company_id);
 113      $company_name = $q->loadResult();
 114      $q->clear();
 115      $company_name_sql = db_escape($company_name);
 116      $where = '(contact_company = \'' . $company_name_sql . '\' or contact_company = ' . (int)$company_id . ')';
 117      if (count($aDpts)) {
 118          $where_dept = '(contact_department = 0 OR (contact_department IN (' . implode(',', array_keys($aDpts)) . ')))';
 119      } else {
 120          $where_dept = '(contact_department = 0)';
 121      }
 122  }
 123  
 124  // This should now work on company ID, but we need to be able to handle both

 125  $q->addTable('contacts', 'a');
 126  $q->leftJoin('companies', 'b', 'company_id = contact_company');
 127  $q->leftJoin('departments', 'c', 'dept_id = contact_department');
 128  $q->addQuery('contact_id, contact_first_name, contact_last_name, contact_company, contact_department');
 129  $q->addQuery('company_name');
 130  $q->addQuery('dept_name');
 131  if ($where) { // Don't assume where is set. Change needed to fix Mantis Bug 0002056
 132      $q->addWhere($where);
 133  }
 134  if ($where_dept) { // Don't assume where is set. Change needed to fix Mantis Bug 0002056
 135      $q->addWhere($where_dept);
 136  }
 137  $q->addWhere('(contact_owner = ' . (int)$AppUI->user_id . ' OR contact_private = 0)');
 138  $q->addOrder('company_name, contact_company, dept_name, contact_department, contact_last_name'); // May need to review this.

 139  $contacts = $q->loadHashList('contact_id');
 140  ?>
 141  
 142  <form action="index.php?m=public&a=contact_selector&dialog=1&<?php if (!is_null($call_back))
 143      echo 'call_back=' . $call_back . '&'; ?>company_id=<?php echo $company_id ?>" method='post' name='frmContactSelect'>
 144  
 145  <?php
 146  $actual_department = '';
 147  $actual_company = '';
 148  $companies_names = array(0 => $AppUI->_('Select a company')) + $aCpies;
 149  echo arraySelect($companies_names, 'company_id', 'onchange="document.frmContactSelect.contacts_submited.value=0; ' . 'setContactIDs(); document.frmContactSelect.submit();"', 0);
 150  ?>
 151  <br /><br />
 152  <?php
 153  if (function_exists('styleRenderBoxTop')) {
 154      echo styleRenderBoxTop();
 155  }
 156  ?>
 157  <table width="100%" class="std">
 158  <tr><td>
 159  <h4><a href="javascript: void(0);" onclick="window.location.href=setContactIDs('GET','dialog=1&<?php if (!is_null($call_back))
 160      echo 'call_back=' . $call_back . '&'; ?>show_all=1');"><?php echo $AppUI->_('View all allowed companies'); ?></a></h4>
 161  <hr />
 162  <h2><?php echo $AppUI->_('Contacts for'); ?> <?php echo $company_name ?></h2>
 163  <?php
 164  foreach ($contacts as $contact_id => $contact_data) {
 165      if (!$contact_data['company_name']) {
 166          $contact_company = $contact_data['contact_company'];
 167      } else {
 168          $contact_company = $contact_data['company_name'];
 169      }
 170      if ($contact_company && $contact_company != $actual_company) {
 171          echo '<h4>' . $contact_company . '</h4>';
 172          $actual_company = $contact_company;
 173      }
 174      $contact_department = $contact_data['dept_name'] ? $contact_data['dept_name'] : $contact_data['contact_department'];
 175      if ($contact_department && $contact_department != $actual_department) {
 176          echo '<h5>' . $contact_department . '</h5>';
 177          $actual_department = $contact_department;
 178      }
 179      $checked = in_array($contact_id, $contacts_id) ? 'checked="checked"' : '';
 180      echo '<input type="checkbox" name="contact_id[]" id="contact_' . $contact_id . '" value="' . $contact_id . '" ' . $checked . ' />';
 181      echo '<label for="contact_' . $contact_id . '">' . $contact_data['contact_first_name'] . ' ' . $contact_data['contact_last_name'] . '</label>';
 182      echo '<br />';
 183  }
 184  ?>
 185  <hr />
 186  <input name="contacts_submited" type="hidden" value="1" />
 187  <input name="selected_contacts_id" type="hidden" value="<?php echo $selected_contacts_id; ?>" />
 188  <input type="submit" value="<?php echo $AppUI->_('Continue'); ?>" onclick="setContactIDs()" class="button" />
 189  </td></tr>
 190  </table>
 191  </form>


Generated: Fri Jan 9 03:00:02 2009 Cross-referenced by PHPXref 0.7