コード例 #1
0
 public function execute()
 {
     if (!$this->getRequest()->request('json', 0)) {
         $action = new contactsContactsInfoAction();
         echo $action->display();
         return;
     }
     $m = new waContactModel();
     $contact_id = $this->getRequest()->request('id', 0, 'int');
     $contact = new waContact($contact_id);
     $values = $contact->load('js', true);
     if (isset($values['company_contact_id'])) {
         if (!$m->getById($values['company_contact_id'])) {
             $values['company_contact_id'] = 0;
             $contact->save(array('company_contact_id' => 0));
         }
     }
     $values['photo_url_96'] = $contact->getPhoto(96);
     $values['photo_url_20'] = $contact->getPhoto(20);
     $fields = waContactFields::getInfo($contact['is_company'] ? 'company' : 'person', true);
     echo json_encode(array('fields' => $fields, 'values' => $values, 'top' => contactsHelper::getTop($contact)));
 }
コード例 #2
0
 /** Using $this->id get waContact and save it in $this->contact;
  * Load vars into $this->view specific to waContact. */
 protected function getContactInfo()
 {
     $system = wa();
     if ($this->id == $system->getUser()->getId()) {
         $this->contact = $system->getUser();
         $this->view->assign('own_profile', true);
     } else {
         $this->contact = new waContact($this->id);
         $this->view->assign('own_profile', false);
     }
     $exists = $this->contact->exists();
     if ($exists) {
         $this->view->assign('contact', $this->contact);
         // who created this contact and when
         $this->view->assign('contact_create_time', waDateTime::format('datetime', $this->contact['create_datetime'], $system->getUser()->getTimezone()));
         if ($this->contact['create_contact_id']) {
             try {
                 $author = new waContact($this->contact['create_contact_id']);
                 if ($author['name']) {
                     $this->view->assign('author', $author);
                 }
             } catch (Exception $e) {
                 // Contact not found. Ignore silently.
             }
         }
         $this->view->assign('top', $this->contact->getTopFields());
         // Main contact editor data
         $fieldValues = $this->contact->load('js', true);
         $m = new waContactModel();
         if (isset($fieldValues['company_contact_id'])) {
             if (!$m->getById($fieldValues['company_contact_id'])) {
                 $fieldValues['company_contact_id'] = 0;
                 $this->contact->save(array('company_contact_id' => 0));
             }
         }
         $contactFields = waContactFields::getInfo($this->contact['is_company'] ? 'company' : 'person', true);
         // Only show fields that are allowed in own profile
         if (!empty($this->params['limited_own_profile'])) {
             $allowed = array();
             foreach (waContactFields::getAll('person') as $f) {
                 if ($f->getParameter('allow_self_edit')) {
                     $allowed[$f->getId()] = true;
                 }
             }
             $fieldValues = array_intersect_key($fieldValues, $allowed);
             $contactFields = array_intersect_key($contactFields, $allowed);
         }
         contactsHelper::normalzieContactFieldValues($fieldValues, $contactFields);
         $this->view->assign('contactFields', $contactFields);
         $this->view->assign('contactFieldsOrder', array_keys($contactFields));
         $this->view->assign('fieldValues', $fieldValues);
         // Contact categories
         $cm = new waContactCategoriesModel();
         $this->view->assign('contact_categories', array_values($cm->getContactCategories($this->id)));
     } else {
         $this->view->assign('contact', array('id' => $this->id));
     }
     return $exists;
 }
コード例 #3
0
 public function execute()
 {
     // only allowed to global admin
     if (!wa()->getUser()->getRights('webasyst', 'backend')) {
         throw new waRightsException('Access denied.');
     }
     $app_id = waRequest::post('app_id');
     $name = waRequest::post('name');
     $value = (int) waRequest::post('value');
     $contact_id = waRequest::get('id');
     $has_backend_access_old = $this->hasBackendAccess($contact_id);
     if (!$name && !$value) {
         $values = waRequest::post('app');
         if (!is_array($values)) {
             throw new waException('Bad values for access rights.');
         }
     } else {
         $values = array($name => $value);
     }
     $right_model = new waContactRightsModel();
     $is_admin = $right_model->get($contact_id, 'webasyst', 'backend', false);
     if ($is_admin && $app_id != 'webasyst') {
         throw new waException('Cannot change application rights for global admin.');
     }
     // If $contact_id used to have limited access and we're changing global admin privileges,
     // then need to notify all applications to remove their custom access records.
     if (!$is_admin && $app_id == 'webasyst' && $name == 'backend') {
         foreach (wa()->getApps() as $aid => $app) {
             try {
                 if (isset($app['rights']) && $app['rights']) {
                     $app_config = SystemConfig::getAppConfig($aid);
                     $class_name = $app_config->getPrefix() . "RightConfig";
                     $file_path = $app_config->getAppPath('lib/config/' . $class_name . ".class.php");
                     $right_config = null;
                     if (!file_exists($file_path)) {
                         continue;
                     }
                     waSystem::getInstance($aid, $app_config);
                     include_once $file_path;
                     /**
                      * @var waRightConfig
                      */
                     $right_config = new $class_name();
                     $right_config->clearRights($contact_id);
                 }
             } catch (Exception $e) {
                 // silently ignore other applications errors
             }
         }
     }
     // Update $app_id access records
     $app_config = SystemConfig::getAppConfig($app_id);
     $class_name = $app_config->getPrefix() . "RightConfig";
     $file_path = $app_config->getAppPath('lib/config/' . $class_name . ".class.php");
     $right_config = null;
     if (file_exists($file_path)) {
         // Init app
         waSystem::getInstance($app_id, $app_config);
         include_once $file_path;
         /**
          * @var waRightConfig
          */
         $right_config = new $class_name();
     }
     foreach ($values as $name => $value) {
         if ($right_config && $right_config->setRights($contact_id, $name, $value)) {
             // If we've got response from custom rights config, then no need to update main rights table
             continue;
         }
         // Set default limited rights
         if ($right_config && $name == 'backend' && $value == 1) {
             /**
              * @var $right_config waRightConfig
              */
             foreach ($right_config->setDefaultRights($contact_id) as $n => $v) {
                 $right_model->save($contact_id, $app_id, $n, $v);
             }
         }
         $right_model->save($contact_id, $app_id, $name, $value);
     }
     waSystem::setActive('contacts');
     if ($contact_id) {
         // TODO: use waContact method for disabling
         $is_user = waRequest::post('is_user', null, 'int');
         if ($is_user === -1 || $is_user === 0 || $is_user === 1) {
             $contact = new waContact($contact_id);
             $contact->save(array('is_user' => $is_user));
             $this->response['access_disable_msg'] = contactsHelper::getAccessDisableMsg($contact);
         }
     }
     $has_backend_access_new = $this->hasBackendAccess($contact_id);
     if ($has_backend_access_new !== $has_backend_access_old) {
         if ($has_backend_access_new) {
             $this->logAction("grant_backend_access", null, $contact_id);
         } else {
             $this->logAction("revoke_backend_access", null, $contact_id);
         }
     }
 }
コード例 #4
0
 public function execute()
 {
     $this->id = (int) waRequest::post('id');
     // Check access
     if (!$this->id) {
         if (!$this->getRights('create')) {
             throw new waRightsException('Access denied.');
         }
     } else {
         $cr = new contactsRightsModel();
         if ($cr->getRight(null, $this->id) != 'write') {
             throw new waRightsException('Access denied.');
         }
     }
     $this->type = waRequest::post('type');
     $this->contact = new waContact($this->id);
     if ($this->type == 'company') {
         $this->contact['is_company'] = 1;
     }
     $data = json_decode(waRequest::post('data'), true);
     if (!$this->id && !isset($data['create_method'])) {
         $data['create_method'] = 'add';
     }
     $oldLocale = $this->getUser()->getLocale();
     // get old data for logging
     if ($this->id) {
         $old_data = array();
         foreach ($data as $field_id => $field_value) {
             $old_data[$field_id] = $this->contact->get($field_id);
         }
     }
     $response = array();
     if (!($errors = $this->contact->save($data, true))) {
         if ($this->id) {
             $new_data = array();
             foreach ($data as $field_id => $field_value) {
                 if (!isset($errors[$field_id])) {
                     $response[$field_id] = $this->contact->get($field_id, 'js');
                     $new_data[$field_id] = $this->contact->get($field_id);
                 }
             }
             if (empty($errors)) {
                 $this->logContactEdit($old_data, $new_data);
             }
             $response['name'] = $this->contact->get('name', 'js');
             $response['top'] = contactsHelper::getTop($this->contact);
             $response['id'] = $this->contact->getId();
         } else {
             $response = array('id' => $this->contact->getId());
             $response['address'] = $this->contact->get('address', 'js');
             $this->logAction('contact_add', null, $this->contact->getId());
         }
         // Update recently added menu item
         $name = waContactNameField::formatName($this->contact);
         if ($name || $name === '0') {
             $history = new contactsHistoryModel();
             $history->save('/contact/' . $this->contact->getId(), $name, $this->id ? null : 'add');
             $history = $history->get();
             // to update history in user's browser
         }
     }
     // Reload page with new language if user just changed it in own profile
     if ($this->contact->getId() == $this->getUser()->getId() && $oldLocale != $this->contact->getLocale()) {
         $response['reload'] = true;
     }
     $this->response = array('errors' => $errors, 'data' => $response);
     if (isset($history)) {
         $this->response['history'] = $history;
     }
 }
コード例 #5
0
 public function workupContacts(&$contacts)
 {
     if (!$contacts) {
         return array();
     }
     $contact_fields = array(array_keys(waContactFields::getAll('person', true)), array_keys(waContactFields::getAll('company', true)));
     foreach ($contacts as &$c) {
         $fields = $contact_fields[intval($c['is_company'])];
         $data = array('id' => $c['id']);
         foreach ($fields as $fld_id) {
             if (array_key_exists($fld_id, $c)) {
                 $data[$fld_id] = $c[$fld_id];
                 unset($c[$fld_id]);
             }
         }
         $c = array_merge($data, $c);
     }
     unset($c);
     // load that fields, that are top
     if ($this->getRequest()->request('top')) {
         foreach ($contacts as &$c) {
             $c['top'] = contactsHelper::getTop(new waContact($c['id']));
         }
         unset($c);
     }
 }
コード例 #6
0
 public function execute()
 {
     $this->prepare();
     if ($query = trim(waRequest::post('query'), '/')) {
         if (strpos($query, '/') === false) {
             $h = $hash = 'search/' . $query;
         } else {
             $h = $hash = $query;
             if (substr($hash, 0, 14) == 'import/results') {
                 $h = str_replace('import/results', 'import', $hash);
             }
         }
     } else {
         $h = $hash = '';
     }
     $h_parts = explode('/', $h, 2);
     $collection = new contactsCollection($h);
     $this->response['fields'] = array();
     $fields = '*,photo_url_32,photo_url_96';
     if ($h_parts[0] === 'users' || $h_parts[0] === 'group') {
         if (!wa()->getUser()->isAdmin()) {
             throw new waRightsException(_w('Access denied'));
         }
         $fields .= ',_access';
         $this->response['fields']['_access'] = array('id' => '_access', 'name' => _w('Access'), 'type' => 'Access', 'vertical' => true);
     }
     $collection->orderBy($this->sort, $this->order);
     $this->response['count'] = $collection->count();
     $view = waRequest::post('view');
     if ($view == 'list') {
         // Preload info to cache to avoid excess DB access
         $cm = new waCountryModel();
         $cm->preload();
     }
     $this->response['contacts'] = array_values($collection->getContacts($fields, $this->offset, $this->limit));
     $this->workupContacts($this->response['contacts']);
     $this->response['total_count'] = $collection->count();
     foreach ($this->response['contacts'] as $i => &$c) {
         $c['offset'] = $this->offset + $i;
     }
     unset($c);
     if ($view == 'list') {
         // Need to format field values correctly for this view.
         foreach ($this->response['contacts'] as &$cdata) {
             $c = new waContact($cdata['id']);
             $c->setCache($cdata);
             $data = $c->load('list,js') + $cdata;
             contactsHelper::normalzieContactFieldValues($data, waContactFields::getInfo($c['is_company'] ? 'company' : 'person', true));
             if (isset($data['photo'])) {
                 $data['photo'] = $c->getPhoto();
             }
             $c->removeCache(array_keys($cdata));
             $cdata = $data;
         }
         $this->response['fields'] = array_merge($this->response['fields'], contactsHelper::getFieldsDescription(array('title', 'name', 'photo', 'firstname', 'middlename', 'lastname', 'locale', 'timezone', 'jobtitle', 'company', 'sex', 'company_contact_id'), true));
         unset($cdata);
     } else {
         foreach ($this->response['contacts'] as &$cdata) {
             $cdata['name'] = waContactNameField::formatName($cdata);
             if ($cdata['name'] == $cdata['id']) {
                 $cdata['name'] = false;
             }
         }
         unset($cdata);
     }
     // for companies set name to company name
     // for contacts with empty name, set it to <no name>
     foreach ($this->response['contacts'] as &$c) {
         if (isset($c['name']) && trim($c['name'])) {
             continue;
         }
         if (isset($c['company']) && trim($c['company'])) {
             $c['name'] = $c['company'];
             unset($c['company']);
             continue;
         }
         $c['name'] = '<' . _w('no name') . '>';
     }
     unset($c);
     $title = $collection->getTitle();
     $hm = new contactsHistoryModel();
     if ($hash) {
         $type = explode('/', $hash);
         $hash = substr($hash, 0, 1) == '/' ? $hash : '/contacts/' . $hash;
         $type = $type[0];
         // if search query looks like a quick search then remove field name from header
         if ($type == 'search' && preg_match('~^/contacts/search/(name\\*=[^/]*|email\\*=[^/]*@[^/]*)/?$~i', $hash)) {
             $title = preg_replace("~^[^=]+=~", '', $title);
         }
         // save history
         if ($type == 'search') {
             $hm->save($hash, $title, $type, $this->response['count']);
             $this->logAction('search');
         }
         // Information about system category in categories view
         if (substr($hash, 0, 19) === '/contacts/category/') {
             $category_id = (int) substr($hash, 19);
             $cm = new waContactCategoryModel();
             $category = $cm->getById($category_id);
             if ($category && $category['system_id']) {
                 $this->response['system_category'] = $category['system_id'];
             }
         }
     }
     // Update history in user's browser
     $this->response['history'] = $hm->get();
     $this->response['title'] = $title;
 }