public function execute() { mb_internal_encoding("UTF-8"); $query = waRequest::post('query'); $query = strtolower($query); $collection_by_email = new waContactsCollection('/search/email*=' . $query . '/'); $contacts_by_email = $collection_by_email->getContacts('*'); $collection_by_name = new waContactsCollection('/search/name*=' . $query . '/'); $contacts_by_name = $collection_by_name->getContacts('*'); if (is_array($contacts_by_email) && is_array($contacts_by_name)) { $contacts = array_merge($contacts_by_email, $contacts_by_name); } else { if (is_array($contacts_by_email) || is_array($contacts_by_name)) { $contacts = is_array($contacts_by_email) ? $contacts_by_email : $contacts_by_name; } else { $contacts = array(); } } $modelContactCategory = new waContactCategoryModel(); $result = $modelContactCategory->getByField('name', $query); // query("SELECT * FROM wa_contact_category WHERE name LIKE '%".mysql_escape_string($query)."%'")->fetchAll(); if ($result) { $search['group'] = $result; $search['contacts'] = $contacts; } else { $search['group'] = array(); $search['contacts'] = $contacts; } $this->response['search'] = $search; }
public function execute() { $id = waRequest::post('id'); if (is_numeric($id)) { $modelNotifierRule = new shopNotifierRuleModel(); $result = $modelNotifierRule->getById($id); $result['data_contact'] = json_decode($result['data_contact'], true); if (count((array) $result['data_contact']['contact']) > 1) { $ids_contact = implode(',', (array) $result['data_contact']['contact']); } else { if (count((array) $result['data_contact']['contact']) == 1) { foreach ($result['data_contact']['contact'] as $c) { $ids_contact = $c; } } } if (isset($ids_contact)) { $result['contacts'] = array(); $collection = new waContactsCollection('/id/' . $ids_contact . '/'); $result['contacts'] = $collection->getContacts('*'); } if (array_key_exists("group", $result['data_contact']) && count($result['data_contact']['group'])) { $result['groups'] = array(); $modelContactCategory = new waContactCategoryModel(); foreach ($result['data_contact']['group'] as $group_id) { $result['groups'][$group_id] = $modelContactCategory->getById($group_id); } } $result['state_name'] = (array) json_decode($result['state_name']); $this->response['result'] = $result; $this->response['message'] = 'ok'; } else { $this->response['message'] = 'fail'; } }
/** * @param int[] $params Deleted contact_id * @see waEventHandler::execute() * @return void */ public function execute(&$params) { $contact_ids = $params; // We need some info about (not yet) deleted contacts to save into order params. // The idea is to pretend that orders were created by guests with no auth. $c = new waContactsCollection('id/' . implode(',', $contact_ids)); $contacts = $c->getContacts('name,phone,email'); foreach ($contacts as &$contact) { if (is_array($contact['phone'])) { $phone = reset($contact['phone']); if (is_array($phone)) { if (isset($phone['value'])) { $phone = $phone['value']; } else { $phone = ''; } } $contact['phone'] = $phone; } if (is_array($contact['email'])) { $email = reset($contact['email']); $contact['email'] = $email; } } $order_model = new shopOrderModel(); $order_params_model = new shopOrderParamsModel(); $product_reviews_model = new shopProductReviewsModel(); foreach ($contacts as $contact) { // Insert customer info into params of their orders $order_ids = array_keys($order_model->select('id')->where('contact_id=:contact_id', array('contact_id' => $contact['id']))->fetchAll('id')); $order_params_model->set($order_ids, $this->extractContactInfo($contact), false); // Insert contact name into their reviews $product_reviews_model->updateByField('contact_id', $contact['id'], array('contact_id' => 0, 'name' => $contact['name'], 'auth_provider' => null)); } // Update orders as if they were created by guests with no auth $order_model->updateByField('contact_id', $contact_ids, array('contact_id' => null)); // Forget the customer $scm = new shopCustomerModel(); $scm->deleteById($contact_ids); // Forget that this user created coupons $coupm = new shopCouponModel(); $coupm->updateByField('create_contact_id', $contact_ids, array('create_contact_id' => 0)); // !!! TODO: take a look to other models related with contacts }
/** * @param int|array $contact_id */ public static function getAuthorInfo($contact_id) { $fields = 'id,name,photo_url_50,photo_url_20,is_user'; $contact_ids = (array) $contact_id; $collection = new waContactsCollection('id/' . implode(',', $contact_ids)); $contacts = $collection->getContacts($fields, 0, count($contact_ids)); if (is_numeric($contact_id)) { if (isset($contacts[$contact_id])) { return $contacts[$contact_id]; } else { return array_fill_keys(explode(',', $fields), ''); } } else { return $contacts; } }
/** * Extend items by adding contact info into $rows[i]['user'] * Uses: * - $rows[i]['contact_id'] * - $rows[i]['name'] or $rows[i]['contact_name'] when contact is not found or its name is empty * - $rows[i]['auth_provider'] for default userpic URL * * @param array $rows * @param array $fields * @param bool $get_link pass true to get $rows[i]['user']['posts_link'] */ public static function extendUser(&$rows, $fields = array(), $get_link = false) { $default_fields = array('id', 'name', 'firstname', 'middlename', 'lastname'); $fields = array_unique(array_merge($fields, $default_fields)); // All contact ids $ids = array(); foreach ($rows as $row) { if ($row['contact_id']) { $ids[] = intval($row['contact_id']); } } $ids = array_unique($ids); // Fetch contacts using collection $collection = new waContactsCollection($ids); $contacts = $collection->getContacts(implode(',', $fields), 0, count($ids)); // Prepare data row to use as a placeholder when contact is not found $contact = new waContact(0); $contacts[0] = array('name' => ''); $photo_fields = array(); foreach ($fields as $field) { if (preg_match('@^photo_url_(\\d+)$@', $field, $matches)) { $photo_fields[] = $field; $contacts[0][$field] = $contact->getPhoto($matches[1], $matches[1]); } else { $contacts[0][$field] = $contact->get($field); } } // Format contact names foreach ($contacts as &$c) { $c['name'] = waContactNameField::formatName($c); } unset($c); // Add data as 'user' key to each row in $rows $app_static_url = wa()->getAppStaticUrl(); foreach ($rows as &$row) { $row['user'] = array(); $id = $row['contact_id'] = max(0, intval($row['contact_id'])); if (!isset($contacts[$id])) { $id = 0; } if (isset($contacts[$id])) { if (isset($row['url']) && $get_link && !isset($contacts[$id]['posts_link'])) { $contacts[$id]['posts_link'] = blogPost::getUrl($row, 'author'); } $row['user'] = $contacts[$id]; } if (!$id || !isset($contacts[$id])) { if (isset($row['name'])) { $row['user']['name'] = $row['name']; } elseif (isset($row['contact_name'])) { $row['user']['name'] = $row['contact_name']; } if (isset($row['auth_provider'])) { if ($row['auth_provider'] && $row['auth_provider'] != blogCommentModel::AUTH_GUEST) { $row['user']['photo_url'] = "{$app_static_url}img/{$row['auth_provider']}.png"; foreach ($photo_fields as $field) { $row['user'][$field] =& $row['user']['photo_url']; } } } } unset($row); } }
public function contact($id) { if (!is_numeric($id)) { $collection = new waContactsCollection('/search/' . $id . '/', array('check_rights' => false)); $result = $collection->getContacts('id', 0, 1); if ($result) { $c = current($result); return new waContact($c['id']); } else { return new waContact(); } } $contact_model = new waContactModel(); if ($contact = $contact_model->getById($id)) { return new waContact($contact); } return new waContact(); }
/** * * Extend items by contact info * @param array $rows * @param array $fields * @param bool $get_link */ public static function extendUser(&$rows, $fields = array(), $get_link = false) { $default_fields = array('id', 'name'); $fields = array_unique(array_merge($fields, $default_fields)); $ids = array(); foreach ($rows as $row) { if ($row['contact_id']) { $ids[] = intval($row['contact_id']); } } $ids = array_unique($ids); $collection = new waContactsCollection($ids); $contacts = $collection->getContacts(implode(',', $fields), 0, count($ids)); $contact = new waContact(0); $contacts[0] = array('name' => ''); $photo_fields = array(); foreach ($fields as $field) { if (preg_match('@^photo_url_(\\d+)$@', $field, $matches)) { $photo_fields[] = $field; $contacts[0][$field] = $contact->getPhoto($matches[1], $matches[1]); } else { $contacts[0][$field] = $contact->get($field); } } $app_static_url = wa()->getAppStaticUrl(); foreach ($rows as &$row) { $row['user'] = array(); $id = $row['contact_id'] = max(0, intval($row['contact_id'])); if (!isset($contacts[$id])) { $id = 0; } if (isset($contacts[$id])) { if (isset($row['url']) && $get_link && !isset($contacts[$id]['posts_link'])) { $contacts[$id]['posts_link'] = blogPost::getUrl($row, 'author'); } $row['user'] = $contacts[$id]; } if (!$id || !isset($contacts[$id])) { if (isset($row['name'])) { $row['user']['name'] = $row['name']; } elseif (isset($row['contact_name'])) { $row['user']['name'] = $row['contact_name']; } if (isset($row['auth_provider'])) { if ($row['auth_provider'] && $row['auth_provider'] != blogCommentModel::AUTH_GUEST) { $row['user']['photo_url'] = "{$app_static_url}img/{$row['auth_provider']}.png"; foreach ($photo_fields as $field) { $row['user'][$field] =& $row['user']['photo_url']; } } } } unset($row); } }
public function contactsDelete($contact_ids) { $c = new waContactsCollection('id/' . implode(',', $contact_ids)); $contacts = $c->getContacts('name,phone,email'); foreach ($contacts as &$contact) { if (is_array($contact['phone'])) { $phone = reset($contact['phone']); if (is_array($phone)) { if (isset($phone['value'])) { $phone = $phone['value']; } else { $phone = ''; } } $contact['phone'] = $phone; } if (is_array($contact['email'])) { $email = reset($contact['email']); $contact['email'] = $email; } } $comment_model = new photosCommentModel(); foreach ($contacts as $contact) { // Insert contact name into their reviews $comment_model->updateByField('contact_id', $contact['id'], array('contact_id' => 0, 'name' => $contact['name'], 'email' => $contact['email'], 'auth_provider' => null)); } }