public function execute()
 {
     $id = waRequest::request('id', null, waRequest::TYPE_INT);
     $scm = new shopCustomerModel();
     $customer = $scm->getById($id);
     try {
         $contact = new waContact($id);
         $contact->getName();
     } catch (waException $e) {
         // !!! What to do when shop_customer exists, but no wa_contact found?
         throw $e;
     }
     $ccsm = new waContactCategoriesModel();
     $contact_categories = $ccsm->getContactCategories($id);
     $contacts_url = wa()->getAppUrl('contacts');
     // Info above tabs
     $top = array();
     foreach (array('email', 'phone', 'im') as $f) {
         if ($v = $contact->get($f, 'top,html')) {
             $top[] = array('id' => $f, 'name' => waContactFields::get($f)->getName(), 'value' => is_array($v) ? implode(', ', $v) : $v);
         }
     }
     // Get photo
     $photo = $contact->get('photo');
     $config = $this->getConfig();
     $use_gravatar = $config->getGeneralSettings('use_gravatar');
     $gravatar_default = $config->getGeneralSettings('gravatar_default');
     if (!$photo && $use_gravatar) {
         $photo = shopHelper::getGravatar($contact->get('email', 'default'), 96, $gravatar_default);
     } else {
         $photo = $contact->getPhoto(96);
     }
     $contact['photo'] = $photo;
     // Customer orders
     $im = new shopOrderItemsModel();
     $orders_collection = new shopOrdersCollection('search/contact_id=' . $id);
     $total_count = $orders_collection->count();
     $orders = $orders_collection->getOrders('*,items,params', 0, $total_count);
     shopHelper::workupOrders($orders);
     foreach ($orders as &$o) {
         $o['total_formatted'] = waCurrency::format('%{s}', $o['total'], $o['currency']);
         $o['shipping_name'] = ifset($o['params']['shipping_name'], '');
         $o['payment_name'] = ifset($o['params']['payment_name'], '');
         // !!! TODO: shipping and payment icons
     }
     // Customer reviews
     $prm = new shopProductReviewsModel();
     $reviews = $prm->getList('*,is_new,product', array('escape' => false, 'where' => array('contact_id' => $id), 'limit' => false));
     // Customer affiliate transactions history
     $atm = new shopAffiliateTransactionModel();
     $affiliate_history = $atm->getByContact($id);
     $this->view->assign('top', $top);
     $this->view->assign('orders', $orders);
     $this->view->assign('reviews', $reviews);
     $this->view->assign('contact', $contact);
     $this->view->assign('customer', $customer);
     $this->view->assign('contacts_url', $contacts_url);
     $this->view->assign('affiliate_history', $affiliate_history);
     $this->view->assign('contact_categories', $contact_categories);
     $this->view->assign('def_cur_tmpl', str_replace('0', '%s', waCurrency::format('%{s}', 0, wa()->getConfig()->getCurrency())));
     $this->view->assign('point_rate', str_replace(',', '.', (double) str_replace(',', '.', wa()->getSetting('affiliate_usage_rate'))));
     $fields = waContactFields::getAll('person');
     if (isset($fields['name'])) {
         unset($fields['name']);
     }
     $this->view->assign('fields', $fields);
     $this->view->assign('orders_default_view', $config->getOption('orders_default_view'));
     /*
      * @event backend_customer
      * @return array[string]array $return[%plugin_id%] array of html output
      * @return array[string][string]string $return[%plugin_id%]['info_section'] html output
      * @return array[string][string]string $return[%plugin_id%]['name_suffix'] html output
      * @return array[string][string]string $return[%plugin_id%]['header'] html output
      * @return array[string][string]string $return[%plugin_id%]['action_link'] html output
      */
     $this->view->assign('backend_customer', wa()->event('backend_customer', $customer));
 }
Exemple #2
0
 public function getOrder($id, $extend = false, $escape = true)
 {
     $order = $this->getById($id);
     if (!$order) {
         return array();
     }
     $order_params_model = new shopOrderParamsModel();
     $order['params'] = $order_params_model->get($id);
     if ($order['contact_id']) {
         $contact = new waContact($order['contact_id']);
         $order['contact'] = array('id' => $order['contact_id'], 'name' => $contact->getName(), 'email' => $contact->get('email', 'default'), 'phone' => $contact->get('phone', 'default'));
         $config = wa('shop')->getConfig();
         $use_gravatar = $config->getGeneralSettings('use_gravatar');
         $gravatar_default = $config->getGeneralSettings('gravatar_default');
         if (!$contact->get('photo') && $use_gravatar) {
             $order['contact']['photo_50x50'] = shopHelper::getGravatar($order['contact']['email'], 50, $gravatar_default);
         } else {
             $order['contact']['photo_50x50'] = $contact->getPhoto(50);
         }
     } else {
         $order['contact'] = $this->extractConctactInfo($order['params']);
     }
     if (!empty($order['params']['coupon_id'])) {
         $coupon_model = new shopCouponModel();
         $coupon = $coupon_model->getById($order['params']['coupon_id']);
         $order['coupon'] = array();
         if ($coupon) {
             $order['coupon'] = $coupon;
         } else {
             if (!empty($order['params']['coupon_code'])) {
                 $order['coupon']['code'] = $order['params']['coupon_code'];
             }
         }
     }
     $order_items_model = new shopOrderItemsModel();
     $order['items'] = $order_items_model->getItems($id, $extend);
     if ($escape) {
         if (!empty($order['items'])) {
             foreach ($order['items'] as &$product) {
                 if (!empty($product['name'])) {
                     $product['name'] = htmlspecialchars($product['name']);
                 }
                 if (!empty($product['item']['name'])) {
                     $product['item']['name'] = htmlspecialchars($product['item']['name']);
                 }
                 if (!empty($product['skus'])) {
                     foreach ($product['skus'] as &$sku) {
                         if (!empty($sku['name'])) {
                             $sku['name'] = htmlspecialchars($sku['name']);
                         }
                         unset($sku);
                     }
                 }
                 if (!empty($product['services'])) {
                     foreach ($product['services'] as &$service) {
                         if (!empty($service['name'])) {
                             $service['name'] = htmlspecialchars($service['name']);
                         }
                         if (!empty($service['item']['name'])) {
                             $service['item']['name'] = htmlspecialchars($service['item']['name']);
                         }
                         if (!empty($service['variants'])) {
                             foreach ($service['variants'] as &$variant) {
                                 $variant['name'] = htmlspecialchars($variant['name']);
                                 unset($variant);
                             }
                         }
                         unset($service);
                     }
                 }
                 unset($product);
             }
         }
         $order['contact']['name'] = htmlspecialchars($order['contact']['name']);
     }
     return $order;
 }
 public function execute()
 {
     $email = waRequest::get('email');
     $default = waRequest::get('default');
     $this->response = shopHelper::getGravatar($email, 50, $default) . '&f=y';
 }
 public function execute()
 {
     $category_id = waRequest::request('category', 0, 'int');
     $search = waRequest::request('search');
     $start = waRequest::request('start', 0, 'int');
     $limit = 50;
     $order = waRequest::request('order', '!last_order');
     $config = $this->getConfig();
     $use_gravatar = $config->getGeneralSettings('use_gravatar');
     $gravatar_default = $config->getGeneralSettings('gravatar_default');
     // Get customers
     $scm = new shopCustomerModel();
     list($customers, $total) = $scm->getList($category_id, $search, $start, $limit, $order);
     $has_more = $start + count($customers) < $total;
     $countries = array();
     foreach ($customers as &$c) {
         $c['affiliate_bonus'] = (double) $c['affiliate_bonus'];
         if (!$c['photo'] && $use_gravatar) {
             $c['photo'] = shopHelper::getGravatar(!empty($c['email']) ? $c['email'] : '', 50, $gravatar_default);
         } else {
             $c['photo'] = waContact::getPhotoUrl($c['id'], $c['photo'], 50, 50);
         }
         $c['categories'] = array();
         if (!empty($c['address']['region']) && !empty($c['address']['country'])) {
             $countries[$c['address']['country']] = array();
         }
     }
     unset($c);
     // Add region names to addresses
     if ($countries) {
         $rm = new waRegionModel();
         foreach ($rm->where('country_iso3 IN (?)', array_keys($countries))->query() as $row) {
             $countries[$row['country_iso3']][$row['code']] = $row['name'];
         }
         foreach ($customers as &$c) {
             if (!empty($c['address']['region']) && !empty($c['address']['country'])) {
                 $country = $c['address']['country'];
                 $region = $c['address']['region'];
                 if (!empty($countries[$country]) && !empty($countries[$country][$region])) {
                     $c['address']['region_formatted'] = $countries[$country][$region];
                 }
             }
         }
         unset($c);
     }
     // Contact categories
     $ccm = new waContactCategoryModel();
     $categories = $ccm->getAll('id');
     if ($customers) {
         $ccsm = new waContactCategoriesModel();
         foreach ($ccsm->getContactsCategories(array_keys($customers)) as $c_id => $list) {
             foreach ($list as $cat_id) {
                 if (!empty($categories[$cat_id])) {
                     $customers[$c_id]['categories'][$cat_id] = $categories[$cat_id];
                 }
             }
         }
     }
     // Set up lazy loading
     if (!$has_more) {
         // Do not trigger lazy loading, show total count at end of list
         $total_customers_number = $start + count($customers);
     } else {
         $total_customers_number = null;
         // trigger lazy loading
     }
     // List title and other params depending on list type
     if ($search) {
         $title = _w('Search results');
         $hash_start = '#/search/0/' . urlencode($search) . '/';
         $discount = null;
     } else {
         if ($category_id) {
             if (!empty($categories[$category_id])) {
                 $title = $categories[$category_id]['name'];
             } else {
                 $title = _w('Unknown category') . ' ' . $category_id;
             }
             $hash_start = '#/category/' . $category_id . '/';
             if (wa()->getSetting('discount_category')) {
                 $ccdm = new shopContactCategoryDiscountModel();
                 $discount = sprintf_wp('%s%% discount', $ccdm->getDiscount($category_id));
             } else {
                 $discount = null;
             }
         } else {
             $title = _w('All customers');
             $hash_start = '#/all/0/';
             $discount = null;
         }
     }
     $lazy_loading_params = array('limit=' . $limit, 'start=' . ($start + $limit), 'order=' . $order);
     if ($search) {
         $lazy_loading_params[] = 'search=' . $search;
     } else {
         if ($category_id) {
             $lazy_loading_params[] = 'category=' . $category_id;
         }
     }
     $lazy_loading_params = implode('&', $lazy_loading_params);
     $this->view->assign('cols', self::getCols());
     $this->view->assign('title', $title);
     $this->view->assign('order', $order);
     $this->view->assign('total', $total);
     $this->view->assign('discount', $discount);
     $this->view->assign('customers', $customers);
     $this->view->assign('hash_start', $hash_start);
     $this->view->assign('category_id', $category_id);
     $this->view->assign('lazy_loading_params', $lazy_loading_params);
     $this->view->assign('total_customers_number', $total_customers_number);
 }
 protected function extendContacts(&$orders)
 {
     $config = $this->getConfig();
     $use_gravatar = $config->getGeneralSettings('use_gravatar');
     $gravatar_default = $config->getGeneralSettings('gravatar_default');
     $emails = array();
     foreach ($orders as &$o) {
         if (isset($o['contact'])) {
             if (!$o['contact']['photo'] && $use_gravatar) {
                 if (!isset($emails[$o['contact']['id']])) {
                     $c = new waContact($o['contact']['id']);
                     $emails[$o['contact']['id']] = $c->get('email', 'default');
                 }
                 $email = $emails[$o['contact']['id']];
                 $o['contact']['photo_50x50'] = shopHelper::getGravatar($email, 50, $gravatar_default);
             } else {
                 $o['contact']['photo_50x50'] = waContact::getPhotoUrl($o['contact']['id'], $o['contact']['photo'], 50, 50);
             }
         } else {
             // contact deleted
             $o['contact']['name'] = isset($o['params']['contact_name']) ? $o['params']['contact_name'] : '';
             $o['contact']['name'] = htmlspecialchars($o['contact']['name']);
             $o['contact']['email'] = isset($o['params']['contact_email']) ? $o['params']['contact_email'] : '';
             $o['contact']['phone'] = isset($o['params']['contact_phone']) ? $o['params']['contact_phone'] : '';
             if ($use_gravatar) {
                 $o['contact']['photo_50x50'] = shopHelper::getGravatar($o['contact']['email'], 50, $gravatar_default);
             } else {
                 $o['contact']['photo_50x50'] = waContact::getPhotoUrl(null, null, 50, 50);
             }
         }
     }
     unset($o);
 }