app_defaults.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. if ($domains_processed == 1) {
  3. //populate new phone_label values, phone_type_* values
  4. $obj = new schema;
  5. $obj->db = $db;
  6. $obj->db_type = $db_type;
  7. $obj->schema();
  8. $field_exists = $obj->column_exists($db_name, 'v_contact_phones', 'phone_type'); //check if field exists
  9. if ($field_exists) {
  10. //add multi-lingual support
  11. $language = new text;
  12. $text = $language->get();
  13. // populate phone_type_* values
  14. $sql = "update v_contact_phones set phone_type_voice = '1' ";
  15. $sql .= "where phone_type = 'home' ";
  16. $sql .= "or phone_type = 'work' ";
  17. $sql .= "or phone_type = 'voice' ";
  18. $sql .= "or phone_type = 'voicemail' ";
  19. $sql .= "or phone_type = 'cell' ";
  20. $sql .= "or phone_type = 'pcs' ";
  21. $database = new database;
  22. $database->execute($sql);
  23. unset($sql);
  24. $sql = "update v_contact_phones set phone_type_fax = '1' where phone_type = 'fax'";
  25. $database = new database;
  26. $database->execute($sql);
  27. unset($sql);
  28. $sql = "update v_contact_phones set phone_type_video = '1' where phone_type = 'video'";
  29. $database = new database;
  30. $database->execute($sql);
  31. unset($sql);
  32. $sql = "update v_contact_phones set phone_type_text = '1' where phone_type = 'cell' or phone_type = 'pager'";
  33. $database = new database;
  34. $database->execute($sql);
  35. unset($sql);
  36. // migrate phone_type values to phone_label, correct case and make multilingual where appropriate
  37. $default_phone_types = array('home','work','pref','voice','fax','msg','cell','pager','modem','car','isdn','video','pcs');
  38. $default_phone_labels = array($text['option-home'],$text['option-work'],'Pref','Voice',$text['option-fax'],$text['option-voicemail'],$text['option-mobile'],$text['option-pager'],'Modem','Car','ISDN','Video','PCS');
  39. foreach ($default_phone_types as $index => $old) {
  40. $sql = "update v_contact_phones set phone_label = :phone_label where phone_type = :phone_type ";
  41. $parameters['phone_label'] = $default_phone_labels[$index]; //new
  42. $parameters['phone_type'] = $old;
  43. $database = new database;
  44. $database->execute($sql, $parameters);
  45. unset($sql, $parameters);
  46. }
  47. // empty phone_type field to prevent confusion in the future
  48. $sql = "update v_contact_phones set phone_type is null";
  49. $database = new database;
  50. $database->execute($sql);
  51. unset($sql);
  52. }
  53. unset($obj);
  54. //populate primary email from deprecated field in v_contact table
  55. $obj = new schema;
  56. $obj->db = $db;
  57. $obj->db_type = $db_type;
  58. $obj->schema();
  59. $field_exists = $obj->column_exists($db_name, 'v_contacts', 'contact_email'); //check if field exists
  60. if ($field_exists) {
  61. // get email records
  62. $sql = "select * from v_contacts where contact_email is not null and contact_email != '' ";
  63. $database = new database;
  64. $result = $database->select($sql);
  65. unset($sql);
  66. if (is_array($result) && @sizeof($result) != 0) {
  67. foreach($result as $row) {
  68. $array['contact_emails'][0]['contact_email_uuid'] = uuid();
  69. $array['contact_emails'][0]['domain_uuid'] = $row['domain_uuid'];
  70. $array['contact_emails'][0]['contact_uuid'] = $row['contact_uuid'];
  71. $array['contact_emails'][0]['email_primary'] = 1;
  72. $array['contact_emails'][0]['email_address'] = $row['contact_email'];
  73. $p = new permissions;
  74. $p->add('contact_email_add', 'temp');
  75. $database = new database;
  76. $database->app_name = 'contacts';
  77. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  78. $database->save($array, false);
  79. unset($array);
  80. $p->delete('contact_email_add', 'temp');
  81. //verify and remove value from old field
  82. $sql = "select email_address from v_contact_emails ";
  83. $sql .= "where domain_uuid = :domain_uuid ";
  84. $sql .= "and contact_uuid = :contact_uuid ";
  85. $sql .= "and email_address = :email_address ";
  86. $parameters['domain_uuid'] = $row['domain_uuid'];
  87. $parameters['contact_uuid'] = $row['contact_uuid'];
  88. $parameters['email_address'] = $row['contact_email'];
  89. $database = new database;
  90. $result_2 = $database->select($sql, $parameters, 'all');
  91. unset($sql, $parameters);
  92. if (is_array($result_2) && @sizeof($result_2) != 0) {
  93. $sql = "update v_contacts set contact_email = null ";
  94. $sql .= "where domain_uuid = :domain_uuid ";
  95. $sql .= "and contact_uuid = :contact_uuid ";
  96. $parameters['domain_uuid'] = $row['domain_uuid'];
  97. $parameters['contact_uuid'] = $row['contact_uuid'];
  98. $database = new database;
  99. $database->execute($sql, $parameters);
  100. unset($sql, $parameters);
  101. }
  102. unset($result_2);
  103. }
  104. }
  105. unset($result, $row);
  106. }
  107. unset($obj);
  108. //populate primary url from deprecated field in v_contact table
  109. $obj = new schema;
  110. $obj->db = $db;
  111. $obj->db_type = $db_type;
  112. $obj->schema();
  113. $field_exists = $obj->column_exists($db_name, 'v_contacts', 'contact_url'); //check if field exists
  114. if ($field_exists) {
  115. // get email records
  116. $sql = "select * from v_contacts where contact_url is not null and contact_url != ''";
  117. $database = new database;
  118. $result = $database->select($sql);
  119. unset($sql);
  120. if (is_array($result) && @sizeof($result) != 0) {
  121. foreach($result as $row) {
  122. $array['contact_urls'][0]['contact_url_uuid'] = uuid();
  123. $array['contact_urls'][0]['domain_uuid'] = $row['domain_uuid'];
  124. $array['contact_urls'][0]['contact_uuid'] = $row['contact_uuid'];
  125. $array['contact_urls'][0]['url_primary'] = 1;
  126. $array['contact_urls'][0]['url_address'] = $row['contact_url'];
  127. $p = new permissions;
  128. $p->add('contact_url_add', 'temp');
  129. $database = new database;
  130. $database->app_name = 'contacts';
  131. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  132. $database->save($array, false);
  133. unset($array);
  134. $p->delete('contact_url_add', 'temp');
  135. //verify and remove value from old field
  136. $sql = "select url_address from v_contact_urls ";
  137. $sql .= "where domain_uuid = :domain_uuid ";
  138. $sql .= "and contact_uuid = :contact_uuid ";
  139. $sql .= "and url_address = :url_address ";
  140. $parameters['domain_uuid'] = $row['domain_uuid'];
  141. $parameters['contact_uuid'] = $row['contact_uuid'];
  142. $parameters['url_address'] = $row['contact_url'];
  143. $database = new database;
  144. $result_2 = $database->select($sql, $parameters, 'all');
  145. unset($sql, $parameters);
  146. if (is_array($result_2) && @sizeof($result_2) != 0) {
  147. $sql = "update v_contacts set contact_url = '' ";
  148. $sql .= "where domain_uuid = :domain_uuid ";
  149. $sql .= "and contact_uuid = :contact_uuid ";
  150. $parameters['domain_uuid'] = $row['domain_uuid'];
  151. $parameters['contact_uuid'] = $row['contact_uuid'];
  152. $database = new database;
  153. $database->execute($sql, $parameters);
  154. unset($sql, $parameters);
  155. }
  156. unset($result_2);
  157. }
  158. }
  159. unset($result, $row);
  160. }
  161. unset($obj);
  162. //set [name]_primary fields to 0 where null
  163. $name_tables = array('phones','addresses','emails','urls');
  164. $name_fields = array('phone','address','email','url');
  165. foreach ($name_tables as $name_index => $name_table) {
  166. $sql = "update v_contact_".$name_table." set ".$name_fields[$name_index]."_primary = 0 ";
  167. $sql .= "where ".$name_fields[$name_index]."_primary is null ";
  168. $database = new database;
  169. $database->execute($sql);
  170. unset($sql);
  171. }
  172. unset($name_tables, $name_fields, $name_index, $name_table);
  173. //move the users from the contact groups table into the contact users table
  174. $sql = "select * from v_contact_groups ";
  175. $sql .= "where group_uuid in (select user_uuid from v_users) ";
  176. $database = new database;
  177. $result = $database->select($sql, null, 'all');
  178. if (is_array($result) && @sizeof($result) != 0) {
  179. foreach ($result as &$row) {
  180. $p = new permissions;
  181. $p->add('contact_user_add', 'temp');
  182. $p->add('contact_group_delete', 'temp');
  183. $array['contact_users'][0]['contact_user_uuid'] = uuid();
  184. $array['contact_users'][0]['domain_uuid'] = $row["domain_uuid"];
  185. $array['contact_users'][0]['contact_uuid'] = $row["contact_uuid"];
  186. $array['contact_users'][0]['user_uuid'] = $row["group_uuid"];
  187. $database = new database;
  188. $database->app_name = 'contacts';
  189. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  190. $database->save($array, false);
  191. unset($array);
  192. $array['contact_groups'][0]['contact_group_uuid'] = $row["contact_group_uuid"];
  193. $database = new database;
  194. $database->app_name = 'contacts';
  195. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  196. $database->delete($array);
  197. unset($array);
  198. $p->delete('contact_user_add', 'temp');
  199. $p->delete('contact_group_delete', 'temp');
  200. }
  201. }
  202. unset($sql, $result, $row);
  203. }
  204. ?>