[ Index ]
 

Code source de dotProject 2.1 RC1

Accédez au Source d'autres logiciels libres | Soutenez Angelica Josefina !

title

Body

[fermer]

/db/ -> upgrade_contacts_company.php (source)

   1  <?php
   2  
   3  /**
   4  * This script iterates all contacts and verify if the contact_company 
   5  * field has a text value; if it does, it searches of the company in the 
   6  * companies table, if it finds it then the contact is related to it by its id. 
   7  * If it doesn't find it, the it creates the company (only the name) and then it 
   8  * relates it to the contact using the new company's id.
   9  */
  10  
  11  if (! defined('DP_BASE_DIR'))
  12      die('You must not use this file directly, please direct your browser to install/index.php instead');
  13  
  14  dPmsg("Fetching companies list");
  15  foreach(db_loadList("SELECT * FROM contacts") as $contact) {
  16      $contact_company = $contact["contact_company"];
  17      if (is_numeric($contact_company)){
  18          if(!checkCompanyId($contact_company)){
  19              dPmsg("Error found in contact_company in the contact ".getContactGeneralInformation($contact));
  20          }
  21      } else if ($contact_company != "") {
  22          $company_id = fetchCompanyId($contact_company);
  23          // If we find company_id
  24          
  25          if (!$company_id) {
  26              // We need to create the new company
  27              $company_id = insertCompany($contact_company);
  28          }
  29          
  30          if($company_id){
  31              updateContactCompany($contact, $company_id);
  32              dPmsg("Contact's company updated - ".getContactGeneralInformation($contact)." - ($company_id) $contact_company");
  33          } else {
  34              dPmsg("Unable to update contact's company - ".getContactGeneralInformation($contact));
  35          }
  36      }
  37  }
  38  
  39  
  40  function updateContactCompany($contact_array, $company_id) {
  41      $sql = "UPDATE contacts SET contact_company = $company_id
  42              WHERE contact_id = ".$contact_array["contact_id"];
  43      db_exec($sql);
  44  }
  45  
  46  function getContactGeneralInformation($contact_array) {
  47      $contact_info  = "(".$contact_array["contact_id"].") ";
  48      $contact_info .= $contact_array["contact_first_name"]." ".$contact_array["contact_last_name"];
  49      return $contact_info;
  50  }
  51  
  52  function fetchCompanyId($company_name) {
  53      return db_loadResult("SELECT company_id FROM companies WHERE company_name = '$company_name'");
  54  }
  55  
  56  function checkCompanyId($company_id) {
  57      return db_loadResult("SELECT count(*) FROM companies WHERE company_id = '$company_id'");
  58  }
  59  
  60  function insertCompany($company_name) {
  61      $sql = "INSERT INTO companies (company_name) VALUES ('$company_name')";
  62      db_exec($sql);
  63      return db_insert_id();
  64  }


Généré le : Sun Feb 18 19:46:52 2007 par Balluche grâce à PHPXref 0.7