PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/include/SugarFields/Fields/Datetime/SugarFieldDatetime.php

https://gitlab.com/tjaafar/SuiteCRM
PHP | 215 lines | 135 code | 27 blank | 53 comment | 33 complexity | 18c11f23703d5d40e4df20433182556c MD5 | raw file
  1. <?php
  2. /*********************************************************************************
  3. * SugarCRM Community Edition is a customer relationship management program developed by
  4. * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
  5. * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
  6. * Copyright (C) 2011 - 2014 Salesagility Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU Affero General Public License version 3 as published by the
  10. * Free Software Foundation with the addition of the following permission added
  11. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  12. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  13. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License along with
  21. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  22. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  23. * 02110-1301 USA.
  24. *
  25. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  26. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  27. *
  28. * The interactive user interfaces in modified source and object code versions
  29. * of this program must display Appropriate Legal Notices, as required under
  30. * Section 5 of the GNU Affero General Public License version 3.
  31. *
  32. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  33. * these Appropriate Legal Notices must retain the display of the "Powered by
  34. * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
  35. * reasonably feasible for technical reasons, the Appropriate Legal Notices must
  36. * display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
  37. ********************************************************************************/
  38. require_once('include/SugarFields/Fields/Base/SugarFieldBase.php');
  39. class SugarFieldDatetime extends SugarFieldBase {
  40. function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
  41. // Create Smarty variables for the Calendar picker widget
  42. if(!isset($displayParams['showMinutesDropdown'])) {
  43. $displayParams['showMinutesDropdown'] = false;
  44. }
  45. if(!isset($displayParams['showHoursDropdown'])) {
  46. $displayParams['showHoursDropdown'] = false;
  47. }
  48. if(!isset($displayParams['showNoneCheckbox'])) {
  49. $displayParams['showNoneCheckbox'] = false;
  50. }
  51. if(!isset($displayParams['showFormats'])) {
  52. $displayParams['showFormats'] = false;
  53. }
  54. if(!isset($displayParams['hiddeCalendar'])) {
  55. $displayParams['hiddeCalendar'] = false;
  56. }
  57. // jpereira@dri - #Bug49552 - Datetime field unable to follow parent class methods
  58. //jchi , bug #24557 , 10/31/2008
  59. if(isset($vardef['name']) && ($vardef['name'] == 'date_entered' || $vardef['name'] == 'date_modified')){
  60. return $this->getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
  61. }
  62. //end
  63. return parent::getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
  64. // ~ jpereira@dri - #Bug49552 - Datetime field unable to follow parent class methods
  65. }
  66. function getImportViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex)
  67. {
  68. $displayParams['showMinutesDropdown'] = false;
  69. $displayParams['showHoursDropdown'] = false;
  70. $displayParams['showNoneCheckbox'] = false;
  71. $displayParams['showFormats'] = true;
  72. $displayParams['hiddeCalendar'] = false;
  73. $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
  74. return $this->fetch($this->findTemplate('EditView'));
  75. }
  76. function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
  77. if($this->isRangeSearchView($vardef)) {
  78. $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
  79. $id = isset($displayParams['idName']) ? $displayParams['idName'] : $vardef['name'];
  80. $this->ss->assign('original_id', "{$id}");
  81. $this->ss->assign('id_range', "range_{$id}");
  82. $this->ss->assign('id_range_start', "start_range_{$id}");
  83. $this->ss->assign('id_range_end', "end_range_{$id}");
  84. $this->ss->assign('id_range_choice', "{$id}_range_choice");
  85. if(file_exists('custom/include/SugarFields/Fields/Datetimecombo/RangeSearchForm.tpl'))
  86. {
  87. return $this->fetch('custom/include/SugarFields/Fields/Datetimecombo/RangeSearchForm.tpl');
  88. }
  89. return $this->fetch('include/SugarFields/Fields/Datetimecombo/RangeSearchForm.tpl');
  90. }
  91. return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'EditView');
  92. }
  93. public function getEmailTemplateValue($inputField, $vardef, $context = null){
  94. global $timedate;
  95. // This does not return a smarty section, instead it returns a direct value
  96. if(isset($context['notify_user'])) {
  97. $user = $context['notify_user'];
  98. } else {
  99. $user = $GLOBALS['current_user'];
  100. }
  101. if($vardef['type'] == 'date') {
  102. if(!$timedate->check_matching_format($inputField, TimeDate::DB_DATE_FORMAT)) {
  103. return $inputField;
  104. }
  105. // convert without TZ
  106. return $timedate->to_display($inputField, $timedate->get_db_date_format(), $timedate->get_date_format($user));
  107. } else {
  108. if(!$timedate->check_matching_format($inputField, TimeDate::DB_DATETIME_FORMAT)) {
  109. return $inputField;
  110. }
  111. return $timedate->to_display_date_time($inputField, true, true, $user);
  112. }
  113. }
  114. public function save($bean, $inputData, $field, $def, $prefix = '') {
  115. global $timedate;
  116. if ( !isset($inputData[$prefix.$field]) ) {
  117. return;
  118. }
  119. $offset = strlen(trim($inputData[$prefix.$field])) < 11 ? false : true;
  120. if ($timedate->check_matching_format($inputData[$prefix.$field], TimeDate::DB_DATE_FORMAT)) {
  121. $bean->$field = $inputData[$prefix.$field];
  122. } else {
  123. $bean->$field = $timedate->to_db_date($inputData[$prefix.$field], $offset);
  124. }
  125. }
  126. /**
  127. * @see SugarFieldBase::importSanitize()
  128. */
  129. public function importSanitize(
  130. $value,
  131. $vardef,
  132. $focus,
  133. ImportFieldSanitize $settings
  134. )
  135. {
  136. global $timedate;
  137. $format = $timedate->merge_date_time($settings->dateformat, $settings->timeformat);
  138. if ( !$timedate->check_matching_format($value, $format) ) {
  139. $parts = $timedate->split_date_time($value);
  140. if(empty($parts[0])) {
  141. $datepart = $timedate->getNow()->format($settings->dateformat);
  142. }
  143. else {
  144. $datepart = $parts[0];
  145. }
  146. if(empty($parts[1])) {
  147. $timepart = $timedate->fromTimestamp(0)->format($settings->timeformat);
  148. } else {
  149. $timepart = $parts[1];
  150. // see if we can get by stripping the seconds
  151. if(strpos($settings->timeformat, 's') === false) {
  152. $sep = $timedate->timeSeparatorFormat($settings->timeformat);
  153. // We are assuming here seconds are the last component, which
  154. // is kind of reasonable - no sane time format puts seconds first
  155. $timeparts = explode($sep, $timepart);
  156. if(!empty($timeparts[2])) {
  157. $timepart = join($sep, array($timeparts[0], $timeparts[1]));
  158. }
  159. }
  160. }
  161. $value = $timedate->merge_date_time($datepart, $timepart);
  162. if ( !$timedate->check_matching_format($value, $format) ) {
  163. return false;
  164. }
  165. }
  166. try {
  167. $date = SugarDateTime::createFromFormat($format, $value, new DateTimeZone($settings->timezone));
  168. } catch(Exception $e) {
  169. return false;
  170. }
  171. return $date->asDb();
  172. }
  173. function getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
  174. global $timedate,$current_user;
  175. //check to see if the date is in the proper format
  176. $user_dateFormat = $timedate->get_date_format();
  177. if(!empty($vardef['value']) && !$timedate->check_matching_format( $vardef['value'],$user_dateFormat)){
  178. //date is not in proper user format, so get the SugarDateTiemObject and inject the vardef with a new element
  179. $sdt = $timedate->fromString($vardef['value'],$current_user);
  180. if (!empty($sdt)) {
  181. //the new 'date_formatted_value' array element will be used in include/SugarFields/Fields/Datetime/DetailView.tpl if it exists
  182. $vardef['date_formatted_value'] = $timedate->asUserDate($sdt,$current_user);
  183. }
  184. }
  185. return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'DetailView');
  186. }
  187. }