[ Index ]

Source Code Reference for V1.00

title

Body

[close]

/includes/ -> main_functions.php (source)

   1  <?php /* $Id: main_functions.php 226 2008-09-05 09:47:47Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/includes/main_functions.php $ */
   2  ##
   3  ## Global General Purpose Functions
   4  ##
   5  if (!defined('W2P_BASE_DIR')) {
   6      die('You should not access this file directly.');
   7  }
   8  
   9  define('SECONDS_PER_DAY', 60 * 60 * 24);
  10  
  11  ##
  12  ## Returns the best color based on a background color (x is cross-over)
  13  ##
  14  function bestColor($bg, $lt = '#ffffff', $dk = '#000000') {
  15      // cross-over color = x
  16      $x = 128;
  17      $r = hexdec(substr($bg, 0, 2));
  18      $g = hexdec(substr($bg, 2, 2));
  19      $b = hexdec(substr($bg, 4, 2));
  20  
  21      if ($r < $x && $g < $x || $r < $x && $b < $x || $b < $x && $g < $x) {
  22          return $lt;
  23      } else {
  24          return $dk;
  25      }
  26  }
  27  
  28  ##
  29  ## returns a select box based on an key,value array where selected is based on key
  30  ##
  31  function arraySelect(&$arr, $select_name, $select_attribs, $selected, $translate = false) {
  32      global $AppUI;
  33      if (!is_array($arr)) {
  34          dprint(__file__, __line__, 0, 'arraySelect called with no array');
  35          return '';
  36      }
  37      reset($arr);
  38      $s = '<select id="' . $select_name . '" name="' . $select_name . '" ' . $select_attribs . '>';
  39      $did_selected = 0;
  40      foreach ($arr as $k => $v) {
  41          if ($translate) {
  42              $v = $AppUI->_($v);
  43              // This is supplied to allow some Hungarian characters to
  44              // be translated correctly. There are probably others.
  45              // As such a more general approach probably based upon an
  46              // array lookup for replacements would be a better approach. AJD.
  47              $v = str_replace('&#369;', '�', $v);
  48              $v = str_replace('&#337;', '�', $v);
  49          }
  50          $s .= '<option value="' . $k . '"' . ((($k == $selected && strcmp($k, $selected) == 0) && !$did_selected) ? ' selected="selected"' : '') . '>' . $v . '</option>';
  51          if (($k == $selected && strcmp($k, $selected) == 0)) {
  52              $did_selected = 1;
  53          }
  54      }
  55      $s .= '</select>';
  56      return $s;
  57  }
  58  
  59  ##
  60  ## returns a select box based on an key,value array where selected is based on key
  61  ##
  62  function arraySelectTree(&$arr, $select_name, $select_attribs, $selected, $translate = false) {
  63      global $AppUI;
  64      reset($arr);
  65  
  66      $children = array();
  67      // first pass - collect children
  68      foreach ($arr as $k => $v) {
  69          $id = $v[0];
  70          $pt = $v[2];
  71          $list = $children[$pt] ? $children[$pt] : array();
  72          array_push($list, $v);
  73          $children[$pt] = $list;
  74      }
  75      $list = tree_recurse($arr[0][2], '', array(), $children);
  76      return arraySelect($list, $select_name, $select_attribs, $selected, $translate);
  77  }
  78  
  79  function tree_recurse($id, $indent, $list, $children) {
  80      if ($children[$id]) {
  81          foreach ($children[$id] as $v) {
  82              $id = $v[0];
  83              $txt = $v[1];
  84              $pt = $v[2];
  85              $list[$id] = $indent . ' ' . $txt;
  86              $list = tree_recurse($id, $indent . '--', $list, $children);
  87          }
  88      }
  89      return $list;
  90  }
  91  
  92  /**
  93   **    Provide Projects Selectbox sorted by Companies
  94   **    @author gregorerhardt with special thanks to original author aramis
  95   **    @param     int         userID
  96   **    @param     string     HTML select box name identifier
  97   **    @param    string    HTML attributes
  98   **    @param    int            Proejct ID for preselection
  99   **    @param     int            Project ID which will be excluded from the list 
 100   **                                    (e.g. in the tasks import list exclude the project to import into)
 101   **    @return    string     HTML selectbox
 102  
 103   */
 104  
 105  function projectSelectWithOptGroup($user_id, $select_name, $select_attribs, $selected, $excludeProjWithId = null) {
 106      global $AppUI;
 107      $q = new DBQuery();
 108      $q->addTable('projects', 'pr');
 109      $q->addQuery('pr.project_id, co.company_name, project_name');
 110      if (!empty($excludeProjWithId)) {
 111          $q->addWhere('pr.project_id <> ' . $excludeProjWithId);
 112      }
 113      $proj = new CProject();
 114      $proj->setAllowedSQL($user_id, $q, null, 'pr');
 115      $q->addOrder('co.company_name, project_name');
 116      $projects = $q->loadList();
 117      $s = '<select name="' . $select_name . '" ' . $select_attribs . '>';
 118      $s .= '<option value="0" ' . ($selected == 0 ? 'selected="selected"' : '') . ' >' . $AppUI->_('None') . '</option>';
 119      $current_company = '';
 120      foreach ($projects as $p) {
 121          if ($p['company_name'] != $current_company) {
 122              $current_company = $p['company_name'];
 123              $s .= '<optgroup label="' . $current_company . '" >' . $current_company . '</optgroup>';
 124          }
 125          $s .= '<option value="' . $p['project_id'] . '" ' . ($selected == $p['project_id'] ? 'selected="selected"' : '') . '>&nbsp;&nbsp;&nbsp;' . $p['project_name'] . '</option>';
 126      }
 127      $s .= '</select>';
 128      return $s;
 129  }
 130  
 131  ##
 132  ## Merges arrays maintaining/overwriting shared numeric indicees
 133  ##
 134  function arrayMerge($a1, $a2) {
 135      foreach ($a2 as $k => $v) {
 136          $a1[$k] = $v;
 137      }
 138      return $a1;
 139  }
 140  
 141  ##
 142  ## breadCrumbs - show a separated list of crumbs
 143  ## array is in the form url => title
 144  ##
 145  function breadCrumbs(&$arr) {
 146      global $AppUI;
 147      $crumbs = array();
 148      foreach ($arr as $k => $v) {
 149          $crumbs[] = '<a class="button" href="' . $k . '"><span>' . $AppUI->_($v) . '</span></a>';
 150      }
 151      return implode('</td><td align="left" nowrap="nowrap">', $crumbs);
 152  }
 153  ##
 154  ## generate link for context help -- old version
 155  ##
 156  function contextHelp($title, $link = '') {
 157      return w2PcontextHelp($title, $link);
 158  }
 159  
 160  function w2PcontextHelp($title, $link = '') {
 161      global $AppUI;
 162      return '<a href="#' . $link . '" onclick="javascript:window.open(\'?m=help&amp;dialog=1&amp;hid=' . $link . '\', \'contexthelp\', \'width=400, height=400, left=50, top=50, scrollbars=yes, resizable=yes\')">' . $AppUI->_($title) . '</a>';
 163  }
 164  
 165  /**
 166   * Retrieves a configuration setting.
 167   * @param $key string The name of a configuration setting
 168   * @param $default string The default value to return if the key not found.
 169   * @return The value of the setting, or the default value if not found.
 170   */
 171  function w2PgetConfig($key, $default = null) {
 172      global $w2Pconfig;
 173      if (array_key_exists($key, $w2Pconfig)) {
 174          return $w2Pconfig[$key];
 175      } else {
 176          return $default;
 177      }
 178  }
 179  
 180  function w2PgetUsername($user) {
 181      $q = new DBQuery;
 182      $q->addTable('users');
 183      $q->addQuery('contact_first_name, contact_last_name');
 184      $q->addJoin('contacts', 'con', 'contact_id = user_contact', 'inner');
 185      $q->addWhere('user_username like \'' . $user . '\' OR user_id = ' . (int)$user);
 186      $r = $q->loadList();
 187      return $r[0]['contact_first_name'] . ' ' . $r[0]['contact_last_name'];
 188  }
 189  
 190  function w2PgetUsernameFromID($user) {
 191      $q = new DBQuery;
 192      $q->addTable('users');
 193      $q->addQuery('contact_first_name, contact_last_name');
 194      $q->addJoin('contacts', 'con', 'contact_id = user_contact', 'inner');
 195      $q->addWhere('user_id = ' . (int)$user);
 196      $r = $q->loadList();
 197      return $r[0]['contact_first_name'] . ' ' . $r[0]['contact_last_name'];
 198  }
 199  
 200  function w2PgetUsers($module = '') {
 201      global $AppUI;
 202      $q = new DBQuery;
 203      $q->addTable('users');
 204      $q->addQuery('user_id, concat_ws(\' \', contact_first_name, contact_last_name) as name');
 205      $q->addJoin('contacts', 'con', 'contact_id = user_contact', 'inner');
 206      $q->addOrder('contact_first_name,contact_last_name');
 207  
 208      // get CCompany() to filter by company
 209      require_once ($AppUI->getModuleClass('companies'));
 210      $obj = new CCompany();
 211      $companies = $obj->getAllowedSQL($AppUI->user_id, 'company_id');
 212      $q->addJoin('companies', 'com', 'company_id = contact_company');
 213      if ($companies) {
 214          $q->addWhere('(' . implode(' OR ', $companies) . ' OR contact_company=\'\' OR contact_company IS NULL OR contact_company = 0)');
 215      }
 216      require_once ($AppUI->getModuleClass('departments'));
 217      $dpt = new CDepartment();
 218      $depts = $dpt->getAllowedSQL($AppUI->user_id, 'dept_id');
 219      $q->addJoin('departments', 'dep', 'dept_id = contact_department');
 220      if ($depts) {
 221          $q->addWhere('(' . implode(' OR ', $depts) . ' OR contact_department=0)');
 222      }
 223      //print_r($q->prepare());
 224  
 225      return arrayMerge(array(0 => $AppUI->_('All Users')), $q->loadHashList());
 226  }
 227  
 228  function w2PgetUsersList($stub = null, $where = null, $orderby = 'contact_first_name, contact_last_name') {
 229      global $AppUI;
 230      $q = new DBQuery;
 231      $q->addTable('users');
 232      $q->addQuery('DISTINCT(user_id), user_username, contact_last_name, contact_first_name,
 233           contact_email, company_name, contact_company, dept_id, dept_name, CONCAT(contact_first_name,\' \',contact_last_name) contact_name, user_type');
 234      $q->addJoin('contacts', 'con', 'contact_id = user_contact', 'inner');
 235      if ($stub) {
 236          $q->addWhere('(UPPER(user_username) LIKE \'' . $stub . '%\' or UPPER(contact_first_name) LIKE \'' . $stub . '%\' OR UPPER(contact_last_name) LIKE \'' . $stub . '%\')');
 237      } elseif ($where) {
 238          $where = $q->quote('%' . $where . '%');
 239          $q->addWhere('(UPPER(user_username) LIKE ' . $where . ' OR UPPER(contact_first_name) LIKE ' . $where . ' OR UPPER(contact_last_name) LIKE ' . $where . ')');
 240      }
 241  
 242      $q->addGroup('user_id');
 243      $q->addOrder($orderby);
 244  
 245      // get CCompany() to filter by company
 246      require_once ($AppUI->getModuleClass('companies'));
 247      $obj = new CCompany();
 248      $companies = $obj->getAllowedSQL($AppUI->user_id, 'company_id');
 249      $q->addJoin('companies', 'com', 'company_id = contact_company');
 250      if ($companies) {
 251          $q->addWhere('(' . implode(' OR ', $companies) . ' OR contact_company=\'\' OR contact_company IS NULL OR contact_company = 0)');
 252      }
 253      require_once ($AppUI->getModuleClass('departments'));
 254      $dpt = new CDepartment();
 255      $depts = $dpt->getAllowedSQL($AppUI->user_id, 'dept_id');
 256      $q->addJoin('departments', 'dep', 'dept_id = contact_department');
 257      if ($depts) {
 258          $q->addWhere('(' . implode(' OR ', $depts) . ' OR contact_department=0)');
 259      }
 260      //print_r($q->prepare());
 261  
 262      return $q->loadList();
 263  }
 264  
 265  function w2PgetUsersHashList($stub = null, $where = null, $orderby = 'contact_first_name, contact_last_name') {
 266      global $AppUI;
 267      $q = new DBQuery;
 268      $q->addTable('users');
 269      $q->addQuery('DISTINCT(user_id), user_username, contact_last_name, contact_first_name,
 270           contact_email, company_name, contact_company, dept_id, dept_name, CONCAT(contact_first_name,\' \',contact_last_name) contact_name, user_type');
 271      $q->addJoin('contacts', 'con', 'contact_id = user_contact', 'inner');
 272      if ($stub) {
 273          $q->addWhere('(UPPER(user_username) LIKE \'' . $stub . '%\' or UPPER(contact_first_name) LIKE \'' . $stub . '%\' OR UPPER(contact_last_name) LIKE \'' . $stub . '%\')');
 274      } elseif ($where) {
 275          $where = $q->quote('%' . $where . '%');
 276          $q->addWhere('(UPPER(user_username) LIKE ' . $where . ' OR UPPER(contact_first_name) LIKE ' . $where . ' OR UPPER(contact_last_name) LIKE ' . $where . ')');
 277      }
 278  
 279      $q->addGroup('user_id');
 280      $q->addOrder($orderby);
 281  
 282      // get CCompany() to filter by company
 283      require_once ($AppUI->getModuleClass('companies'));
 284      $obj = new CCompany();
 285      $companies = $obj->getAllowedSQL($AppUI->user_id, 'company_id');
 286      $q->addJoin('companies', 'com', 'company_id = contact_company');
 287      if ($companies) {
 288          $q->addWhere('(' . implode(' OR ', $companies) . ' OR contact_company=\'\' OR contact_company IS NULL OR contact_company = 0)');
 289      }
 290      require_once ($AppUI->getModuleClass('departments'));
 291      $dpt = new CDepartment();
 292      $depts = $dpt->getAllowedSQL($AppUI->user_id, 'dept_id');
 293      $q->addJoin('departments', 'dep', 'dept_id = contact_department');
 294      if ($depts) {
 295          $q->addWhere('(' . implode(' OR ', $depts) . ' OR contact_department=0)');
 296      }
 297      //print_r($q->prepare());
 298  
 299      return $q->loadHashList('user_id');
 300  }
 301  
 302  ##
 303  ## displays the configuration array of a module for informational purposes
 304  ##
 305  function w2PshowModuleConfig($config) {
 306      global $AppUI;
 307      $s = '<table cellspacing="2" cellpadding="2" border="0" class="std" width="50%">';
 308      $s .= '<tr><th colspan="2">' . $AppUI->_('Module Configuration') . '</th></tr>';
 309      foreach ($config as $k => $v) {
 310          $s .= '<tr><td width="50%">' . $AppUI->_($k) . '</td><td width="50%" class="hilite">' . $AppUI->_($v) . '</td></tr>';
 311      }
 312      $s .= '</table>';
 313      return ($s);
 314  }
 315  
 316  /**
 317   *    Function to recussively find an image in a number of places
 318   *    @param string The name of the image
 319   *    @param string Optional name of the current module
 320   */
 321  function w2PfindImage($name, $module = null) {
 322      // uistyle must be declared globally
 323      global $uistyle;
 324      //print_r($name.' '.$module.' '.w2PgetConfig('host_style'));
 325      if ($module && file_exists(W2P_BASE_DIR . '/modules/' . $module . '/images/' . $name)) {
 326          return './modules/' . $module . '/images/' . $name;
 327      } elseif ($module && file_exists(W2P_BASE_DIR . '/style/' . $uistyle . '/images/modules/' . $module . '/' . $name)) {
 328          return './style/' . $uistyle . '/images/modules/' . $module . '/' . $name;
 329      } elseif (file_exists(W2P_BASE_DIR . '/style/' . $uistyle . '/images/icons/' . $name)) {
 330          return './style/' . $uistyle . '/images/icons/' . $name;
 331      } elseif (file_exists(W2P_BASE_DIR . '/style/' . $uistyle . '/images/obj/' . $name)) {
 332          return './style/' . $uistyle . '/images/obj/' . $name;
 333      } elseif (file_exists(W2P_BASE_DIR . '/style/' . $uistyle . '/images/' . $name)) {
 334          return './style/' . $uistyle . '/images/' . $name;
 335      } elseif ($module && file_exists(W2P_BASE_DIR . '/style/' . w2PgetConfig('host_style') . '/images/modules/' . $module . '/' . $name)) {
 336          return './style/' . w2PgetConfig('host_style') . '/images/modules/' . $module . '/' . $name;
 337      } elseif (file_exists(W2P_BASE_DIR . '/style/' . w2PgetConfig('host_style') . '/images/icons/' . $name)) {
 338          return './style/' . w2PgetConfig('host_style') . '/images/icons/' . $name;
 339      } elseif (file_exists(W2P_BASE_DIR . '/style/' . w2PgetConfig('host_style') . '/images/obj/' . $name)) {
 340          return './style/' . w2PgetConfig('host_style') . '/images/obj/' . $name;
 341      } else {
 342          return './style/' . w2PgetConfig('host_style') . '/images/' . $name;
 343      }
 344  }
 345  
 346  /**
 347   *    Workaround removed due to problems in Opera and other issues
 348   *    with IE6.
 349   *    Workaround to display png images with alpha-transparency in IE6.0
 350   *    @param string The name of the image
 351   *    @param string The image width
 352   *    @param string The image height
 353   *    @param string The alt text for the image
 354   */
 355  function w2PshowImage($src, $wid = '', $hgt = '', $alt = '', $title = '', $module = null) {
 356      global $AppUI, $m;
 357      /*
 358      if (strpos( $src, '.png' ) > 0 && strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 6.0' ) !== false) {
 359      return "<div style=\"height:{$hgt}px; width:{$wid}px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='$src', sizingMethod='scale');\" ></div>";
 360      } else {
 361      */
 362      if ($src == '') {
 363          return '';
 364      } elseif ($module) {
 365          $src = w2PfindImage($src, $module);
 366      } else {
 367          $src = w2PfindImage($src, $m);
 368      }
 369  
 370      if (!$alt && !$title) {
 371          $result = '';
 372      } elseif ($alt && $title) {
 373          $result = w2PtoolTip($alt, $title);
 374      } elseif ($alt && !$title) {
 375          $result = w2PtoolTip($m, $alt);
 376      } elseif (!$alt && $title) {
 377          $result = w2PtoolTip($m, $title);
 378      }
 379      $result .= '<img src="' . $src . '" alt="" ';
 380      if ($wid) {
 381          $result .= ' width="' . $wid . '"';
 382      }
 383      if ($hgt) {
 384          $result .= ' height="' . $hgt . '"';
 385      }
 386      $result .= ' border="0" />';
 387      if (!$alt && !$title) {
 388          //do nothing
 389      } elseif ($alt && $title) {
 390          $result .= w2PendTip();
 391      } elsei