Ejemplo n.º 1
0
 /**
  * @static
  * @param $args
  * @return array
  *
  * The newsletter system requests updated customer / user data from this group plugin.
  * It does this when building the member list, and also 
  */
 public static function newsletter_callback($args)
 {
     if (!isset($args['owner_table']) || !$args['owner_table']) {
         return array();
     }
     switch ($args['owner_table']) {
         case 'user':
             if ((int) $args['owner_id'] > 0) {
                 $sql = "SELECT c.customer_name AS company_name, c.customer_name AS customer_name";
                 $sql .= " , pu.user_id ";
                 $sql .= " , c.customer_id ";
                 $sql .= " ,c.credit ";
                 $sql .= " , pu.name AS user_name, pu.name AS first_name, pu.last_name AS last_name, pu.phone AS phone, pu.`email` AS `email`, pu.`mobile` AS `mobile`";
                 $sql .= " , a.line_1, a.line_2, a.suburb, a.state, a.region, a.country, a.post_code ";
                 $sql .= ' FROM `' . _DB_PREFIX . "user` pu";
                 $sql .= " LEFT JOIN `" . _DB_PREFIX . "customer` c ON pu.customer_id = c.customer_id";
                 $sql .= ' LEFT JOIN `' . _DB_PREFIX . "address` a ON c.customer_id = a.owner_id AND a.owner_table = 'customer' AND a.address_type = 'physical'";
                 $sql .= " WHERE pu.user_id = " . (int) $args['owner_id'];
                 $user = qa1($sql);
                 if (!is_array($user) || !isset($user['user_id']) || !$user['user_id']) {
                     return false;
                 }
                 if (isset($args['basic']) && $args['basic']) {
                     return $user;
                 }
                 //                    $name_parts = explode(" ",preg_replace('/\s+/',' ',$user['user_name']));
                 //                    $user['first_name'] = array_shift($name_parts);
                 //                    $user['last_name'] = implode(' ',$name_parts);
                 // get extras for the user.
                 $extras = module_extra::get_extras(array('owner_table' => 'user', 'owner_id' => $user['user_id']));
                 foreach ($extras as $extra) {
                     if (!strlen(trim($extra['extra']))) {
                         continue;
                     }
                     $key = $extra['extra_key'];
                     $x = 1;
                     while (isset($user[$key])) {
                         $key = $extra['extra_key'] . $x;
                         $x++;
                     }
                     $user[$key] = trim($extra['extra']);
                 }
                 // get extras for the customer.
                 if (isset($user['customer_id']) && $user['customer_id'] > 0) {
                     $extras = module_extra::get_extras(array('owner_table' => 'customer', 'owner_id' => $user['customer_id']));
                     foreach ($extras as $extra) {
                         if (!strlen(trim($extra['extra']))) {
                             continue;
                         }
                         $key = $extra['extra_key'];
                         $x = 1;
                         while (isset($user[$key])) {
                             $key = $extra['extra_key'] . $x;
                             $x++;
                         }
                         $user[$key] = trim($extra['extra']);
                     }
                 }
                 if ($user['customer_id']) {
                     $user['_edit_link'] = module_user::link_open_contact($user['user_id'], false, $user);
                 } else {
                     $user['_edit_link'] = module_user::link_open($user['user_id'], false, $user);
                 }
                 return $user;
             }
             break;
         case 'customer':
             if (module_config::c('newsletter_send_all_customer_contacts', 1)) {
                 // update - we use the above 'user' callback and return a listing for each contact in the array.
                 // using the special _multi flag hack to tell our newsletter plugin that this result contains multiple entries.
                 $users = array('_multi' => true);
                 $sql = "SELECT u.user_id FROM `" . _DB_PREFIX . "user` u WHERE u.customer_id = " . (int) $args['owner_id'];
                 $contacts = qa($sql);
                 foreach ($contacts as $contact) {
                     $data_args = array('owner_id' => $contact['user_id'], 'owner_table' => 'user');
                     $users[$contact['user_id']] = self::newsletter_callback($data_args);
                     if ($users[$contact['user_id']]) {
                         $users[$contact['user_id']]['data_args'] = json_encode($data_args);
                     }
                 }
                 return $users;
             } else {
                 $sql = "SELECT c.customer_name AS company_name, c.customer_name AS customer_name";
                 $sql .= " ,c.credit ";
                 $sql .= " , pu.user_id ";
                 $sql .= " , c.customer_id ";
                 $sql .= " , pu.name AS user_name, pu.name AS first_name, pu.last_name AS last_name, pu.phone AS phone, pu.`email` AS `email`, pu.`mobile` AS `mobile`";
                 $sql .= " , a.line_1, a.line_2, a.suburb, a.state, a.region, a.country, a.post_code ";
                 $sql .= " FROM `" . _DB_PREFIX . "customer` c ";
                 $sql .= ' LEFT JOIN `' . _DB_PREFIX . "address` a ON c.customer_id = a.owner_id AND a.owner_table = 'customer' AND a.address_type = 'physical'";
                 $sql .= ' LEFT JOIN `' . _DB_PREFIX . "user` pu ON c.primary_user_id = pu.user_id";
                 $sql .= " WHERE c.customer_id = " . (int) $args['owner_id'];
                 $user = qa1($sql);
                 if (!$user || !isset($user['customer_id'])) {
                     return array();
                 }
                 //$name_parts = explode(" ",preg_replace('/\s+/',' ',$user['user_name']));
                 //$user['first_name'] = array_shift($name_parts);
                 //$user['last_name'] = implode(' ',$name_parts);
                 if (isset($args['basic']) && $args['basic']) {
                     return $user;
                 }
                 // get extras for the customer.
                 $extras = module_extra::get_extras(array('owner_table' => 'customer', 'owner_id' => $user['customer_id']));
                 foreach ($extras as $extra) {
                     if (!strlen(trim($extra['extra']))) {
                         continue;
                     }
                     $key = $extra['extra_key'];
                     $x = 1;
                     while (isset($user[$key])) {
                         $key = $extra['extra_key'] . $x;
                         $x++;
                     }
                     $user[$key] = trim($extra['extra']);
                 }
                 if (isset($user['user_id']) && $user['user_id'] > 0) {
                     // get extras for the user.
                     $extras = module_extra::get_extras(array('owner_table' => 'user', 'owner_id' => $user['user_id']));
                     foreach ($extras as $extra) {
                         if (!strlen(trim($extra['extra']))) {
                             continue;
                         }
                         $key = $extra['extra_key'];
                         $x = 1;
                         while (isset($user[$key])) {
                             $key = $extra['extra_key'] . $x;
                             $x++;
                         }
                         $user[$key] = trim($extra['extra']);
                     }
                 }
                 $user['_edit_link'] = module_customer::link_open($user['customer_id'], false, $user);
                 return $user;
             }
         case 'website':
             $sql = "SELECT c.customer_name AS company_name";
             $sql .= " ,c.credit ";
             $sql .= " ,w.name AS website_name";
             $sql .= " ,w.url AS website_url";
             $sql .= " , pu.user_id ";
             $sql .= " , c.customer_id ";
             $sql .= " , pu.name AS user_name, pu.phone AS phone, pu.`email` AS `email`, pu.`mobile` AS `mobile`";
             $sql .= " , a.line_1, a.line_2, a.suburb, a.state, a.region, a.country, a.post_code ";
             $sql .= " FROM `" . _DB_PREFIX . "website` w ";
             $sql .= ' LEFT JOIN `' . _DB_PREFIX . "customer` c ON w.customer_id = c.customer_id";
             $sql .= ' LEFT JOIN `' . _DB_PREFIX . "address` a ON c.customer_id = a.owner_id AND a.owner_table = 'customer' AND a.address_type = 'physical'";
             $sql .= ' LEFT JOIN `' . _DB_PREFIX . "user` pu ON c.primary_user_id = pu.user_id";
             $sql .= " WHERE w.website_id = " . (int) $args['owner_id'];
             $user = qa1($sql);
             $name_parts = explode(" ", preg_replace('/\\s+/', ' ', $user['user_name']));
             $user['first_name'] = array_shift($name_parts);
             $user['last_name'] = implode(' ', $name_parts);
             if (isset($args['basic']) && $args['basic']) {
                 return $user;
             }
             // get extras for the website.
             $extras = module_extra::get_extras(array('owner_table' => 'website', 'owner_id' => $args['owner_id']));
             foreach ($extras as $extra) {
                 if (!strlen(trim($extra['extra']))) {
                     continue;
                 }
                 $key = $extra['extra_key'];
                 $x = 1;
                 while (isset($user[$key])) {
                     $key = $extra['extra_key'] . $x;
                     $x++;
                 }
                 $user[$key] = trim($extra['extra']);
             }
             // then get extras for the company
             $extras = module_extra::get_extras(array('owner_table' => 'customer', 'owner_id' => $user['customer_id']));
             foreach ($extras as $extra) {
                 if (!strlen(trim($extra['extra']))) {
                     continue;
                 }
                 $key = $extra['extra_key'];
                 $x = 1;
                 while (isset($user[$key])) {
                     $key = $extra['extra_key'] . $x;
                     $x++;
                 }
                 $user[$key] = trim($extra['extra']);
             }
             if (isset($user['user_id']) && $user['user_id'] > 0) {
                 // get extras for the user.
                 $extras = module_extra::get_extras(array('owner_table' => 'user', 'owner_id' => $user['user_id']));
                 foreach ($extras as $extra) {
                     if (!strlen(trim($extra['extra']))) {
                         continue;
                     }
                     $key = $extra['extra_key'];
                     $x = 1;
                     while (isset($user[$key])) {
                         $key = $extra['extra_key'] . $x;
                         $x++;
                     }
                     $user[$key] = trim($extra['extra']);
                 }
             }
             $user['_edit_link'] = module_customer::link_open($user['customer_id'], false, $user);
             return $user;
         case 'ticket':
             //echo 'Getting ticket for '.$args['owner_id'] . ' and basic is '.var_export($args['basic'],true);exit;
             return module_ticket::get_newsletter_recipient($args['owner_id'], isset($args['basic']) && $args['basic']);
         case 'member':
             return module_member::get_newsletter_recipient($args['owner_id'], isset($args['basic']) && $args['basic']);
         case 'newsletter_subscription':
             return module_member::get_newsletter_recipient($args['owner_id'], isset($args['basic']) && $args['basic']);
     }
     return array();
 }