| Conditions | 183 |
| Paths | 6 |
| Total Lines | 1002 |
| Code Lines | 491 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 859 | public function doCollectOneCollector() |
||
| 860 | { |
||
| 861 | global $conf, $langs, $user; |
||
| 862 | |||
| 863 | //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log'; |
||
| 864 | |||
| 865 | require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php'; |
||
| 866 | |||
| 867 | dol_syslog("EmailCollector::doCollectOneCollector start", LOG_DEBUG); |
||
| 868 | |||
| 869 | $langs->loadLangs(array("project", "companies", "mails", "errors", "ticket")); |
||
| 870 | |||
| 871 | $error = 0; |
||
| 872 | $this->output = ''; |
||
| 873 | $this->error=''; |
||
| 874 | |||
| 875 | $now = dol_now(); |
||
| 876 | |||
| 877 | if (empty($this->host)) |
||
| 878 | { |
||
| 879 | $this->error=$langs->trans('ErrorFieldRequired', 'EMailHost'); |
||
| 880 | return -1; |
||
| 881 | } |
||
| 882 | if (empty($this->login)) |
||
| 883 | { |
||
| 884 | $this->error=$langs->trans('ErrorFieldRequired', 'Login'); |
||
| 885 | return -1; |
||
| 886 | } |
||
| 887 | if (empty($this->source_directory)) |
||
| 888 | { |
||
| 889 | $this->error=$langs->trans('ErrorFieldRequired', 'MailboxSourceDirectory'); |
||
| 890 | return -1; |
||
| 891 | } |
||
| 892 | if (! function_exists('imap_open')) |
||
| 893 | { |
||
| 894 | $this->error='IMAP function not enabled on your PHP'; |
||
| 895 | return -2; |
||
| 896 | } |
||
| 897 | |||
| 898 | $this->fetchFilters(); |
||
| 899 | $this->fetchActions(); |
||
| 900 | |||
| 901 | $sourcedir = $this->source_directory; |
||
| 902 | $targetdir = ($this->target_directory ? $this->target_directory : ''); // Can be '[Gmail]/Trash' or 'mytag' |
||
| 903 | |||
| 904 | $connectstringserver = $this->getConnectStringIMAP(); |
||
| 905 | $connectstringsource = $connectstringserver.imap_utf7_encode($sourcedir); |
||
| 906 | $connectstringtarget = $connectstringserver.imap_utf7_encode($targetdir); |
||
| 907 | |||
| 908 | $connection = imap_open($connectstringsource, $this->login, $this->password); |
||
| 909 | if (! $connection) |
||
| 910 | { |
||
| 911 | $this->error = 'Failed to open IMAP connection '.$connectstringsource; |
||
| 912 | return -3; |
||
| 913 | } |
||
| 914 | imap_errors(); // Clear stack of errors. |
||
| 915 | |||
| 916 | // $conf->global->MAIL_PREFIX_FOR_EMAIL_ID must be defined |
||
| 917 | $host=dol_getprefix('email'); |
||
| 918 | |||
| 919 | // Define the IMAP search string |
||
| 920 | // See https://tools.ietf.org/html/rfc3501#section-6.4.4 for IMAPv4 (PHP not yet compatible) |
||
| 921 | // See https://tools.ietf.org/html/rfc1064 page 13 for IMAPv2 |
||
| 922 | //$search='ALL'; |
||
| 923 | $search='UNDELETED'; // Seems not supported by some servers |
||
| 924 | $searchhead=''; |
||
| 925 | $searchfilterdoltrackid=0; |
||
| 926 | $searchfilternodoltrackid=0; |
||
| 927 | foreach($this->filters as $rule) |
||
| 928 | { |
||
| 929 | if (empty($rule['status'])) continue; |
||
| 930 | |||
| 931 | if ($rule['type'] == 'to') $search.=($search?' ':'').'TO "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 932 | if ($rule['type'] == 'bcc') $search.=($search?' ':'').'BCC'; |
||
| 933 | if ($rule['type'] == 'cc') $search.=($search?' ':'').'CC'; |
||
| 934 | if ($rule['type'] == 'from') $search.=($search?' ':'').'FROM "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 935 | if ($rule['type'] == 'subject') $search.=($search?' ':'').'SUBJECT "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 936 | if ($rule['type'] == 'body') $search.=($search?' ':'').'BODY "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 937 | if ($rule['type'] == 'header') $search.=($search?' ':'').'HEADER '.$rule['rulevalue']; |
||
| 938 | |||
| 939 | if ($rule['type'] == 'notinsubject') $search.=($search?' ':'').'SUBJECT NOT "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 940 | if ($rule['type'] == 'notinbody') $search.=($search?' ':'').'BODY NOT "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 941 | |||
| 942 | if ($rule['type'] == 'seen') $search.=($search?' ':'').'SEEN'; |
||
| 943 | if ($rule['type'] == 'unseen') $search.=($search?' ':'').'UNSEEN'; |
||
| 944 | if ($rule['type'] == 'unanswered') $search.=($search?' ':'').'UNANSWERED'; |
||
| 945 | if ($rule['type'] == 'answered') $search.=($search?' ':'').'ANSWERED'; |
||
| 946 | if ($rule['type'] == 'smaller') $search.=($search?' ':'').'SMALLER "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 947 | if ($rule['type'] == 'larger') $search.=($search?' ':'').'LARGER "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 948 | |||
| 949 | if ($rule['type'] == 'withtrackingid') { $searchfilterdoltrackid++; $searchhead.='/References.*@'.preg_quote($host, '/').'/'; } |
||
| 950 | if ($rule['type'] == 'withouttrackingid') { $searchfilternodoltrackid++; $searchhead.='! /References.*@'.preg_quote($host, '/').'/';} |
||
| 951 | } |
||
| 952 | |||
| 953 | if (empty($targetdir)) // Use last date as filter if there is no targetdir defined. |
||
| 954 | { |
||
| 955 | $fromdate=0; |
||
| 956 | if ($this->datelastok) $fromdate = $this->datelastok; |
||
| 957 | if ($fromdate > 0) $search.=($search?' ':'').'SINCE '.date('j-M-Y', $fromdate - 1); // SENTSINCE not supported. Date must be X-Abc-9999 (X on 1 digit if < 10) |
||
| 958 | //$search.=($search?' ':'').'SINCE 8-Apr-2018'; |
||
| 959 | } |
||
| 960 | dol_syslog("IMAP search string = ".$search); |
||
| 961 | //var_dump($search); |
||
| 962 | |||
| 963 | $nbemailprocessed=0; |
||
| 964 | $nbemailok=0; |
||
| 965 | $nbactiondone=0; |
||
| 966 | |||
| 967 | // Scan IMAP inbox |
||
| 968 | $arrayofemail= imap_search($connection, $search, null, "UTF-8"); |
||
| 969 | if ($arrayofemail === false) |
||
| 970 | { |
||
| 971 | // Nothing found or search string not understood |
||
| 972 | $mapoferrrors = imap_errors(); |
||
| 973 | if ($mapoferrrors !== false) |
||
| 974 | { |
||
| 975 | $error++; |
||
| 976 | $this->error = "Search string not understood - ".join(',', $mapoferrrors); |
||
| 977 | $this->errors[] = $this->error; |
||
| 978 | } |
||
| 979 | } |
||
| 980 | |||
| 981 | // Loop on each email found |
||
| 982 | if (! $error && ! empty($arrayofemail) && count($arrayofemail) > 0) |
||
| 983 | { |
||
| 984 | // Loop to get part html and plain |
||
| 985 | /* |
||
| 986 | 0 multipart/mixed |
||
| 987 | 1 multipart/alternative |
||
| 988 | 1.1 text/plain |
||
| 989 | 1.2 text/html |
||
| 990 | 2 message/rfc822 |
||
| 991 | 2 multipart/mixed |
||
| 992 | 2.1 multipart/alternative |
||
| 993 | 2.1.1 text/plain |
||
| 994 | 2.1.2 text/html |
||
| 995 | 2.2 message/rfc822 |
||
| 996 | 2.2 multipart/alternative |
||
| 997 | 2.2.1 text/plain |
||
| 998 | 2.2.2 text/html |
||
| 999 | */ |
||
| 1000 | /** |
||
| 1001 | * create_part_array |
||
| 1002 | * |
||
| 1003 | * @param Object $structure Structure |
||
| 1004 | * @param string $prefix prefix |
||
| 1005 | * @return array Array with number and object |
||
| 1006 | */ |
||
| 1007 | /*function createPartArray($structure, $prefix = "") |
||
| 1008 | { |
||
| 1009 | //print_r($structure); |
||
| 1010 | $part_array=array(); |
||
| 1011 | if (count($structure->parts) > 0) { // There some sub parts |
||
| 1012 | foreach ($structure->parts as $count => $part) { |
||
| 1013 | addPartToArray($part, $prefix.($count+1), $part_array); |
||
| 1014 | } |
||
| 1015 | }else{ // Email does not have a seperate mime attachment for text |
||
| 1016 | $part_array[] = array('part_number' => $prefix.'1', 'part_object' => $structure); |
||
| 1017 | } |
||
| 1018 | return $part_array; |
||
| 1019 | }*/ |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Sub function for createPartArray(). Only called by createPartArray() and itself. |
||
| 1023 | * |
||
| 1024 | * @param Object $obj Structure |
||
| 1025 | * @param string $partno Part no |
||
| 1026 | * @param array $part_array array |
||
| 1027 | * @return void |
||
| 1028 | */ |
||
| 1029 | /*function addPartToArray($obj, $partno, &$part_array) |
||
| 1030 | { |
||
| 1031 | $part_array[] = array('part_number' => $partno, 'part_object' => $obj); |
||
| 1032 | if ($obj->type == 2) { // Check to see if the part is an attached email message, as in the RFC-822 type |
||
| 1033 | //print_r($obj); |
||
| 1034 | if (array_key_exists('parts', $obj)) { // Check to see if the email has parts |
||
| 1035 | foreach ($obj->parts as $count => $part) { |
||
| 1036 | // Iterate here again to compensate for the broken way that imap_fetchbody() handles attachments |
||
| 1037 | if (count($part->parts) > 0) { |
||
| 1038 | foreach ($part->parts as $count2 => $part2) { |
||
| 1039 | addPartToArray($part2, $partno.".".($count2+1), $part_array); |
||
| 1040 | } |
||
| 1041 | }else{ // Attached email does not have a seperate mime attachment for text |
||
| 1042 | $part_array[] = array('part_number' => $partno.'.'.($count+1), 'part_object' => $obj); |
||
| 1043 | } |
||
| 1044 | } |
||
| 1045 | }else{ // Not sure if this is possible |
||
| 1046 | $part_array[] = array('part_number' => $partno.'.1', 'part_object' => $obj); |
||
| 1047 | } |
||
| 1048 | }else{ // If there are more sub-parts, expand them out. |
||
| 1049 | if (array_key_exists('parts', $obj)) { |
||
| 1050 | foreach ($obj->parts as $count => $p) { |
||
| 1051 | addPartToArray($p, $partno.".".($count+1), $part_array); |
||
| 1052 | } |
||
| 1053 | } |
||
| 1054 | } |
||
| 1055 | }*/ |
||
| 1056 | |||
| 1057 | dol_syslog("Start of loop on email", LOG_INFO, 1); |
||
| 1058 | |||
| 1059 | foreach($arrayofemail as $imapemail) |
||
| 1060 | { |
||
| 1061 | if ($nbemailprocessed > 1000) |
||
| 1062 | { |
||
| 1063 | break; // Do not process more than 1000 email per launch (this is a different protection than maxnbcollectedpercollect |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | $header = imap_fetchheader($connection, $imapemail, 0); |
||
| 1067 | $matches=array(); |
||
| 1068 | preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $header, $matches); |
||
| 1069 | $headers = array_combine($matches[1], $matches[2]); |
||
| 1070 | //var_dump($headers); |
||
| 1071 | |||
| 1072 | // If there is a filter on trackid |
||
| 1073 | if ($searchfilterdoltrackid > 0) |
||
| 1074 | { |
||
| 1075 | //if (empty($headers['X-Dolibarr-TRACKID'])) continue; |
||
| 1076 | if (empty($headers['References']) || ! preg_match('/@'.preg_quote($host, '/').'/', $headers['References'])) |
||
| 1077 | { |
||
| 1078 | $nbemailprocessed++; |
||
| 1079 | continue; |
||
| 1080 | } |
||
| 1081 | } |
||
| 1082 | if ($searchfilternodoltrackid > 0) |
||
| 1083 | { |
||
| 1084 | if (! empty($headers['References']) && preg_match('/@'.preg_quote($host, '/').'/', $headers['References'])) |
||
| 1085 | { |
||
| 1086 | $nbemailprocessed++; |
||
| 1087 | continue; |
||
| 1088 | } |
||
| 1089 | //if (! empty($headers['X-Dolibarr-TRACKID']) continue; |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | $thirdpartystatic=new Societe($this->db); |
||
| 1093 | $contactstatic=new Contact($this->db); |
||
| 1094 | $projectstatic=new Project($this->db); |
||
| 1095 | |||
| 1096 | $nbactiondoneforemail = 0; |
||
| 1097 | $errorforemail = 0; |
||
| 1098 | $errorforactions = 0; |
||
| 1099 | $thirdpartyfoundby = ''; |
||
| 1100 | $contactfoundby = ''; |
||
| 1101 | $projectfoundby = ''; |
||
| 1102 | |||
| 1103 | $this->db->begin(); |
||
| 1104 | |||
| 1105 | // GET Email meta datas |
||
| 1106 | $overview = imap_fetch_overview($connection, $imapemail, 0); |
||
| 1107 | |||
| 1108 | dol_syslog("** Process email - msgid=".$overview[0]->message_id." date=".dol_print_date($overview[0]->udate, 'dayrfc', 'gmt')." subject=".$overview[0]->subject); |
||
| 1109 | |||
| 1110 | // Parse IMAP email structure |
||
| 1111 | global $htmlmsg, $plainmsg, $charset, $attachments; |
||
| 1112 | $this->getmsg($connection, $imapemail); |
||
| 1113 | |||
| 1114 | //$htmlmsg,$plainmsg,$charset,$attachments |
||
| 1115 | $messagetext = $plainmsg ? $plainmsg : dol_string_nohtmltag($htmlmsg, 0); |
||
| 1116 | /*var_dump($plainmsg); |
||
| 1117 | var_dump($htmlmsg); |
||
| 1118 | var_dump($messagetext);*/ |
||
| 1119 | /*var_dump($charset); |
||
| 1120 | var_dump($attachments); |
||
| 1121 | exit;*/ |
||
| 1122 | |||
| 1123 | // Parse IMAP email structure |
||
| 1124 | /* |
||
| 1125 | $structure = imap_fetchstructure($connection, $imapemail, 0); |
||
| 1126 | |||
| 1127 | $partplain = $parthtml = -1; |
||
| 1128 | $encodingplain = $encodinghtml = ''; |
||
| 1129 | |||
| 1130 | $result = createPartArray($structure, ''); |
||
| 1131 | |||
| 1132 | foreach($result as $part) |
||
| 1133 | { |
||
| 1134 | // $part['part_object']->type seems 0 for content |
||
| 1135 | // $part['part_object']->type seems 5 for attachment |
||
| 1136 | if (empty($part['part_object'])) continue; |
||
| 1137 | if ($part['part_object']->subtype == 'HTML') |
||
| 1138 | { |
||
| 1139 | $parthtml=$part['part_number']; |
||
| 1140 | if ($part['part_object']->encoding == 4) |
||
| 1141 | { |
||
| 1142 | $encodinghtml = 'aaa'; |
||
| 1143 | } |
||
| 1144 | } |
||
| 1145 | if ($part['part_object']->subtype == 'PLAIN') |
||
| 1146 | { |
||
| 1147 | $partplain=$part['part_number']; |
||
| 1148 | if ($part['part_object']->encoding == 4) |
||
| 1149 | { |
||
| 1150 | $encodingplain = 'rr'; |
||
| 1151 | } |
||
| 1152 | } |
||
| 1153 | } |
||
| 1154 | //var_dump($result); var_dump($partplain); var_dump($parthtml); |
||
| 1155 | |||
| 1156 | var_dump($structure); |
||
| 1157 | var_dump($parthtml); |
||
| 1158 | var_dump($partplain); |
||
| 1159 | |||
| 1160 | $messagetext = imap_fetchbody($connection, $imapemail, ($parthtml != '-1' ? $parthtml : ($partplain != '-1' ? $partplain : 1)), FT_PEEK); |
||
| 1161 | */ |
||
| 1162 | |||
| 1163 | //var_dump($messagetext); |
||
| 1164 | //var_dump($structure->parts[0]->parts); |
||
| 1165 | //print $header; |
||
| 1166 | //print $messagetext; |
||
| 1167 | //exit; |
||
| 1168 | |||
| 1169 | $fromstring=$overview[0]->from; |
||
| 1170 | $sender=$overview[0]->sender; |
||
| 1171 | $to=$overview[0]->to; |
||
| 1172 | $sendtocc=$overview[0]->cc; |
||
| 1173 | $sendtobcc=$overview[0]->bcc; |
||
| 1174 | $date=$overview[0]->udate; |
||
| 1175 | $msgid=str_replace(array('<','>'), '', $overview[0]->message_id); |
||
| 1176 | $subject=$overview[0]->subject; |
||
| 1177 | //var_dump($msgid);exit; |
||
| 1178 | |||
| 1179 | $reg=array(); |
||
| 1180 | if (preg_match('/^(.*)<(.*)>$/', $fromstring, $reg)) |
||
| 1181 | { |
||
| 1182 | $from=$reg[2]; |
||
| 1183 | $fromtext=$reg[1]; |
||
| 1184 | } |
||
| 1185 | else |
||
| 1186 | { |
||
| 1187 | $from = $fromstring; |
||
| 1188 | $fromtext=''; |
||
| 1189 | } |
||
| 1190 | $fk_element_id = 0; $fk_element_type = ''; |
||
| 1191 | |||
| 1192 | $contactid = 0; $thirdpartyid = 0; $projectid = 0; |
||
| 1193 | |||
| 1194 | // Analyze TrackId in field References. For example: |
||
| 1195 | // References: <1542377954.SMTPs-dolibarr-thi649@8f6014fde11ec6cdec9a822234fc557e> |
||
| 1196 | // References: <1542377954.SMTPs-dolibarr-tic649@8f6014fde11ec6cdec9a822234fc557e> |
||
| 1197 | // References: <1542377954.SMTPs-dolibarr-abc649@8f6014fde11ec6cdec9a822234fc557e> |
||
| 1198 | $trackid = ''; |
||
| 1199 | $reg=array(); |
||
| 1200 | if (! empty($headers['References']) && preg_match('/dolibarr-([a-z]+)([0-9]+)@'.preg_quote($host, '/').'/', $headers['References'], $reg)) |
||
| 1201 | { |
||
| 1202 | $trackid = $reg[1].$reg[2]; |
||
| 1203 | |||
| 1204 | $objectid = 0; |
||
| 1205 | $objectemail = null; |
||
| 1206 | if ($reg[0] == 'inv') |
||
| 1207 | { |
||
| 1208 | $objectid = $reg[1]; |
||
| 1209 | $objectemail = new Facture($this->db); |
||
| 1210 | } |
||
| 1211 | if ($reg[0] == 'proj') |
||
| 1212 | { |
||
| 1213 | $objectid = $reg[1]; |
||
| 1214 | $objectemail = new Project($this->db); |
||
| 1215 | } |
||
| 1216 | if ($reg[0] == 'con') |
||
| 1217 | { |
||
| 1218 | $objectid = $reg[1]; |
||
| 1219 | $objectemail = new Contact($this->db); |
||
| 1220 | } |
||
| 1221 | if ($reg[0] == 'thi') |
||
| 1222 | { |
||
| 1223 | $objectid = $reg[1]; |
||
| 1224 | $objectemail = new Societe($this->db); |
||
| 1225 | } |
||
| 1226 | if ($reg[0] == 'use') |
||
| 1227 | { |
||
| 1228 | $objectid = $reg[1]; |
||
| 1229 | $objectemail = new User($this->db); |
||
| 1230 | } |
||
| 1231 | if ($reg[0] == 'tic') |
||
| 1232 | { |
||
| 1233 | $objectid = $reg[1]; |
||
| 1234 | $objectemail = new Ticket($this->db); |
||
| 1235 | } |
||
| 1236 | |||
| 1237 | if (is_object($objectemail)) |
||
| 1238 | { |
||
| 1239 | $result = $objectemail->fetch($objectid); |
||
| 1240 | if ($result > 0) |
||
| 1241 | { |
||
| 1242 | $fk_element_id = $objectemail->id; |
||
| 1243 | $fk_element_type = $objectemail->element; |
||
| 1244 | // Fix fk_element_type |
||
| 1245 | if ($fk_element_type == 'facture') $fk_element_type = 'invoice'; |
||
| 1246 | |||
| 1247 | $thirdpartyid = $objectemail->fk_soc; |
||
| 1248 | $contactid = $objectemail->fk_socpeople; |
||
| 1249 | $projectid = isset($objectemail->fk_project)?$objectemail->fk_project:$objectemail->fk_projet; |
||
| 1250 | } |
||
| 1251 | } |
||
| 1252 | |||
| 1253 | // Project |
||
| 1254 | if ($projectid > 0) |
||
| 1255 | { |
||
| 1256 | $result = $projectstatic->fetch($projectid); |
||
| 1257 | if ($result <= 0) $projectstatic->id = 0; |
||
| 1258 | else |
||
| 1259 | { |
||
| 1260 | $projectid = $projectstatic->id; |
||
| 1261 | $projectfoundby = 'trackid ('.$trackid.')'; |
||
| 1262 | if (empty($contactid)) $contactid = $projectstatic->fk_contact; |
||
| 1263 | if (empty($thirdpartyid)) $thirdpartyid = $projectstatic->fk_soc; |
||
| 1264 | } |
||
| 1265 | } |
||
| 1266 | // Contact |
||
| 1267 | if ($contactid > 0) |
||
| 1268 | { |
||
| 1269 | $result = $contactstatic->fetch($contactid); |
||
| 1270 | if ($result <= 0) $contactstatic->id = 0; |
||
| 1271 | else |
||
| 1272 | { |
||
| 1273 | $contactid = $contactstatic->id; |
||
| 1274 | $contactfoundby = 'trackid ('.$trackid.')'; |
||
| 1275 | if (empty($thirdpartyid)) $thirdpartyid = $contactstatic->fk_soc; |
||
| 1276 | } |
||
| 1277 | } |
||
| 1278 | // Thirdparty |
||
| 1279 | if ($thirdpartyid > 0) |
||
| 1280 | { |
||
| 1281 | $result = $thirdpartystatic->fetch($thirdpartyid); |
||
| 1282 | if ($result <= 0) $thirdpartystatic->id = 0; |
||
| 1283 | else |
||
| 1284 | { |
||
| 1285 | $thirdpartyid = $thirdpartystatic->id; |
||
| 1286 | $thirdpartyfoundby = 'trackid ('.$trackid.')'; |
||
| 1287 | } |
||
| 1288 | } |
||
| 1289 | } |
||
| 1290 | |||
| 1291 | if (empty($contactid)) // Try to find contact using email |
||
| 1292 | { |
||
| 1293 | $result = $contactstatic->fetch(0, null, '', $from); |
||
| 1294 | |||
| 1295 | if ($result > 0) |
||
| 1296 | { |
||
| 1297 | $contactid = $contactstatic->id; |
||
| 1298 | $contactfoundby = 'email of contact ('.$from.')'; |
||
| 1299 | if ($contactstatic->socid > 0) |
||
| 1300 | { |
||
| 1301 | $result = $thirdpartystatic->fetch($contactstatic->socid); |
||
| 1302 | if ($result > 0) |
||
| 1303 | { |
||
| 1304 | $thirdpartyid = $thirdpartystatic->id; |
||
| 1305 | $thirdpartyfoundby = 'email of contact ('.$from.')'; |
||
| 1306 | } |
||
| 1307 | } |
||
| 1308 | } |
||
| 1309 | } |
||
| 1310 | |||
| 1311 | if (empty($thirdpartyid)) // Try to find thirdparty using email |
||
| 1312 | { |
||
| 1313 | $result = $thirdpartystatic->fetch(0, '', '', '', '', '', '', '', '', '', $from); |
||
| 1314 | if ($result > 0) $thirdpartyfoundby = 'email ('.$from.')'; |
||
| 1315 | } |
||
| 1316 | |||
| 1317 | // Do operations |
||
| 1318 | foreach($this->actions as $operation) |
||
| 1319 | { |
||
| 1320 | if ($errorforactions) break; |
||
| 1321 | if (empty($operation['status'])) continue; |
||
| 1322 | |||
| 1323 | // Make Operation |
||
| 1324 | dol_syslog("Execute action ".$operation['type']." actionparam=".$operation['actionparam'].' thirdpartystatic->id='.$thirdpartystatic->id.' contactstatic->id='.$contactstatic->id.' projectstatic->id='.$projectstatic->id); |
||
| 1325 | |||
| 1326 | $actioncode = 'EMAIL_IN'; |
||
| 1327 | // If we scan the Sent box, we use the code for out email |
||
| 1328 | if ($this->source_directory == 'Sent') $actioncode = 'EMAIL_OUT'; |
||
| 1329 | |||
| 1330 | $description = $descriptiontitle = $descriptionmeta = $descriptionfull = ''; |
||
| 1331 | if (in_array($operation['type'], array('recordevent', 'project', 'ticket'))) |
||
| 1332 | { |
||
| 1333 | if ($operation['type'] == 'project') $descriptiontitle = $langs->trans("ProjectCreatedByEmailCollector", $msgid); |
||
| 1334 | elseif ($operation['type'] == 'ticket') $descriptiontitle = $langs->trans("TicketCreatedByEmailCollector", $msgid); |
||
| 1335 | else $descriptiontitle = $langs->trans("ActionAC_".$actioncode).' - '.$langs->trans("MailFrom").' '.$from; |
||
| 1336 | |||
| 1337 | $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("MailTopic").' : '.dol_escape_htmltag($subject)); |
||
| 1338 | $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("MailFrom").($langs->trans("MailFrom") != 'From' ? ' (From)':'').' : '.dol_escape_htmltag($fromstring)); |
||
| 1339 | if ($sender) $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("Sender").($langs->trans("Sender") != 'Sender' ? ' (Sender)':'').' : '.dol_escape_htmltag($sender)); |
||
| 1340 | $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("MailTo").($langs->trans("MailTo") != 'To' ? ' (To)':'').' : '.dol_escape_htmltag($to)); |
||
| 1341 | if ($sendtocc) $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("MailCC").($langs->trans("MailCC") != 'CC' ? ' (CC)':'').' : '.dol_escape_htmltag($sendtocc)); |
||
| 1342 | //if ($bcc) $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("Bcc").' : '.dol_escape_htmltag($bcc)); |
||
| 1343 | } |
||
| 1344 | |||
| 1345 | // Search and create thirdparty |
||
| 1346 | if ($operation['type'] == 'loadthirdparty' || $operation['type'] == 'loadandcreatethirdparty') |
||
| 1347 | { |
||
| 1348 | if (empty($operation['actionparam'])) |
||
| 1349 | { |
||
| 1350 | $errorforactions++; |
||
| 1351 | $this->error = "Action loadthirdparty or loadandcreatethirdparty has empty parameter. Must be 'SET:xxx' or 'EXTRACT:(body|subject):regex' to define how to extract data"; |
||
| 1352 | $this->errors[] = $this->error; |
||
| 1353 | } |
||
| 1354 | else |
||
| 1355 | { |
||
| 1356 | $actionparam = $operation['actionparam']; |
||
| 1357 | $nametouseforthirdparty=''; |
||
| 1358 | |||
| 1359 | // $this->actionparam = 'SET:aaa' or 'EXTRACT:BODY:....' |
||
| 1360 | $arrayvaluetouse = dolExplodeIntoArray($actionparam, ';', '='); |
||
| 1361 | foreach($arrayvaluetouse as $propertytooverwrite => $valueforproperty) |
||
| 1362 | { |
||
| 1363 | $sourcestring=''; |
||
| 1364 | $sourcefield=''; |
||
| 1365 | $regexstring=''; |
||
| 1366 | $regforregex=array(); |
||
| 1367 | |||
| 1368 | if (preg_match('/^EXTRACT:([a-zA-Z0-9]+):(.*)$/', $valueforproperty, $regforregex)) |
||
| 1369 | { |
||
| 1370 | $sourcefield=$regforregex[1]; |
||
| 1371 | $regexstring=$regforregex[2]; |
||
| 1372 | } |
||
| 1373 | |||
| 1374 | if (! empty($sourcefield) && ! empty($regexstring)) |
||
| 1375 | { |
||
| 1376 | if (strtolower($sourcefield) == 'body') $sourcestring=$messagetext; |
||
| 1377 | elseif (strtolower($sourcefield) == 'subject') $sourcestring=$subject; |
||
| 1378 | elseif (strtolower($sourcefield) == 'header') $sourcestring=$header; |
||
| 1379 | |||
| 1380 | if ($sourcestring) |
||
| 1381 | { |
||
| 1382 | $regforval=array(); |
||
| 1383 | //var_dump($regexstring);var_dump($sourcestring); |
||
| 1384 | if (preg_match('/'.$regexstring.'/ms', $sourcestring, $regforval)) |
||
| 1385 | { |
||
| 1386 | //var_dump($regforval[1]);exit; |
||
| 1387 | // Overwrite param $tmpproperty |
||
| 1388 | $nametouseforthirdparty = isset($regforval[1])?trim($regforval[1]):null; |
||
| 1389 | } |
||
| 1390 | else |
||
| 1391 | { |
||
| 1392 | // Regex not found |
||
| 1393 | $nametouseforthirdparty = null; |
||
| 1394 | } |
||
| 1395 | //var_dump($object->$tmpproperty);exit; |
||
| 1396 | } |
||
| 1397 | else |
||
| 1398 | { |
||
| 1399 | // Nothing can be done for this param |
||
| 1400 | $errorforactions++; |
||
| 1401 | $this->error = 'The extract rule to use to load thirdparty has on an unknown source (must be HEADER, SUBJECT or BODY)'; |
||
| 1402 | $this->errors[] = $this->error; |
||
| 1403 | } |
||
| 1404 | } |
||
| 1405 | elseif (preg_match('/^(SET|SETIFEMPTY):(.*)$/', $valueforproperty, $reg)) |
||
| 1406 | { |
||
| 1407 | //if (preg_match('/^options_/', $tmpproperty)) $object->array_options[preg_replace('/^options_/', '', $tmpproperty)] = $reg[1]; |
||
| 1408 | //else $object->$tmpproperty = $reg[1]; |
||
| 1409 | $nametouseforthirdparty = $reg[2]; |
||
| 1410 | } |
||
| 1411 | else |
||
| 1412 | { |
||
| 1413 | $errorforactions++; |
||
| 1414 | $this->error = 'Bad syntax for description of action parameters: '.$actionparam; |
||
| 1415 | $this->errors[] = $this->error; |
||
| 1416 | break; |
||
| 1417 | } |
||
| 1418 | } |
||
| 1419 | |||
| 1420 | if (! $errorforactions && $nametouseforthirdparty) |
||
| 1421 | { |
||
| 1422 | $result = $thirdpartystatic->fetch(0, $nametouseforthirdparty); |
||
| 1423 | if ($result < 0) |
||
| 1424 | { |
||
| 1425 | $errorforactions++; |
||
| 1426 | $this->error = 'Error when getting thirdparty with name '.$nametouseforthirdparty.' (may be 2 record exists with same name ?)'; |
||
| 1427 | $this->errors[] = $this->error; |
||
| 1428 | break; |
||
| 1429 | } |
||
| 1430 | elseif ($result == 0) |
||
| 1431 | { |
||
| 1432 | if ($operation['type'] == 'loadthirdparty') |
||
| 1433 | { |
||
| 1434 | dol_syslog("Third party with name ".$nametouseforthirdparty." was not found"); |
||
| 1435 | |||
| 1436 | $errorforactions++; |
||
| 1437 | $this->error = 'ErrorFailedToLoadThirdParty'; |
||
| 1438 | $this->errors[] = 'ErrorFailedToLoadThirdParty'; |
||
| 1439 | } |
||
| 1440 | elseif ($operation['type'] == 'loadandcreatethirdparty') |
||
| 1441 | { |
||
| 1442 | dol_syslog("Third party with name ".$nametouseforthirdparty." was not found. We try to create it."); |
||
| 1443 | |||
| 1444 | // Create thirdparty |
||
| 1445 | $thirdpartystatic->name = $nametouseforthirdparty; |
||
| 1446 | if ($fromtext != $nametouseforthirdparty) $thirdpartystatic->name_alias = $fromtext; |
||
| 1447 | $thirdpartystatic->email = $from; |
||
| 1448 | |||
| 1449 | // Overwrite values with values extracted from source email |
||
| 1450 | $errorforthisaction = $this->overwritePropertiesOfObject($thirdpartystatic, $operation['actionparam'], $messagetext, $subject, $header); |
||
| 1451 | |||
| 1452 | if ($errorforthisaction) |
||
| 1453 | { |
||
| 1454 | $errorforactions++; |
||
| 1455 | } |
||
| 1456 | else |
||
| 1457 | { |
||
| 1458 | $result = $thirdpartystatic->create($user); |
||
| 1459 | if ($result <= 0) |
||
| 1460 | { |
||
| 1461 | $errorforactions++; |
||
| 1462 | $this->error = $thirdpartystatic->error; |
||
| 1463 | $this->errors = $thirdpartystatic->errors; |
||
| 1464 | } |
||
| 1465 | } |
||
| 1466 | } |
||
| 1467 | } |
||
| 1468 | } |
||
| 1469 | } |
||
| 1470 | } |
||
| 1471 | // Create event |
||
| 1472 | elseif ($operation['type'] == 'recordevent') |
||
| 1473 | { |
||
| 1474 | if ($projectstatic->id > 0) |
||
| 1475 | { |
||
| 1476 | if ($projectfoundby) $descriptionmeta = dol_concatdesc($descriptionmeta, 'Project found from '.$projectfoundby); |
||
| 1477 | } |
||
| 1478 | if ($thirdpartystatic->id > 0) |
||
| 1479 | { |
||
| 1480 | if ($thirdpartyfoundby) $descriptionmeta = dol_concatdesc($descriptionmeta, 'Third party found from '.$thirdpartyfoundby); |
||
| 1481 | } |
||
| 1482 | if ($contactstatic->id > 0) |
||
| 1483 | { |
||
| 1484 | if ($contactfoundby) $descriptionmeta = dol_concatdesc($descriptionmeta, 'Contact/address found from '.$contactfoundby); |
||
| 1485 | } |
||
| 1486 | |||
| 1487 | $description = $descriptiontitle; |
||
| 1488 | $description = dol_concatdesc($description, "-----"); |
||
| 1489 | $description = dol_concatdesc($description, $descriptionmeta); |
||
| 1490 | $description = dol_concatdesc($description, "-----"); |
||
| 1491 | $description = dol_concatdesc($description, $messagetext); |
||
| 1492 | |||
| 1493 | $descriptionfull = $description; |
||
| 1494 | $descriptionfull = dol_concatdesc($descriptionfull, "----- Header"); |
||
| 1495 | $descriptionfull = dol_concatdesc($descriptionfull, $header); |
||
| 1496 | |||
| 1497 | // Insert record of emails sent |
||
| 1498 | $actioncomm = new ActionComm($this->db); |
||
| 1499 | $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...) |
||
| 1500 | $actioncomm->code = 'AC_'.$actioncode; |
||
| 1501 | $actioncomm->label = $langs->trans("ActionAC_".$actioncode).' - '.$langs->trans("MailFrom").' '.$from; |
||
| 1502 | $actioncomm->note = $descriptionfull; |
||
| 1503 | $actioncomm->fk_project = $projectstatic->id; |
||
| 1504 | $actioncomm->datep = $date; |
||
| 1505 | $actioncomm->datef = $date; |
||
| 1506 | $actioncomm->percentage = -1; // Not applicable |
||
| 1507 | $actioncomm->socid = $thirdpartystatic->id; |
||
| 1508 | $actioncomm->contactid = $contactstatic->id; |
||
| 1509 | $actioncomm->socpeopleassigned = (!empty($contactstatic->id) ? array($contactstatic->id => '') : array()); |
||
| 1510 | $actioncomm->authorid = $user->id; // User saving action |
||
| 1511 | $actioncomm->userownerid = $user->id; // Owner of action |
||
| 1512 | // Fields when action is an email (content should be added into note) |
||
| 1513 | $actioncomm->email_msgid = $msgid; |
||
| 1514 | $actioncomm->email_from = $fromstring; |
||
| 1515 | $actioncomm->email_sender= $sender; |
||
| 1516 | $actioncomm->email_to = $to; |
||
| 1517 | $actioncomm->email_tocc = $sendtocc; |
||
| 1518 | $actioncomm->email_tobcc = $sendtobcc; |
||
| 1519 | $actioncomm->email_subject = $subject; |
||
| 1520 | $actioncomm->errors_to = ''; |
||
| 1521 | |||
| 1522 | if (! in_array($fk_element_type, array('societe','contact','project','user'))) |
||
| 1523 | { |
||
| 1524 | $actioncomm->fk_element = $fk_element_id; |
||
| 1525 | $actioncomm->elementtype = $fk_element_type; |
||
| 1526 | } |
||
| 1527 | |||
| 1528 | //$actioncomm->extraparams = $extraparams; |
||
| 1529 | |||
| 1530 | // Overwrite values with values extracted from source email |
||
| 1531 | $errorforthisaction = $this->overwritePropertiesOfObject($actioncomm, $operation['actionparam'], $messagetext, $subject, $header); |
||
| 1532 | |||
| 1533 | if ($errorforthisaction) |
||
| 1534 | { |
||
| 1535 | $errorforactions++; |
||
| 1536 | } |
||
| 1537 | else |
||
| 1538 | { |
||
| 1539 | $result = $actioncomm->create($user); |
||
| 1540 | if ($result <= 0) |
||
| 1541 | { |
||
| 1542 | $errorforactions++; |
||
| 1543 | $this->errors = $actioncomm->errors; |
||
| 1544 | } |
||
| 1545 | } |
||
| 1546 | } |
||
| 1547 | // Create event |
||
| 1548 | elseif ($operation['type'] == 'project') |
||
| 1549 | { |
||
| 1550 | $projecttocreate = new Project($this->db); |
||
| 1551 | if ($thirdpartystatic->id > 0) |
||
| 1552 | { |
||
| 1553 | $projecttocreate->socid = $thirdpartystatic->id; |
||
| 1554 | if ($thirdpartyfoundby) $descriptionmeta = dol_concatdesc($descriptionmeta, 'Third party found from '.$thirdpartyfoundby); |
||
| 1555 | } |
||
| 1556 | if ($contactstatic->id > 0) |
||
| 1557 | { |
||
| 1558 | $projecttocreate->contact_id = $contactstatic->id; |
||
| 1559 | if ($contactfoundby) $descriptionmeta = dol_concatdesc($descriptionmeta, 'Contact/address found from '.$contactfoundby); |
||
| 1560 | } |
||
| 1561 | |||
| 1562 | $description = $descriptiontitle; |
||
| 1563 | $description = dol_concatdesc($description, "-----"); |
||
| 1564 | $description = dol_concatdesc($description, $descriptionmeta); |
||
| 1565 | $description = dol_concatdesc($description, "-----"); |
||
| 1566 | $description = dol_concatdesc($description, $messagetext); |
||
| 1567 | |||
| 1568 | $descriptionfull = $description; |
||
| 1569 | $descriptionfull = dol_concatdesc($descriptionfull, "----- Header"); |
||
| 1570 | $descriptionfull = dol_concatdesc($descriptionfull, $header); |
||
| 1571 | |||
| 1572 | $id_opp_status = dol_getIdFromCode($this->db, 'PROSP', 'c_lead_status', 'code', 'rowid'); |
||
| 1573 | $percent_opp_status = dol_getIdFromCode($this->db, 'PROSP', 'c_lead_status', 'code', 'percent'); |
||
| 1574 | |||
| 1575 | $projecttocreate->title = $subject; |
||
| 1576 | $projecttocreate->date_start = $date; |
||
| 1577 | $projecttocreate->date_end = ''; |
||
| 1578 | $projecttocreate->opp_status = $id_opp_status; |
||
| 1579 | $projecttocreate->opp_percent = $percent_opp_status; |
||
| 1580 | $projecttocreate->description = dol_concatdesc(dolGetFirstLineOfText(dol_string_nohtmltag($description, 2), 10), '...'.$langs->transnoentities("SeePrivateNote").'...'); |
||
| 1581 | $projecttocreate->note_private = $descriptionfull; |
||
| 1582 | $projecttocreate->entity = $conf->entity; |
||
| 1583 | |||
| 1584 | // Overwrite values with values extracted from source email. |
||
| 1585 | // This may overwrite any $projecttocreate->xxx properties. |
||
| 1586 | $savesocid = $projecttocreate->socid; |
||
| 1587 | $errorforthisaction = $this->overwritePropertiesOfObject($projecttocreate, $operation['actionparam'], $messagetext, $subject, $header); |
||
| 1588 | |||
| 1589 | // Set project ref if not yet defined |
||
| 1590 | if (empty($projecttocreate->ref)) |
||
| 1591 | { |
||
| 1592 | // Get next project Ref |
||
| 1593 | $defaultref=''; |
||
| 1594 | $modele = empty($conf->global->PROJECT_ADDON)?'mod_project_simple':$conf->global->PROJECT_ADDON; |
||
| 1595 | |||
| 1596 | // Search template files |
||
| 1597 | $file=''; $classname=''; $filefound=0; $reldir=''; |
||
| 1598 | $dirmodels=array_merge(array('/'), (array) $conf->modules_parts['models']); |
||
| 1599 | foreach($dirmodels as $reldir) |
||
| 1600 | { |
||
| 1601 | $file=dol_buildpath($reldir."core/modules/project/".$modele.'.php', 0); |
||
| 1602 | if (file_exists($file)) |
||
| 1603 | { |
||
| 1604 | $filefound=1; |
||
| 1605 | $classname = $modele; |
||
| 1606 | break; |
||
| 1607 | } |
||
| 1608 | } |
||
| 1609 | |||
| 1610 | if ($filefound) |
||
| 1611 | { |
||
| 1612 | $result=dol_include_once($reldir."core/modules/project/".$modele.'.php'); |
||
| 1613 | $modProject = new $classname; |
||
| 1614 | |||
| 1615 | if ($savesocid > 0) |
||
| 1616 | { |
||
| 1617 | if ($savesocid != $projecttocreate->socid) |
||
| 1618 | { |
||
| 1619 | $errorforactions++; |
||
| 1620 | setEventMessages('You loaded a thirdparty (id='.$savesocid.') and you force another thirdparty id (id='.$projecttocreate->socid.') by setting socid in operation with a different value', null, 'errors'); |
||
| 1621 | } |
||
| 1622 | } |
||
| 1623 | else { |
||
| 1624 | if ($projecttocreate->socid > 0) |
||
| 1625 | { |
||
| 1626 | $thirdpartystatic->fetch($projecttocreate->socid); |
||
| 1627 | } |
||
| 1628 | } |
||
| 1629 | |||
| 1630 | $defaultref = $modProject->getNextValue(($thirdpartystatic->id > 0 ? $thirdpartystatic : null), $projecttocreate); |
||
| 1631 | } |
||
| 1632 | $projecttocreate->ref = $defaultref; |
||
| 1633 | } |
||
| 1634 | |||
| 1635 | if ($errorforthisaction) |
||
| 1636 | { |
||
| 1637 | $errorforactions++; |
||
| 1638 | } |
||
| 1639 | else |
||
| 1640 | { |
||
| 1641 | if (empty($projecttocreate->ref) || (is_numeric($projecttocreate->ref) && $projecttocreate->ref <= 0)) |
||
| 1642 | { |
||
| 1643 | $errorforactions++; |
||
| 1644 | $this->error = 'Failed to create project: Can\'t get a valid value for the field ref with numbering template = '.$modele.', thirdparty id = '.$thirdpartystatic->id; |
||
|
|
|||
| 1645 | } |
||
| 1646 | else |
||
| 1647 | { |
||
| 1648 | // Create project |
||
| 1649 | $result = $projecttocreate->create($user); |
||
| 1650 | if ($result <= 0) |
||
| 1651 | { |
||
| 1652 | $errorforactions++; |
||
| 1653 | $this->error = 'Failed to create project: '.$langs->trans($projecttocreate->error); |
||
| 1654 | $this->errors = $projecttocreate->errors; |
||
| 1655 | } |
||
| 1656 | } |
||
| 1657 | } |
||
| 1658 | } |
||
| 1659 | // Create event |
||
| 1660 | elseif ($operation['type'] == 'ticket') |
||
| 1661 | { |
||
| 1662 | $tickettocreate = new Ticket($this->db); |
||
| 1663 | if ($thirdpartystatic->id > 0) |
||
| 1664 | { |
||
| 1665 | $tickettocreate->socid = $thirdpartystatic->id; |
||
| 1666 | if ($thirdpartyfoundby) $descriptionmeta = dol_concatdesc($descriptionmeta, 'Third party found from '.$thirdpartyfoundby); |
||
| 1667 | } |
||
| 1668 | if ($contactstatic->id > 0) |
||
| 1669 | { |
||
| 1670 | $tickettocreate->contact_id = $contactstatic->id; |
||
| 1671 | if ($contactfoundby) $descriptionmeta = dol_concatdesc($descriptionmeta, 'Contact/address found from '.$contactfoundby); |
||
| 1672 | } |
||
| 1673 | |||
| 1674 | $description = $descriptiontitle; |
||
| 1675 | $description = dol_concatdesc($description, "-----"); |
||
| 1676 | $description = dol_concatdesc($description, $descriptionmeta); |
||
| 1677 | $description = dol_concatdesc($description, "-----"); |
||
| 1678 | $description = dol_concatdesc($description, $messagetext); |
||
| 1679 | |||
| 1680 | $descriptionfull = $description; |
||
| 1681 | $descriptionfull = dol_concatdesc($descriptionfull, "----- Header"); |
||
| 1682 | $descriptionfull = dol_concatdesc($descriptionfull, $header); |
||
| 1683 | |||
| 1684 | $tickettocreate->subject = $subject; |
||
| 1685 | $tickettocreate->message = $description; |
||
| 1686 | $tickettocreate->type_code = 0; |
||
| 1687 | $tickettocreate->category_code = 0; |
||
| 1688 | $tickettocreate->severity_code = 0; |
||
| 1689 | $tickettocreate->origin_email = $from; |
||
| 1690 | $tickettocreate->fk_user_create = $user->id; |
||
| 1691 | $tickettocreate->datec = $date; |
||
| 1692 | $tickettocreate->fk_project = $projectstatic->id; |
||
| 1693 | $tickettocreate->fk_soc = $thirdpartystatic->id; |
||
| 1694 | $tickettocreate->notify_tiers_at_create = 0; |
||
| 1695 | $tickettocreate->note_private = $descriptionfull; |
||
| 1696 | $tickettocreate->entity = $conf->entity; |
||
| 1697 | //$tickettocreate->fk_contact = $contactstatic->id; |
||
| 1698 | |||
| 1699 | // Overwrite values with values extracted from source email. |
||
| 1700 | // This may overwrite any $projecttocreate->xxx properties. |
||
| 1701 | $savesocid = $tickettocreate->socid; |
||
| 1702 | $errorforthisaction = $this->overwritePropertiesOfObject($tickettocreate, $operation['actionparam'], $messagetext, $subject, $header); |
||
| 1703 | |||
| 1704 | // Set ticket ref if not yet defined |
||
| 1705 | if (empty($tickettocreate->ref)) |
||
| 1706 | { |
||
| 1707 | // Get next project Ref |
||
| 1708 | $defaultref=''; |
||
| 1709 | $modele = empty($conf->global->TICKET_ADDON)?'mod_ticket_simple':$conf->global->TICKET_ADDON; |
||
| 1710 | |||
| 1711 | // Search template files |
||
| 1712 | $file=''; $classname=''; $filefound=0; $reldir=''; |
||
| 1713 | $dirmodels=array_merge(array('/'), (array) $conf->modules_parts['models']); |
||
| 1714 | foreach($dirmodels as $reldir) |
||
| 1715 | { |
||
| 1716 | $file=dol_buildpath($reldir."core/modules/ticket/".$modele.'.php', 0); |
||
| 1717 | if (file_exists($file)) |
||
| 1718 | { |
||
| 1719 | $filefound=1; |
||
| 1720 | $classname = $modele; |
||
| 1721 | break; |
||
| 1722 | } |
||
| 1723 | } |
||
| 1724 | |||
| 1725 | if ($filefound) |
||
| 1726 | { |
||
| 1727 | $result=dol_include_once($reldir."core/modules/ticket/".$modele.'.php'); |
||
| 1728 | $modTicket = new $classname; |
||
| 1729 | |||
| 1730 | if ($savesocid > 0) |
||
| 1731 | { |
||
| 1732 | if ($savesocid != $tickettocreate->socid) |
||
| 1733 | { |
||
| 1734 | $errorforactions++; |
||
| 1735 | setEventMessages('You loaded a thirdparty (id='.$savesocid.') and you force another thirdparty id (id='.$tickettocreate->socid.') by setting socid in operation with a different value', null, 'errors'); |
||
| 1736 | } |
||
| 1737 | } |
||
| 1738 | else { |
||
| 1739 | if ($tickettocreate->socid > 0) |
||
| 1740 | { |
||
| 1741 | $thirdpartystatic->fetch($tickettocreate->socid); |
||
| 1742 | } |
||
| 1743 | } |
||
| 1744 | |||
| 1745 | $defaultref = $modTicket->getNextValue(($thirdpartystatic->id > 0 ? $thirdpartystatic : null), $tickettocreate); |
||
| 1746 | } |
||
| 1747 | $tickettocreate->ref = $defaultref; |
||
| 1748 | } |
||
| 1749 | |||
| 1750 | if ($errorforthisaction) |
||
| 1751 | { |
||
| 1752 | $errorforactions++; |
||
| 1753 | } |
||
| 1754 | else |
||
| 1755 | { |
||
| 1756 | if (is_numeric($tickettocreate->ref) && $tickettocreate->ref <= 0) |
||
| 1757 | { |
||
| 1758 | $errorforactions++; |
||
| 1759 | $this->error = 'Failed to create ticket: Can\'t get a valid value for the field ref with numbering template = '.$modele.', thirdparty id = '.$thirdpartystatic->id; |
||
| 1760 | } |
||
| 1761 | else |
||
| 1762 | { |
||
| 1763 | // Create project |
||
| 1764 | $result = $tickettocreate->create($user); |
||
| 1765 | if ($result <= 0) |
||
| 1766 | { |
||
| 1767 | $errorforactions++; |
||
| 1768 | $this->error = 'Failed to create ticket: '.$langs->trans($tickettocreate->error); |
||
| 1769 | $this->errors = $tickettocreate->errors; |
||
| 1770 | } |
||
| 1771 | } |
||
| 1772 | } |
||
| 1773 | } |
||
| 1774 | |||
| 1775 | if (! $errorforactions) |
||
| 1776 | { |
||
| 1777 | $nbactiondoneforemail++; |
||
| 1778 | } |
||
| 1779 | } |
||
| 1780 | |||
| 1781 | // Error for email or not ? |
||
| 1782 | if (! $errorforactions) |
||
| 1783 | { |
||
| 1784 | if ($targetdir) |
||
| 1785 | { |
||
| 1786 | dol_syslog("EmailCollector::doCollectOneCollector move message ".$imapemail." to ".$connectstringtarget, LOG_DEBUG); |
||
| 1787 | $res = imap_mail_move($connection, $imapemail, $targetdir, 0); |
||
| 1788 | if ($res == false) { |
||
| 1789 | $errorforemail++; |
||
| 1790 | $this->error = imap_last_error(); |
||
| 1791 | $this->errors[] = $this->error; |
||
| 1792 | dol_syslog(imap_last_error()); |
||
| 1793 | } |
||
| 1794 | } |
||
| 1795 | else |
||
| 1796 | { |
||
| 1797 | dol_syslog("EmailCollector::doCollectOneCollector message ".$imapemail." to ".$connectstringtarget." was set to read", LOG_DEBUG); |
||
| 1798 | } |
||
| 1799 | } |
||
| 1800 | else |
||
| 1801 | { |
||
| 1802 | $errorforemail++; |
||
| 1803 | } |
||
| 1804 | |||
| 1805 | unset($objectemail); |
||
| 1806 | unset($projectstatic); |
||
| 1807 | unset($thirdpartystatic); |
||
| 1808 | unset($contactstatic); |
||
| 1809 | |||
| 1810 | $nbemailprocessed++; |
||
| 1811 | |||
| 1812 | if (! $errorforemail) |
||
| 1813 | { |
||
| 1814 | $nbactiondone += $nbactiondoneforemail; |
||
| 1815 | $nbemailok++; |
||
| 1816 | |||
| 1817 | $this->db->commit(); |
||
| 1818 | |||
| 1819 | // Stop the loop to process email if we reach maximum collected per collect |
||
| 1820 | if ($this->maxemailpercollect > 0 && $nbemailok >= $this->maxemailpercollect) |
||
| 1821 | { |
||
| 1822 | dol_syslog("EmailCollect::doCollectOneCollector We reach maximum of ".$nbemailok." collected with success, so we stop this collector now."); |
||
| 1823 | break; |
||
| 1824 | } |
||
| 1825 | } |
||
| 1826 | else |
||
| 1827 | { |
||
| 1828 | $error++; |
||
| 1829 | |||
| 1830 | $this->db->rollback(); |
||
| 1831 | } |
||
| 1832 | } |
||
| 1833 | |||
| 1834 | $output=$langs->trans('XEmailsDoneYActionsDone', $nbemailprocessed, $nbemailok, $nbactiondone); |
||
| 1835 | |||
| 1836 | dol_syslog("End of loop on emails", LOG_INFO, -1); |
||
| 1837 | } |
||
| 1838 | else |
||
| 1839 | { |
||
| 1840 | $output=$langs->trans('NoNewEmailToProcess'); |
||
| 1841 | } |
||
| 1842 | |||
| 1843 | imap_expunge($connection); // To validate any move |
||
| 1844 | |||
| 1845 | imap_close($connection); |
||
| 1846 | |||
| 1847 | $this->datelastresult = $now; |
||
| 1848 | $this->lastresult = $output; |
||
| 1849 | $this->debuginfo = 'IMAP search string used : '.$search; |
||
| 1850 | if ($searchhead) $this->debuginfo .= '<br>Then search string into email header : '.$searchhead; |
||
| 1851 | |||
| 1852 | if (! $error) $this->datelastok = $now; |
||
| 1853 | |||
| 1854 | if (! empty($this->errors)) $this->lastresult.= " - ".join(" - ", $this->errors); |
||
| 1855 | $this->codelastresult = ($error ? 'KO' : 'OK'); |
||
| 1856 | $this->update($user); |
||
| 1857 | |||
| 1858 | dol_syslog("EmailCollector::doCollectOneCollector end", LOG_DEBUG); |
||
| 1859 | |||
| 1860 | return $error?-1:1; |
||
| 1861 | } |
||
| 1995 |