module_name = new InpostShip();
}
public static function showError($error)
{
if (!empty($error['error'])) {
$error_result = (array)$error['result'];
$error_status = $error_result['status'];
$error_message = $error_result['message'];
$error_detail = (array)$error_result['details'];
return 'ERROR '.$error_status.': '.$error_message.' Details: '.$error_detail;
}
}
public function newErrorListDetailsCalculate($error)
{
$details = array();
$errorApi = $error['result']->details;
// print_r($errorApi);exit();
if (!empty($errorApi)) {
foreach ($errorApi as $key => $item) { // shipments
foreach ($item as $key2 => $item2) { // []
foreach ($item2 as $key3 => $item3) { // SHIPMENT1
foreach ($item3 as $key4 => $item4) { // type, eg. receiver
$name = $this->translateF1($key4);
foreach ($item4 as $key5 => $item5) { // []
foreach ($item5 as $key6 => $item6) { // eg. phone
$name2 = $this->translateF2($key6);
foreach ($item6 as $key7 => $item7) { // []
$name3 = $this->translateF4($item7);
$details[] = '['.$name.'] '.$name2.' '.$name3;
}
}
}
}
}
}
}
}
return $details;
}
public function newErrorListDetails($error)
{
$details = array();
$errorApi = $error['result']->details;
// print_r($errorApi);exit();
if (!empty($errorApi)) {
foreach ($errorApi as $key => $item) { // eg. receiver
$name = $this->translateF1($key);
foreach ($item as $key2 => $item2) { // []
foreach ($item2 as $key3 => $item3) { // eg.phone
$name2 = $this->translateF2($key3);
foreach ($item3 as $key4 => $item4) { // type, eg. phone
$name3 = $this->translateF4($item4);
$details[] = '['.$name.'] '.$name2.' '.$name3;
}
}
}
}
}
return $details;
}
private static function showDetail($detail)
{
$details = array();
foreach ($detail as $key => $d) {
foreach ($d as $dd) {
foreach ($dd as $k1 => $d2) {
$details[] = $key.' '.$k1.' => '.(string)$d2[0];
}
}
}
if (!empty($details)) {
return implode(',', $details);
}
return null;
}
private static function detailInfo($error)
{
switch ($error) {
case 'already_dispatched':
$name = '';
break;
}
return $name;
}
/**
* Show errors API
* @param $error
* @param int $type
* @return string
*/
public function displayErrors($error, $type = 1)
{
$description = '';
if (!empty($error['error'])) {
$error_result = (array)$error['result'];
$error_status = $error_result['status'];
$error_message = $error_result['message'];
$error_detail = (array)$error_result['details'];
$description = $this->module_name->l('ERROR', 'inposterrors').' '.$error_status.' - '.
$this->translateF($error_message).'
';
if (!empty($error_detail)) {
if ($type == 2) {
$details = $this->newErrorListDetailsCalculate($error);
} else {
$details = $this->newErrorListDetails($error);
}
}
if (!empty($details)) {
$description .= $this->module_name->l('Details', 'inposterrors') . ':';
foreach ($details as $d) {
$description .= '
' . $d;
}
}
}
return $description;
}
private function showDetailsError($error)
{
$details = array();
foreach ($error as $key => $d) { // sender, receiver
if (is_array($d)) {
foreach ($d as $dd) { // array list
$dd = (array)$dd;
if (is_array($dd)) {
foreach ($dd as $k1 => $d2) { //
// echo "
";print_r($d2);echo ""; if (is_array($d2)) { if (isset($d2[0]) && $d2[0] == 'required') { $details[] = '[' . $this->translateF1($key) . '] ' . $this->translateF2($k1) . '' . ($d2[0] == 'required' ? ' ' . $this->module_name->l('required', 'inposterrors') : ''); } elseif (isset($d2[0]) && $d2[0] == 'invalid') { $details[] = '[' . $this->translateF1($key) . '] ' . $this->translateF2($k1) . '' . ($d2[0] == 'invalid' ? ' ' . $this->module_name->l('invalid', 'inposterrors') : ''); } elseif (isset($d2[0]) && $d2[0] == 'does_not_exist') { $details[] = '[' . $this->translateF1($key) . '] ' . $this->translateF2($k1) . '' . ($d2[0] == 'does_not_exist' ? ' ' . $this->module_name->l('does_not_exist', 'inposterrors') : ''); } elseif (isset($d2[0]) && $d2[0] == 'already_bought') { $details[] = '[' . $this->translateF1($key) . '] ' . ($d2[0] == 'already_bought' ? ' ' . $this->module_name->l('already_bought', 'inposterrors') : ''); } else { foreach ($d2 as $d3) { foreach ($d3 as $k3 => $d4) { foreach ($d4 as $d5) { $details[] = '[' . $this->translateF1($key) . '] ' . $this->translateF2($k1) . ' => ' . $this->translateF3($k3) . ($d5 == 'required' ? ' ' . $this->module_name->l('required', 'inposterrors') : ($d5 == 'invalid' ? ' ' . $this->module_name->l('invalid', 'inposterrors') : '')); } } } } } else { if (!is_object($d2)) { $details[] = '[' . $this->translateF1($key) . '] => ' . $this->translateF4($d2); } else { $errors = (array)$d2; foreach ($errors as $key1 => $e1) { $e1 = (array)$e1; foreach ($e1 as $key2 => $e2) { $e2 = (array)$e2; foreach ($e2 as $key3 => $e3) { $e3 = (array)$e3; foreach ($e3 as $key4 => $e4) { $e4 = (array)$e4; $errorInfo = '[' . $this->translateF1($key) . '] => '; $errorInfo .= $this->translateF1($key1); if (is_string($key1) && $key1 != 'parcels') { $errorInfo .= $this->translateF2($key1); } else { $errorInfo .= ': '.$this->translateF2($key4); } foreach ($e4 as $key5 => $e5) { $e5 = (array)$e5; foreach ($e5 as $key6 => $e6) { $e6 = (array)$e6; if (is_string($key3)) { $errorInfo .= ' (' . $this->translateF3($key3) . ') ' . $this->translateF4($e6[0]); } else { if (is_string($key6)) { $errorInfo .= ' (' . $this->translateF3($key6) . ') ' . $this->translateF4($e6[0]); } else { $errorInfo .= ' ' . $this->translateF4($e6[0]); } } } } $details[] = $errorInfo; } } } } } } } } else { if (isset($dd) && $dd == 'required') { $details[] = '[' . $this->translateF1($key) . '] ' . $this->translateF2($key) . '' . ($dd == 'required' ? ' ' . $this->module_name->l('required', 'inposterrors') : ''); } elseif (isset($dd) && $dd == 'invalid') { $details[] = '[' . $this->translateF1($key) . '] ' . $this->translateF2($key) . '' . ($dd == 'invalid' ? ' ' . $this->module_name->l('invalid', 'inposterrors') : ''); } elseif (isset($dd) && $dd == 'does_not_exist') { $details[] = '[' . $this->translateF1($key) . '] ' . $this->translateF2($key) . '' . ($dd == 'does_not_exist' ? ' ' . $this->module_name->l('does_not_exist', 'inposterrors') : ''); } elseif (isset($dd) && $dd == 'already_bought') { $details[] = '[' . $this->translateF1($key) . '] ' . ($dd == 'already_bought' ? ' ' . $this->module_name->l('already_bought', 'inposterrors') : ''); } elseif (isset($dd) && $dd == 'too_short') { $details[] = '[' . $this->translateF1($key) . '] ' . ($dd == 'too_short' ? ' ' . $this->module_name->l('too_short', 'inposterrors') : ''); } elseif (isset($dd) && $dd == 'Parameter: street cannot be empty.') { $details[] = '[Zlecenie odbioru] Parametr: ulica nie może być pusty'; } } } } else { // $d is array switch ($d) { case 'Parameter: street cannot be empty.': $details[] = '[Zlecenie odbioru] Parametr: ulica nie może być pusty'; break; } } } return $details; } private function translateF($option) { $name = null; if (!empty($option)) { switch ($option) { case 'There are some validation errors. Check details object for more info.': $name = $this->module_name->l('There are some validation errors. Check details object for more info.', 'inposterrors'); break; default: $name = $this->module_name->l($option, 'inposterrors'); break; } } return $name; } private function translateF1($option) { $name = null; if (!empty($option)) { switch ($option) { case "sender": $name = $this->module_name->l('Sender', 'inposterrors'); break; case "receiver": $name = $this->module_name->l('Receiver', 'inposterrors'); break; case "custom_attributes": $name = $this->module_name->l('Atrybuty przesyłki', 'inposterrors'); break; case "shipment": $name = $this->module_name->l('Shipment', 'inposterrors'); break; case "shipments": $name = $this->module_name->l('Shipments', 'inposterrors'); break; case "reference": $name = $this->module_name->l('Numer referencyjny', 'inposterrors'); break; case "parcels": $name = $this->module_name->l('Paczka', 'inposterrors'); break; case "target_point": $name = $this->module_name->l('Paczkomat odbiorczy', 'inposterrors'); break; case "dropoff_point": $name = $this->module_name->l('Paczkomat nadawczy', 'inposterrors'); break; } } return $name; } private function translateF2($option) { $name = null; if (!empty($option)) { switch ($option) { case "address": $name = $this->module_name->l('Address', 'inposterrors'); break; case "phone": $name = $this->module_name->l('Phone', 'inposterrors'); break; case "company_name": $name = $this->module_name->l('Company name', 'inposterrors'); break; case "first_name": $name = $this->module_name->l('First name', 'inposterrors'); break; case "last_name": $name = $this->module_name->l('Last name', 'inposterrors'); break; case "email": $name = $this->module_name->l('E-mail', 'inposterrors'); break; case "weight": $name = $this->module_name->l('waga', 'inposterrors'); break; case "cod": $name = $this->module_name->l('pobranie', 'inposterrors'); break; case "insurance": $name = $this->module_name->l('ubezpieczenie', 'inposterrors'); break; case "target_point": $name = $this->module_name->l('Paczkomat odbiorczy', 'inposterrors'); break; case "dropoff_point": $name = $this->module_name->l('Paczkomat nadawczy', 'inposterrors'); break; case "does_not_exist": $name = $this->module_name->l('nie istnieje', 'inposterrors'); break; } } return $name; } private function translateF3($option) { $name = null; if (!empty($option)) { switch ($option) { case "street": $name = $this->module_name->l('street', 'inposterrors'); break; case "building_number": $name = $this->module_name->l('building number', 'inposterrors'); break; case "city": $name = $this->module_name->l('city', 'inposterrors'); break; case "post_code": $name = $this->module_name->l('post code', 'inposterrors'); break; case "amount": $name = $this->module_name->l('ilość', 'inposterrors'); break; case "does_not_exist": $name = $this->module_name->l('nie istnieje', 'inposterrors'); break; } } return $name; } private function translateF4($option) { $name = null; if (!empty($option)) { switch ($option) { case 'already_dispatched': $name = $this->module_name->l('Przesyłka została już nadana do odbioru przez kuriera.', 'inposterrors'); break; case 'too_short': $name = $this->module_name->l('jest za krótki', 'inposterrors'); break; case 'too_small': $name = $this->module_name->l('jest za mała', 'inposterrors'); break; case 'should_be_greater_than_cod': $name = $this->module_name->l('musi być większe od pobrania', 'inposterrors'); break; case "does_not_exist": $name = $this->module_name->l('nie istnieje', 'inposterrors'); break; } } return $name; } }