| Conditions | 95 |
| Paths | > 20000 |
| Total Lines | 497 |
| 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 |
||
| 783 | public function doCollectOneCollector() |
||
| 784 | { |
||
| 785 | global $conf, $langs, $user; |
||
| 786 | |||
| 787 | //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log'; |
||
| 788 | |||
| 789 | require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php'; |
||
| 790 | |||
| 791 | dol_syslog("EmailCollector::doCollectOneCollector start", LOG_DEBUG); |
||
| 792 | |||
| 793 | $langs->loadLangs(array("project", "companies", "errors")); |
||
| 794 | |||
| 795 | $error = 0; |
||
| 796 | $this->output = ''; |
||
| 797 | $this->error=''; |
||
| 798 | |||
| 799 | $now = dol_now(); |
||
| 800 | |||
| 801 | if (empty($this->host)) |
||
| 802 | { |
||
| 803 | $this->error=$langs->trans('ErrorFieldRequired', 'EMailHost'); |
||
| 804 | return -1; |
||
| 805 | } |
||
| 806 | if (empty($this->user)) |
||
| 807 | { |
||
| 808 | $this->error=$langs->trans('ErrorFieldRequired', 'Login'); |
||
| 809 | return -1; |
||
| 810 | } |
||
| 811 | if (empty($this->source_directory)) |
||
| 812 | { |
||
| 813 | $this->error=$langs->trans('ErrorFieldRequired', 'MailboxSourceDirectory'); |
||
| 814 | return -1; |
||
| 815 | } |
||
| 816 | if (! function_exists('imap_open')) |
||
| 817 | { |
||
| 818 | $this->error='IMAP function not enabled on your PHP'; |
||
| 819 | return -2; |
||
| 820 | } |
||
| 821 | |||
| 822 | $this->fetchFilters(); |
||
| 823 | $this->fetchActions(); |
||
| 824 | |||
| 825 | $sourcedir = $this->source_directory; |
||
| 826 | $targetdir = ($this->target_directory ? $this->target_directory : ''); // Can be '[Gmail]/Trash' or 'mytag' |
||
| 827 | |||
| 828 | $connectstringserver = $this->getConnectStringIMAP(); |
||
| 829 | $connectstringsource = $connectstringserver.imap_utf7_encode($sourcedir); |
||
| 830 | $connectstringtarget = $connectstringserver.imap_utf7_encode($targetdir); |
||
| 831 | |||
| 832 | $connection = imap_open($connectstringsource, $this->user, $this->password); |
||
| 833 | if (! $connection) |
||
| 834 | { |
||
| 835 | $this->error = 'Failed to open IMAP connection '.$connectstringsource; |
||
| 836 | return -3; |
||
| 837 | } |
||
| 838 | |||
| 839 | //$search='ALL'; |
||
| 840 | $search='UNDELETED'; |
||
| 841 | foreach($this->filters as $rule) |
||
| 842 | { |
||
| 843 | if (empty($rule['status'])) continue; |
||
| 844 | |||
| 845 | if ($rule['type'] == 'to') $search.=($search?' ':'').'TO "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 846 | if ($rule['type'] == 'bcc') $search.=($search?' ':'').'BCC'; |
||
| 847 | if ($rule['type'] == 'cc') $search.=($search?' ':'').'CC'; |
||
| 848 | if ($rule['type'] == 'from') $search.=($search?' ':'').'FROM "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 849 | if ($rule['type'] == 'subject') $search.=($search?' ':'').'SUBJECT "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 850 | if ($rule['type'] == 'body') $search.=($search?' ':'').'BODY "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 851 | if ($rule['type'] == 'seen') $search.=($search?' ':'').'SEEN'; |
||
| 852 | if ($rule['type'] == 'unseen') $search.=($search?' ':'').'UNSEEN'; |
||
| 853 | } |
||
| 854 | |||
| 855 | if (empty($targetdir)) // Use last date as filter if there is no targetdir defined. |
||
| 856 | { |
||
| 857 | $fromdate=0; |
||
| 858 | if ($this->datelastresult && $this->codelastresult == 'OK') $fromdate = $this->datelastresult; |
||
| 859 | if ($fromdate > 0) $search.=($search?' ':'').'SINCE '.dol_print_date($fromdate - 1,'dayhourrfc'); |
||
| 860 | } |
||
| 861 | dol_syslog("IMAP search string = ".$search); |
||
| 862 | //var_dump($search); |
||
| 863 | |||
| 864 | $nbemailprocessed=0; |
||
| 865 | $nbemailok=0; |
||
| 866 | $nbactiondone=0; |
||
| 867 | |||
| 868 | // Scan IMAP inbox |
||
| 869 | $arrayofemail= imap_search($connection, $search); |
||
| 870 | //var_dump($arrayofemail);exit; |
||
| 871 | |||
| 872 | // Loop on each email found |
||
| 873 | if (! empty($arrayofemail) && count($arrayofemail) > 0) |
||
| 874 | { |
||
| 875 | foreach($arrayofemail as $imapemail) |
||
| 876 | { |
||
| 877 | if ($nbemailprocessed > 100) break; // Do not process more than 100 email per launch |
||
| 878 | |||
| 879 | $thirdpartystatic=new Societe($this->db); |
||
| 880 | $contactstatic=new Contact($this->db); |
||
| 881 | $projectstatic=new Project($this->db); |
||
| 882 | |||
| 883 | $nbactiondoneforemail = 0; |
||
| 884 | $errorforemail = 0; |
||
| 885 | $errorforactions = 0; |
||
| 886 | $thirdpartyfoundby = ''; |
||
| 887 | $contactfoundby = ''; |
||
| 888 | $projectfoundby = ''; |
||
| 889 | |||
| 890 | $this->db->begin(); |
||
| 891 | |||
| 892 | $overview = imap_fetch_overview($connection, $imapemail, 0); |
||
| 893 | $header = imap_fetchheader($connection, $imapemail, 0); |
||
| 894 | //$message = imap_body($connection, $imapemail, 0); |
||
| 895 | $structure = imap_fetchstructure($connection, $imapemail, 0); |
||
| 896 | $partplain = $parthtml = -1; |
||
| 897 | // Loop to get part html and plain |
||
| 898 | foreach($structure->parts as $key => $part) |
||
| 899 | { |
||
| 900 | if ($part->subtype == 'HTML') $parthtml=$key; |
||
| 901 | if ($part->subtype == 'PLAIN') $partplain=$key; |
||
| 902 | } |
||
| 903 | |||
| 904 | $matches=array(); |
||
| 905 | preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $header, $matches); |
||
| 906 | $headers = array_combine($matches[1], $matches[2]); |
||
| 907 | //var_dump($headers); |
||
| 908 | |||
| 909 | $messagetext = imap_fetchbody($connection, $imapemail, ($parthtml >= 0 ? $parthtml : ($partplain >= 0 ? $partplain : 0))); |
||
| 910 | |||
| 911 | //var_dump($overview); |
||
| 912 | //var_dump($header); |
||
| 913 | //var_dump($message); |
||
| 914 | //var_dump($messagetext); |
||
| 915 | $fromstring=$overview[0]->from; |
||
| 916 | $sender=$overview[0]->sender; |
||
| 917 | $to=$overview[0]->to; |
||
| 918 | $sendtocc=$overview[0]->cc; |
||
| 919 | $sendtobcc=$overview[0]->bcc; |
||
| 920 | $date=$overview[0]->udate; |
||
| 921 | $msgid=str_replace(array('<','>'), '', $overview[0]->message_id); |
||
| 922 | $subject=$overview[0]->subject; |
||
| 923 | //var_dump($msgid);exit; |
||
| 924 | |||
| 925 | $reg=array(); |
||
| 926 | if (preg_match('/^(.*)<(.*)>$/', $fromstring, $reg)) |
||
| 927 | { |
||
| 928 | $from=$reg[1]; |
||
| 929 | $fromtext=$reg[0]; |
||
| 930 | } |
||
| 931 | else |
||
| 932 | { |
||
| 933 | $from = $fromstring; |
||
| 934 | $fromtext=''; |
||
| 935 | } |
||
| 936 | $fk_element_id = 0; $fk_element_type = ''; |
||
| 937 | |||
| 938 | $contactid = 0; $thirdpartyid = 0; $projectid = 0; |
||
| 939 | |||
| 940 | // Analyze TrackId |
||
| 941 | $trackid = ''; |
||
| 942 | $reg=array(); |
||
| 943 | if (! empty($headers['X-Dolibarr-TrackId']) && preg_match('/:\s*([a-z]+)([0-9]+)$/', $headers['X-Dolibarr-TrackId'], $reg)) |
||
| 944 | { |
||
| 945 | $trackid = $reg[0].$reg[1]; |
||
| 946 | |||
| 947 | $objectid = 0; |
||
| 948 | $objectemail = null; |
||
| 949 | if ($reg[0] == 'inv') |
||
| 950 | { |
||
| 951 | $objectid = $reg[1]; |
||
| 952 | $objectemail = new Facture($this->db); |
||
| 953 | } |
||
| 954 | if ($reg[0] == 'proj') |
||
| 955 | { |
||
| 956 | $objectid = $reg[1]; |
||
| 957 | $objectemail = new Project($this->db); |
||
| 958 | } |
||
| 959 | if ($reg[0] == 'con') |
||
| 960 | { |
||
| 961 | $objectid = $reg[1]; |
||
| 962 | $objectemail = new Contact($this->db); |
||
| 963 | } |
||
| 964 | if ($reg[0] == 'thi') |
||
| 965 | { |
||
| 966 | $objectid = $reg[1]; |
||
| 967 | $objectemail = new Societe($this->db); |
||
| 968 | } |
||
| 969 | if ($reg[0] == 'use') |
||
| 970 | { |
||
| 971 | $objectid = $reg[1]; |
||
| 972 | $objectemail = new User($this->db); |
||
| 973 | } |
||
| 974 | |||
| 975 | $result = $objectemail->fetch($objectid); |
||
| 976 | if ($result > 0) |
||
| 977 | { |
||
| 978 | $fk_element_id = $objectemail->id; |
||
| 979 | $fk_element_type = $objectemail->element; |
||
| 980 | // Fix fk_element_type |
||
| 981 | if ($fk_element_type == 'facture') $fk_element_type = 'invoice'; |
||
| 982 | |||
| 983 | $thirdpartyid = $objectemail->fk_soc; |
||
| 984 | $contactid = $objectemail->fk_socpeople; |
||
| 985 | $projectid = isset($objectemail->fk_project)?$objectemail->fk_project:$objectemail->fk_projet; |
||
| 986 | } |
||
| 987 | |||
| 988 | // Project |
||
| 989 | if ($projectid > 0) |
||
| 990 | { |
||
| 991 | $result = $projectstatic->fetch($projectid); |
||
| 992 | if ($result <= 0) $projectstatic->id = 0; |
||
| 993 | else |
||
| 994 | { |
||
| 995 | $projectid = $projectstatic->id; |
||
| 996 | $projectfoundby = 'trackid ('.$trackid.')'; |
||
| 997 | if (empty($contactid)) $contactid = $projectstatic->fk_contact; |
||
| 998 | if (empty($thirdpartyid)) $thirdpartyid = $projectstatic->fk_soc; |
||
| 999 | } |
||
| 1000 | } |
||
| 1001 | // Contact |
||
| 1002 | if ($contactid > 0) |
||
| 1003 | { |
||
| 1004 | $result = $contactstatic->fetch($contactid); |
||
| 1005 | if ($result <= 0) $contactstatic->id = 0; |
||
| 1006 | else |
||
| 1007 | { |
||
| 1008 | $contactid = $contactstatic->id; |
||
| 1009 | $contactfoundby = 'trackid ('.$trackid.')'; |
||
| 1010 | if (empty($thirdpartyid)) $thirdpartyid = $contactstatic->fk_soc; |
||
| 1011 | } |
||
| 1012 | } |
||
| 1013 | // Thirdparty |
||
| 1014 | if ($thirdpartyid > 0) |
||
| 1015 | { |
||
| 1016 | $result = $thirdpartystatic->fetch($thirdpartyid); |
||
| 1017 | if ($result <= 0) $thirdpartystatic->id = 0; |
||
| 1018 | else |
||
| 1019 | { |
||
| 1020 | $thirdpartyid = $thirdpartystatic->id; |
||
| 1021 | $thirdpartyfoundby = 'trackid ('.$trackid.')'; |
||
| 1022 | } |
||
| 1023 | } |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | if (empty($contactid)) // Try to find contact using email |
||
| 1027 | { |
||
| 1028 | $result = $contactstatic->fetch(0, null, '', $from); |
||
| 1029 | if ($result > 0) |
||
| 1030 | { |
||
| 1031 | $contactid = $contactstatic->id; |
||
| 1032 | $contactfoundby = 'email of contact ('.$from.')'; |
||
| 1033 | if ($contactstatic->fk_soc > 0) |
||
| 1034 | { |
||
| 1035 | $result = $thirdpartystatic->fetch($contactstatic->fk_soc); |
||
| 1036 | if ($result > 0) |
||
| 1037 | { |
||
| 1038 | $thirdpartyid = $thirdpartystatic->id; |
||
| 1039 | $thirdpartyfoundby = 'email of contact ('.$from.')'; |
||
| 1040 | } |
||
| 1041 | } |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | } |
||
| 1045 | |||
| 1046 | if (empty($thirdpartyid)) // Try to find thirdparty using email |
||
| 1047 | { |
||
| 1048 | $result = $thirdpartystatic->fetch(0, '', '', '', '', '', '', '', '', '', $from); |
||
| 1049 | if ($result > 0) $thirdpartyfoundby = 'email ('.$from.')'; |
||
| 1050 | } |
||
| 1051 | |||
| 1052 | |||
| 1053 | |||
| 1054 | // Do operations |
||
| 1055 | foreach($this->actions as $operation) |
||
| 1056 | { |
||
| 1057 | if ($errorforactions) break; |
||
| 1058 | if (empty($operation['status'])) continue; |
||
| 1059 | |||
| 1060 | // Make Operation |
||
| 1061 | |||
| 1062 | // Search and create thirdparty |
||
| 1063 | if ($operation['type'] == 'searchandcreatethirdparty') |
||
| 1064 | { |
||
| 1065 | |||
| 1066 | |||
| 1067 | |||
| 1068 | |||
| 1069 | } |
||
| 1070 | // Create event |
||
| 1071 | elseif ($operation['type'] == 'recordevent') |
||
| 1072 | { |
||
| 1073 | $actioncode = 'EMAIL_IN'; |
||
| 1074 | |||
| 1075 | // Insert record of emails sent |
||
| 1076 | $actioncomm = new ActionComm($this->db); |
||
| 1077 | |||
| 1078 | $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...) |
||
| 1079 | $actioncomm->code = 'AC_'.$actioncode; |
||
| 1080 | $actioncomm->label = $langs->trans("EmailReceived").' - '.$langs->trans("From").' '.$from; |
||
| 1081 | $actioncomm->note = $messagetext; |
||
| 1082 | $actioncomm->fk_project = $projectstatic->id; |
||
| 1083 | $actioncomm->datep = $date; |
||
| 1084 | $actioncomm->datef = $date; |
||
| 1085 | $actioncomm->percentage = -1; // Not applicable |
||
| 1086 | $actioncomm->socid = $thirdpartystatic->id; |
||
| 1087 | $actioncomm->contactid = $contactstatic->id; |
||
| 1088 | $actioncomm->authorid = $user->id; // User saving action |
||
| 1089 | $actioncomm->userownerid = $user->id; // Owner of action |
||
| 1090 | // Fields when action is an email (content should be added into note) |
||
| 1091 | $actioncomm->email_msgid = $msgid; |
||
| 1092 | $actioncomm->email_from = $fromstring; |
||
| 1093 | $actioncomm->email_sender= $sender; |
||
| 1094 | $actioncomm->email_to = $to; |
||
| 1095 | $actioncomm->email_tocc = $sendtocc; |
||
| 1096 | $actioncomm->email_tobcc = $sendtobcc; |
||
| 1097 | $actioncomm->email_subject = $subject; |
||
| 1098 | $actioncomm->errors_to = ''; |
||
| 1099 | |||
| 1100 | $actioncomm->fk_element = $fk_element_id; |
||
| 1101 | $actioncomm->elementtype = $fk_element_type; |
||
| 1102 | |||
| 1103 | //$actioncomm->extraparams = $extraparams; |
||
| 1104 | |||
| 1105 | |||
| 1106 | // Overwrite values with values extracted from source email |
||
| 1107 | $errorforthisaction = $this->overwritePropertiesOfObject($actioncommn, $operation['actionparam'], $messagetext, $subject); |
||
| 1108 | |||
| 1109 | if ($errorforthisaction) |
||
| 1110 | { |
||
| 1111 | $errorforactions++; |
||
| 1112 | } |
||
| 1113 | else |
||
| 1114 | { |
||
| 1115 | $result = $actioncomm->create($user); |
||
| 1116 | if ($result <= 0) |
||
| 1117 | { |
||
| 1118 | $errorforactions++; |
||
| 1119 | $this->errors = $actioncomm->errors; |
||
| 1120 | } |
||
| 1121 | } |
||
| 1122 | } |
||
| 1123 | // Create event |
||
| 1124 | elseif ($operation['type'] == 'project') |
||
| 1125 | { |
||
| 1126 | $note_private = $langs->trans("ProjectCreatedByEmailCollector", $msgid); |
||
| 1127 | $projecttocreate = new Project($this->db); |
||
| 1128 | if ($thirdpartystatic->id > 0) |
||
| 1129 | { |
||
| 1130 | $projecttocreate->fk_soc = $thirdpartystatic->id; |
||
| 1131 | if ($thirdpartyfoundby) $note_private .= ' - Third party found from '.$thirdpartyfoundby; |
||
| 1132 | } |
||
| 1133 | if ($contactstatic->id > 0) |
||
| 1134 | { |
||
| 1135 | $projecttocreate->contact_id = $contactstatic->id; |
||
| 1136 | if ($contactfoundby) $note_private .= ' - Contact/address found from '.$contactfoundby; |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | $id_opp_status = dol_getIdFromCode($this->db, 'PROSP', 'c_lead_status', 'code', 'rowid'); |
||
| 1140 | $percent_opp_status = dol_getIdFromCode($this->db, 'PROSP', 'c_lead_status', 'code', 'percent'); |
||
| 1141 | |||
| 1142 | $projecttocreate->title = $subject; |
||
| 1143 | $projecttocreate->date_start = $now; |
||
| 1144 | $projecttocreate->opp_status = $id_opp_status; |
||
| 1145 | $projecttocreate->opp_percent = $percent_opp_status; |
||
| 1146 | $projecttocreate->description = ($note_private?$note_private."\n":'').$messagetext; |
||
| 1147 | $projecttocreate->note_private = $note_private; |
||
| 1148 | $projecttocreate->entity = $conf->entity; |
||
| 1149 | |||
| 1150 | // Get next project Ref |
||
| 1151 | $defaultref=''; |
||
| 1152 | $modele = empty($conf->global->PROJECT_ADDON)?'mod_project_simple':$conf->global->PROJECT_ADDON; |
||
| 1153 | |||
| 1154 | // Search template files |
||
| 1155 | $file=''; $classname=''; $filefound=0; |
||
| 1156 | $dirmodels=array_merge(array('/'),(array) $conf->modules_parts['models']); |
||
| 1157 | foreach($dirmodels as $reldir) |
||
| 1158 | { |
||
| 1159 | $file=dol_buildpath($reldir."core/modules/project/".$modele.'.php',0); |
||
| 1160 | if (file_exists($file)) |
||
| 1161 | { |
||
| 1162 | $filefound=1; |
||
| 1163 | $classname = $modele; |
||
| 1164 | break; |
||
| 1165 | } |
||
| 1166 | } |
||
| 1167 | |||
| 1168 | if ($filefound) |
||
| 1169 | { |
||
| 1170 | $result=dol_include_once($reldir."core/modules/project/".$modele.'.php'); |
||
| 1171 | $modProject = new $classname; |
||
| 1172 | |||
| 1173 | $defaultref = $modProject->getNextValue(($thirdpartystatic->id > 0 ? $thirdpartystatic : null), $projecttocreate); |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | $projecttocreate->ref = $defaultref; |
||
| 1177 | |||
| 1178 | // Overwrite values with values extracted from source email |
||
| 1179 | $errorforthisaction = $this->overwritePropertiesOfObject($projecttocreate, $operation['actionparam'], $messagetext, $subject); |
||
| 1180 | |||
| 1181 | if ($errorforthisaction) |
||
| 1182 | { |
||
| 1183 | $errorforactions++; |
||
| 1184 | } |
||
| 1185 | else |
||
| 1186 | { |
||
| 1187 | if (is_numeric($projecttocreate->ref) && $projecttocreate->ref <= 0) |
||
| 1188 | { |
||
| 1189 | $errorforactions++; |
||
| 1190 | $this->error = 'Failed to create project: Can\'t get a valid value for project Ref'; |
||
| 1191 | } |
||
| 1192 | else |
||
| 1193 | { |
||
| 1194 | // Create project |
||
| 1195 | $result = $projecttocreate->create($user); |
||
| 1196 | if ($result <= 0) |
||
| 1197 | { |
||
| 1198 | $errorforactions++; |
||
| 1199 | $this->error = 'Failed to create project: '.$langs->trans($projecttocreate->error); |
||
| 1200 | $this->errors = $projecttocreate->errors; |
||
| 1201 | } |
||
| 1202 | } |
||
| 1203 | } |
||
| 1204 | } |
||
| 1205 | |||
| 1206 | if (! $errorforactions) |
||
| 1207 | { |
||
| 1208 | $nbactiondoneforemail++; |
||
| 1209 | } |
||
| 1210 | } |
||
| 1211 | |||
| 1212 | // Error for email or not ? |
||
| 1213 | if (! $errorforactions) |
||
| 1214 | { |
||
| 1215 | if ($targetdir) |
||
| 1216 | { |
||
| 1217 | dol_syslog("EmailCollector::doCollectOneCollector move message ".$imapemail." to ".$connectstringtarget, LOG_DEBUG); |
||
| 1218 | $res = imap_mail_move($connection, $imapemail, $targetdir, 0); |
||
| 1219 | if ($res == false) { |
||
| 1220 | $errorforemail++; |
||
| 1221 | $this->error = imap_last_error(); |
||
| 1222 | $this->errors[] = $this->error; |
||
| 1223 | dol_syslog(imap_last_error()); |
||
| 1224 | } |
||
| 1225 | } |
||
| 1226 | else |
||
| 1227 | { |
||
| 1228 | dol_syslog("EmailCollector::doCollectOneCollector message ".$imapemail." to ".$connectstringtarget." was set to read", LOG_DEBUG); |
||
| 1229 | |||
| 1230 | } |
||
| 1231 | } |
||
| 1232 | else |
||
| 1233 | { |
||
| 1234 | $errorforemail++; |
||
| 1235 | } |
||
| 1236 | |||
| 1237 | if (! $errorforemail) |
||
| 1238 | { |
||
| 1239 | $nbactiondone += $nbactiondoneforemail; |
||
| 1240 | $nbemailok++; |
||
| 1241 | |||
| 1242 | $this->db->commit(); |
||
| 1243 | } |
||
| 1244 | else |
||
| 1245 | { |
||
| 1246 | $error++; |
||
| 1247 | |||
| 1248 | $this->db->rollback(); |
||
| 1249 | } |
||
| 1250 | |||
| 1251 | $nbemailprocessed++; |
||
| 1252 | |||
| 1253 | unset($objectemail); |
||
| 1254 | unset($projectstatic); |
||
| 1255 | unset($thirdpartystatic); |
||
| 1256 | unset($contactstatic); |
||
| 1257 | } |
||
| 1258 | |||
| 1259 | $output=$langs->trans('XEmailsDoneYActionsDone', $nbemailprocessed, $nbemailok, $nbactiondone); |
||
| 1260 | } |
||
| 1261 | else |
||
| 1262 | { |
||
| 1263 | $output=$langs->trans('NoNewEmailToProcess'); |
||
| 1264 | } |
||
| 1265 | |||
| 1266 | imap_expunge($connection); // To validate any move |
||
| 1267 | |||
| 1268 | imap_close($connection); |
||
| 1269 | |||
| 1270 | $this->datelastresult = $now; |
||
| 1271 | $this->lastresult = $output; |
||
| 1272 | if (! empty($this->errors)) $this->lastresult.= " - ".join(" - ", $this->errors); |
||
| 1273 | $this->codelastresult = ($error ? 'KO' : 'OK'); |
||
| 1274 | $this->update($user); |
||
| 1275 | |||
| 1276 | dol_syslog("EmailCollector::doCollectOneCollector end", LOG_DEBUG); |
||
| 1277 | |||
| 1278 | return $error?-1:1; |
||
| 1279 | } |
||
| 1280 | } |
||
| 1281 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: