protected function addToCategory()
 {
     $contacts = waRequest::post('contacts', array(), 'array_int');
     $categories = waRequest::post('categories', array(), 'array_int');
     $ccm = new waContactCategoriesModel();
     $ccm->add($contacts, $categories);
     $contacts = count($contacts);
     $categories = count($categories);
     $this->response['message'] = sprintf(_w("%d contact has been added", "%d contacts have been added", $contacts), $contacts);
     $this->response['message'] .= ' ';
     $this->response['message'] .= sprintf(_w("to %d category", "to %d categories", $categories), $categories);
 }
 public function execute()
 {
     $customer_id = waRequest::request('customer_id', 0, 'int');
     $category_id = waRequest::request('category_id', 0, 'int');
     if (!$customer_id || !$category_id) {
         return;
     }
     $ccm = new waContactCategoriesModel();
     $ccm->add($customer_id, $category_id);
     $cm = new shopCustomerModel();
     $this->response['count'] = $cm->getCategoryCounts($category_id);
 }
 /**
  * Обработчик хука signup
  *
  * @param waContact $contact
  */
 public function handlerSignup($contact)
 {
     $category_id = $this->getSettings('category_id');
     $ContactCategory = new waContactCategoryModel();
     // проверим на всякий случай есть-ли еще такая категория
     // а то вдруг ее какой-нибудь дурак удалил, а в настройке плагина она осталась
     // ресурсов на проверку нужно мало, а дураков на свете много
     $category_id = $ContactCategory->select('id')->where('id=:id', array('id' => $category_id))->fetchField();
     if ($contact instanceof waContact && $contact->getId() && $category_id) {
         $ContactCategories = new waContactCategoriesModel();
         $ContactCategories->add($contact->getId(), $category_id);
     }
 }
 protected function addToCategory()
 {
     $contacts = waRequest::post('contacts', array(), 'array_int');
     $categories = $this->getCategories();
     $ccm = new waContactCategoriesModel();
     $ccm->add($contacts, $categories);
     foreach ($categories as $category_id) {
         $c = new waContactsCollection("/category/" . $category_id);
         $this->response['count'][$category_id] = $c->count();
     }
     $contacts = count($contacts);
     $categories = count($categories);
     $this->response['message'] = sprintf(_w("%d contact has been added", "%d contacts have been added", $contacts), $contacts);
     $this->response['message'] .= ' ';
     $this->response['message'] .= sprintf(_w("to %d category", "to %d categories", $categories), $categories);
 }
 public function execute()
 {
     // only allowed to global admin
     if (!wa()->getUser()->getRights('webasyst', 'backend')) {
         throw new waRightsException(_w('Access denied'));
     }
     $contacts = waRequest::post('contacts', array(), 'array_int');
     $categories = waRequest::post('categories', array(), 'array_int');
     $ccm = new waContactCategoriesModel();
     $ccm->add($contacts, $categories);
     foreach ($categories as $category_id) {
         $c = new waContactsCollection("/category/" . $category_id);
         $this->response['count'][$category_id] = $c->count();
     }
     $contacts = count($contacts);
     $categories = count($categories);
     $this->response['message'] = sprintf(_w("%d contact has been added", "%d contacts have been added", $contacts), $contacts);
     $this->response['message'] .= ' ';
     $this->response['message'] .= sprintf(_w("to %d category", "to %d categories", $categories), $categories);
 }
 /**
  * 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;
 }
Esempio n. 7
0
<?php

$category_model = new waContactCategoryModel();
$category = $category_model->getBySystemId('blog');
$contact_model = new waContactModel();
$sql = "SELECT id FROM wa_contact WHERE create_app_id='blog'";
$contact_ids = $contact_model->query($sql)->fetchAll(null, true);
if ($contact_ids) {
    $contact_categories_model = new waContactCategoriesModel();
    $contact_categories_model->add($contact_ids, $category['id']);
}
Esempio n. 8
0
 /**
  * Adds contact to a category.
  *
  * @param int|string $category_id Category's simple numeric or system string key (app_id)
  * @throws waException
  */
 public function addToCategory($category_id)
 {
     if (!$this->id) {
         throw new waException('Contact not saved!');
     }
     if (!is_numeric($category_id)) {
         $category_model = new waContactCategoryModel();
         $category = $category_model->getBySystemId($category_id);
         $category_id = $category['id'];
     }
     $contact_categories_model = new waContactCategoriesModel();
     if (!$contact_categories_model->inCategory($this->id, $category_id)) {
         $contact_categories_model->add($this->id, $category_id);
     }
 }