/**
  * Attach object to specific page and redirects to specified page
  *
  * @access public
  * @param void
  * @return null
  */
 function add_attachment()
 {
     $project = active_project();
     if (!$project instanceof Project) {
         flash_error(lang('project dnx'));
         $this->redirectToReferer(get_url('dashboard'));
     }
     // if
     if (!$project->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         $this->redirectToReferer(get_url('dashboard'));
     }
     // if
     $page_attachment = new PageAttachment();
     $page_attachment->setText(lang('description'));
     $page_attachment->setRelObjectId('0');
     $page_attachment->setRelObjectManager(array_var($_GET, 'rel_object_manager'));
     $page_attachment->setProjectId($project->getId());
     $page_attachment->setPageName(array_var($_GET, 'page_name'));
     $page_attachment->setOrder(array_var($_GET, 'order'));
     $page_attachment->save();
     PageAttachments::reorder(array_var($_GET, 'page_name'), $project);
     $this->redirectToReferer(get_url('dashboard'));
 }
 /**
  * Adds contact to project (as a PageAttachment)
  *
  * @param void
  * @return null
  */
 function add_contact()
 {
     if (!active_project()->canChangePermissions(logged_user())) {
         flash_error(lang('no access permissions'));
         $this->redirectToReferer(active_project()->getOverviewUrl());
     }
     // if
     $already_attached_contacts = PageAttachments::getAttachmentsByTypeAndProject(array('Contacts'), active_project());
     $already_attached_contacts_ids = null;
     if (is_array($already_attached_contacts)) {
         $already_attached_contacts_ids = array();
         foreach ($already_attached_contacts as $already_attached_contact) {
             $already_attached_contacts_ids[] = $already_attached_contact->getRelObjectId();
         }
         // foreach
     }
     // if
     $this->setTemplate('add_contact');
     $contact = new Contact();
     $im_types = ImTypes::findAll(array('order' => '`id`'));
     $contact_data = array_var($_POST, 'contact');
     if (!is_array($contact_data)) {
         $contact_data = array();
         // array
     }
     // if
     $existing_contact_data = array_var($contact_data, 'existing');
     if (!is_array($existing_contact_data)) {
         $existing_contact_data = array();
         // array
     }
     // if
     $new_contact_data = array_var($contact_data, 'new');
     if (!is_array($new_contact_data)) {
         $new_contact_data = array();
         // array
     }
     // if
     $company_data = array_var($new_contact_data, 'company');
     if (!is_array($company_data)) {
         $company_data = array();
         // array
     }
     // if
     $user_data = array_var($new_contact_data, 'user');
     if (!is_array($user_data)) {
         $user_data = array();
         // array
     }
     // if
     $project_init = array_var($_GET, 'project_init');
     tpl_assign('already_attached_contacts_ids', $already_attached_contacts_ids);
     tpl_assign('contact', $contact);
     tpl_assign('contact_data', $contact_data);
     tpl_assign('existing_contact_data', $existing_contact_data);
     tpl_assign('new_contact_data', $new_contact_data);
     tpl_assign('company_data', $company_data);
     tpl_assign('user_data', $user_data);
     tpl_assign('project_init', $project_init);
     tpl_assign('im_types', $im_types);
     tpl_assign('project', active_project());
     if (is_array(array_var($_POST, 'contact'))) {
         if (array_var($contact_data, 'what') == 'existing') {
             if (!Contacts::findById(array_var($existing_contact_data, 'rel_object_id')) instanceof Contact) {
                 tpl_assign('error', new FormSubmissionErrors(array(lang('existing contact required'))));
             } else {
                 $page_attachment = new PageAttachment();
                 $page_attachment->setFromAttributes($existing_contact_data);
                 $page_attachment->setRelObjectManager('Contacts');
                 $page_attachment->setProjectId(active_project()->getId());
                 $page_attachment->setPageName('people');
                 $page_attachment->save();
                 PageAttachments::reorder('people', active_project());
                 flash_success(lang('success add contact', $page_attachment->getObject()->getDisplayName()));
                 if ($project_init) {
                     $this->redirectToUrl(active_project()->getAddContactUrl(array('project_init' => '1')));
                 } else {
                     $this->redirectToUrl(get_url('project', 'people'));
                 }
                 // if
             }
             // if
         } else {
             // New contact
             // Save avatar
             $avatar = array_var($_FILES, 'new_avatar');
             if (is_array($avatar) && isset($avatar['size']) && $avatar['size'] != 0) {
                 try {
                     if (!isset($avatar['name']) || !isset($avatar['type']) || !isset($avatar['size']) || !isset($avatar['tmp_name']) || !is_readable($avatar['tmp_name'])) {
                         throw new InvalidUploadError($avatar, lang('error upload file'));
                     }
                     // if
                     $valid_types = array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png');
                     $max_width = config_option('max_avatar_width', 50);
                     $max_height = config_option('max_avatar_height', 50);
                     if ($avatar['size']) {
                         if (!in_array($avatar['type'], $valid_types) || !($image = getimagesize($avatar['tmp_name']))) {
                             throw new InvalidUploadError($avatar, lang('invalid upload type', 'JPG, GIF, PNG'));
                         } elseif (!$contact->setAvatar($avatar['tmp_name'], $max_width, $max_height, false)) {
                             throw new Error($avatar, lang('error edit avatar'));
                             $contact->setAvatarFile('');
                         }
                         // if
                     }
                     // if
                 } catch (Exception $e) {
                     flash_error($e->getMessage());
                 }
             } else {
                 $contact->setAvatarFile('');
             }
             // if
             try {
                 DB::beginWork();
                 $contact->setFromAttributes($new_contact_data);
                 if (array_var($company_data, 'what') == 'existing') {
                     $company_id = $new_contact_data['company_id'];
                 } else {
                     $company = new Company();
                     $company->setName(array_var($company_data, 'name'));
                     $company->setTimezone(array_var($company_data, 'timezone'));
                     $company->setClientOfId(owner_company()->getId());
                     $company->save();
                     $company_id = $company->getId();
                 }
                 // if
                 $contact->setCompanyId($company_id);
                 // User account info
                 if (array_var($user_data, 'add_account') == "yes") {
                     $user = new User();
                     $user->setFromAttributes($user_data);
                     if (array_var($user_data, 'password_generator') == 'random') {
                         // Generate random password
                         $password = substr(sha1(uniqid(rand(), true)), rand(0, 25), 13);
                     } else {
                         // Validate user input
                         $password = array_var($user_data, 'password');
                         if (trim($password) == '') {
                             throw new Error(lang('password value required'));
                         }
                         // if
                         if ($password != array_var($user_data, 'password_a')) {
                             throw new Error(lang('passwords dont match'));
                         }
                         // if
                     }
                     // if
                     $user->setPassword($password);
                     $user->save();
                     $contact->setUserId($user->getId());
                 } else {
                     $contact->setUserId(0);
                 }
                 // if
                 $contact->save();
                 if (plugin_active('tags')) {
                     $contact->setTagsFromCSV(array_var($new_contact_data, 'tags'));
                 }
                 $contact->clearImValues();
                 foreach ($im_types as $im_type) {
                     $value = trim(array_var($new_contact_data, 'im_' . $im_type->getId()));
                     if ($value != '') {
                         $contact_im_value = new ContactImValue();
                         $contact_im_value->setContactId($contact->getId());
                         $contact_im_value->setImTypeId($im_type->getId());
                         $contact_im_value->setValue($value);
                         $contact_im_value->setIsDefault(array_var($new_contact_data, 'default_im') == $im_type->getId());
                         $contact_im_value->save();
                     }
                     // if
                 }
                 // foreach
                 ApplicationLogs::createLog($contact, null, ApplicationLogs::ACTION_ADD);
                 $page_attachment = new PageAttachment();
                 $page_attachment->setFromAttributes($new_contact_data);
                 $page_attachment->setRelObjectId($contact->getId());
                 $page_attachment->setRelObjectManager('Contacts');
                 $page_attachment->setProjectId(active_project()->getId());
                 $page_attachment->setPageName('people');
                 $page_attachment->save();
                 PageAttachments::reorder('people', active_project());
                 DB::commit();
                 // Send notification...
                 try {
                     if (array_var($user_data, 'add_account') == "yes" && array_var($user_data, 'send_email_notification')) {
                         Notifier::newUserAccount($user, $password);
                     }
                     // if
                 } catch (Exception $e) {
                 }
                 // try
                 flash_success(lang('success add contact', $contact->getDisplayName()));
                 if ($project_init) {
                     $this->redirectToUrl(active_project()->getAddContactUrl(array('project_init' => '1')));
                 } else {
                     $this->redirectToUrl(get_url('project', 'people'));
                 }
                 // if
             } catch (Exception $e) {
                 DB::rollback();
                 tpl_assign('error', $e);
             }
             // try
         }
         // if
     }
     // if
 }