function update($vars, &$errors) { $valid = true; $forms = $this->getForms($vars); foreach ($forms as $cd) { if (!$cd->isValid()) { $valid = false; } if ($cd->get('type') == 'O' && ($form = $cd->getForm($vars)) && ($f = $form->getField('name')) && $f->getClean() && ($o = Organization::lookup(array('name' => $f->getClean()))) && $o->id != $this->getId()) { $valid = false; $f->addError(__('Organization with the same name already exists')); } } if ($vars['domain']) { foreach (explode(',', $vars['domain']) as $d) { if (!Validator::is_email('t@' . trim($d))) { $errors['domain'] = __('Enter a valid email domain, like domain.com'); } } } if ($vars['manager']) { switch ($vars['manager'][0]) { case 's': if ($staff = Staff::lookup(substr($vars['manager'], 1))) { break; } case 't': if ($vars['manager'][0] == 't' && ($team = Team::lookup(substr($vars['manager'], 1)))) { break; } default: $errors['manager'] = __('Select an agent or team from the list'); } } if (!$valid || $errors) { return false; } foreach ($this->getDynamicData() as $cd) { if (($f = $cd->getForm()) && $f->get('type') == 'O' && ($name = $f->getField('name'))) { $this->name = $name->getClean(); $this->save(); } $cd->setSource($vars); if ($cd->save()) { $this->updated = SqlFunction::NOW(); } } // Set flags foreach (array('collab-all-flag' => Organization::COLLAB_ALL_MEMBERS, 'collab-pc-flag' => Organization::COLLAB_PRIMARY_CONTACT, 'assign-am-flag' => Organization::ASSIGN_AGENT_MANAGER) as $ck => $flag) { if ($vars[$ck]) { $this->setStatus($flag); } else { $this->clearStatus($flag); } } // Set staff and primary contacts $this->set('domain', $vars['domain']); $this->set('manager', $vars['manager'] ?: ''); if ($vars['contacts'] && is_array($vars['contacts'])) { foreach ($this->allMembers() as $u) { $u->setPrimaryContact(array_search($u->id, $vars['contacts']) !== false); $u->save(); } } // Send signal for search engine updating if not modifying the // fields specific to the organization if (count($this->dirty) === 0) { Signal::send('model.updated', $this); } return $this->save(); }
/** * Function: __next * * Internal implementation of the next number generator. This method * will lock the database object backing to protect against concurent * ticket processing. The lock will be released at the conclusion of the * session. * * Parameters: * $digits - (int:optional) number of digits (size) of the number. This * is useful for random sequences which need a size hint to * generate a "next" value. * * Returns: * (int) - The current number in the sequence. The sequence is advanced * and assured to be session-wise atomic before the value is returned. */ function __next($digits = false) { // Ensure this block is executed in a single transaction db_autocommit(false); // Lock the database object -- this is important to handle concurrent // requests for new numbers static::objects()->filter(array('id' => $this->id))->lock()->one(); // Increment the counter $next = $this->next; $this->next += $this->increment; $this->updated = SqlFunction::NOW(); $this->save(); db_autocommit(true); return $next; }
function updateInfo($vars, &$errors, $staff = false) { $valid = true; $forms = $this->getForms($vars); foreach ($forms as $cd) { $cd->setSource($vars); if ($staff && !$cd->isValidForStaff()) { $valid = false; } elseif (!$staff && !$cd->isValidForClient()) { $valid = false; } elseif (($form = $cd->getForm()) && $form->get('type') == 'U' && ($f = $form->getField('email')) && $f->getClean() && ($u = User::lookup(array('emails__address' => $f->getClean()))) && $u->id != $this->getId()) { $valid = false; $f->addError(__('Email is assigned to another user')); } } if (!$valid) { return false; } foreach ($forms as $cd) { if (($f = $cd->getForm()) && $f->get('type') == 'U') { if ($name = $f->getField('name')) { $this->name = $name->getClean(); } if ($email = $f->getField('email')) { $this->default_email->address = $email->getClean(); $this->default_email->save(); } } // DynamicFormEntry::save returns the number of answers updated if ($cd->save()) { $this->updated = SqlFunction::NOW(); } } return $this->save(); }