public function execute() { $ids = waRequest::post('id', array(), 'array_int'); if (!$ids) { $ids = (int) waRequest::get('id'); if (!$ids) { throw new Exception('No ids specified.'); } $ids = array($ids); } // do not try to delete self if (in_array($this->getUser()->getId(), $ids)) { die('<p>' . _w('You can not delete yourself.') . '</p><p>' . _w('Please eliminate yourself from deletion list.') . '</p>'); } // Only allow actions with contacts available for current user if (!$this->getRights('category.all')) { $crm = new contactsRightsModel(); $ccm = new waContactCategoriesModel(); $allowed = array_keys($crm->getAllowedCategories()); foreach ($ccm->getContactsCategories($ids) as $id => $cats) { if (!array_intersect($allowed, $cats)) { throw new waRightsException('Access denied'); } } } $superadmin = wa()->getUser()->getRights('webasyst', 'backend'); $result = wa()->event('links', $ids); $this->view->assign('apps', wa()->getApps()); $links = array(); foreach ($result as $app_id => $app_links) { foreach ($app_links as $contact_id => $contact_links) { if ($contact_links) { $links[$contact_id][$app_id] = $contact_links; } } } // Do not allow non-superadmin to remove users if (!$superadmin) { $um = new waUserModel(); $users = array_keys($um->getByField(array('id' => $ids, 'is_user' => 1), 'id')); foreach ($users as $user_id) { if (!isset($links[$user_id]['contacts'])) { $links[$user_id]['contacts'] = array(); } $links[$user_id]['contacts'][] = array('user', 1); } } $contact_model = new waContactModel(); $this->view->assign('ids', $superadmin ? $ids : array_diff($ids, array_keys($links))); $this->view->assign('contacts', $contact_model->getName(array_keys($links))); $this->view->assign('superadmin', $superadmin); $this->view->assign('all', count($ids)); $this->view->assign('links', $links); }
public function execute() { $superadmin = $this->getUser()->getRights('webasyst', 'backend'); $contacts = waRequest::post('id', array(), 'array_int'); // do not try to delete self if (in_array($this->getUser()->getId(), $contacts)) { throw new waRightsException('Access denied: attempt to delete own account.'); } // Only allow actions with contacts available for current user if (!$this->getRights('category.all')) { $crm = new contactsRightsModel(); $ccm = new waContactCategoriesModel(); $allowed = array_keys($crm->getAllowedCategories()); foreach ($ccm->getContactsCategories($contacts) as $id => $cats) { if (!array_intersect($allowed, $cats)) { throw new waRightsException('Access denied: no access to contact ' . $id); } } } // Deletion of contacts with links to other applications is only allowed to superadmins if (!$superadmin && ($links = wa()->event('links', $contacts))) { foreach ($links as $app_id => $l) { foreach ($l as $contact_id => $contact_links) { if ($contact_links) { throw new waRightsException('Access denied: only superadmin is allowed to delete contacts with links to other applications.'); } } } } // Are there users among $contacts? $um = new waUserModel(); $users = array_keys($um->getByField(array('id' => $contacts, 'is_user' => 1), 'id')); // deletion of users is only allowed to superadmins if (!$superadmin && $users) { throw new waRightsException('Access denied: only superadmin is allowed to delete users.'); } // Revoke user access before deletion foreach ($users as $user_id) { waUser::revokeUser($user_id); } // Bye bye... $contact_model = new waContactModel(); $contact_model->delete($contacts); // also throws a contacts.delete event $this->response['deleted'] = count($contacts); $this->response['message'] = sprintf(_w("%d contact has been deleted", "%d contacts have been deleted", $this->response['deleted']), $this->response['deleted']); $this->log('contact_delete', count($contacts)); }
protected function checkAccess() { if ($this->getRights('category.all')) { return; } // Only allow actions with categories available for current user $crm = new contactsRightsModel(); $allowed = $crm->getAllowedCategories(); foreach (waRequest::post('categories', array(), 'array_int') as $id) { if (!isset($allowed[$id])) { throw new waRightsException('Access denied'); } } // Only allow actions with contacts available for current user $allowed = array_keys($allowed); $ccm = new waContactCategoriesModel(); foreach ($ccm->getContactsCategories(waRequest::post('contacts', array(), 'array_int')) as $id => $cats) { if (!array_intersect($allowed, $cats)) { throw new waRightsException('Access denied'); } } }
/** * Merge given contacts into master contact, save, send merge event, then delete slaves. * * !!! Probably should move it into something like contactsHelper * * @param array $merge_ids list of contact ids * @param int $master_id contact id to merge others into * @return array */ public static function merge($merge_ids, $master_id) { $merge_ids[] = $master_id; // List of contacts to merge $collection = new contactsCollection('id/' . implode(',', $merge_ids)); $contacts_data = $collection->getContacts('*'); // Master contact data if (!$master_id || !isset($contacts_data[$master_id])) { throw new waException('No contact to merge into.'); } $master_data = $contacts_data[$master_id]; unset($contacts_data[$master_id]); $master = new waContact($master_id); $result = array('total_requested' => count($contacts_data) + 1, 'total_merged' => 0, 'error' => '', 'users' => 0); if ($master_data['photo']) { $filename = wa()->getDataPath(waContact::getPhotoDir($master_data['id']) . "{$master_data['photo']}.original.jpg", true, 'contacts'); if (!file_exists($filename)) { $master_data['photo'] = null; } } $data_fields = waContactFields::getAll('enabled'); $check_duplicates = array(); // field_id => true $update_photo = null; // if need to update photo here it is file paths // merge loop foreach ($contacts_data as $id => $info) { if ($info['is_user'] > 0) { $result['users']++; unset($contacts_data[$id]); continue; } foreach ($data_fields as $f => $field) { if (!empty($info[$f])) { if ($field->isMulti()) { $master->add($f, $info[$f]); $check_duplicates[$f] = true; } else { // Field does not allow multiple values. // Set value if no value yet. if (empty($master_data[$f])) { $master[$f] = $master_data[$f] = $info[$f]; } } } } // photo if (!$master_data['photo'] && $info['photo'] && !$update_photo) { $filename_original = wa()->getDataPath(waContact::getPhotoDir($info['id']) . "{$info['photo']}.original.jpg", true, 'contacts'); if (file_exists($filename_original)) { $update_photo = array('original' => $filename_original); $filename_crop = wa()->getDataPath(waContact::getPhotoDir($info['id']) . "{$info['photo']}.jpg", true, 'contacts'); if (file_exists($filename_crop)) { $update_photo['crop'] = $filename_crop; } } } // birthday parts if (!empty($data_fields['birthday'])) { foreach (array('birth_day', 'birth_month', 'birth_year') as $f) { if (empty($master_data[$f]) && !empty($info[$f])) { $master[$f] = $master_data[$f] = $info[$f]; } } } } // Remove duplicates foreach (array_keys($check_duplicates) as $f) { $values = $master[$f]; if (!is_array($values) || count($values) <= 1) { continue; } $unique_values = array(); // md5 => true foreach ($values as $k => $v) { if (is_array($v)) { if (isset($v['value']) && is_string($v['value'])) { $v = $v['value']; } else { unset($v['ext'], $v['status']); ksort($v); $v = serialize($v); } } $hash = md5(mb_strtolower($v)); if (!empty($unique_values[$hash])) { unset($values[$k]); continue; } $unique_values[$hash] = true; } $master[$f] = array_values($values); } // Save master contact $errors = $master->save(array(), 42); // 42 == do not validate anything at all if ($errors) { $errormsg = array(); foreach ($errors as $field => $err) { if (!is_array($err)) { $err = array($err); } foreach ($err as $str) { $errormsg[] = $field . ': ' . $str; } } $result['error'] = implode("\n<br>", $errormsg); return $result; } // Merge categories $category_ids = array(); $ccm = new waContactCategoriesModel(); foreach ($ccm->getContactsCategories($merge_ids) as $cid => $cats) { $category_ids += array_flip($cats); } $category_ids = array_keys($category_ids); $ccm->add($master_id, $category_ids); // update photo if ($update_photo) { $rand = mt_rand(); $path = wa()->getDataPath(waContact::getPhotoDir($master['id']), true, 'contacts', false); // delete old image if (file_exists($path)) { waFiles::delete($path); } waFiles::create($path); $filename = $path . "/" . $rand . ".original.jpg"; waFiles::create($filename); waImage::factory($update_photo['original'])->save($filename, 90); if (!empty($update_photo['crop'])) { $filename = $path . "/" . $rand . ".jpg"; waFiles::create($filename); waImage::factory($update_photo['crop'])->save($filename, 90); } else { waFiles::copy($filename, $path . "/" . $rand . ".jpg"); } $master->save(array('photo' => $rand)); } $result['total_merged'] = count($contacts_data) + 1; $contact_ids = array_keys($contacts_data); // wa_log $log_model = new waLogModel(); $log_model->updateByField('contact_id', $contact_ids, array('contact_id' => $master_id)); // wa_login_log $login_log_model = new waLoginLogModel(); $login_log_model->updateByField('contact_id', $contact_ids, array('contact_id' => $master_id)); // Merge event $params = array('contacts' => $contact_ids, 'id' => $master_data['id']); wa()->event(array('contacts', 'merge'), $params); // Delete all merged contacts $contact_model = new waContactModel(); $contact_model->delete($contact_ids, false); // false == do not trigger event $history_model = new contactsHistoryModel(); foreach ($contact_ids as $contact_id) { $history_model->deleteByField(array('type' => 'add', 'hash' => '/contact/' . $contact_id)); } return $result; }
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); }