Esempio n. 1
0
 public function testSqlFunctions()
 {
     $f = new SqlFunction('substring', array(new Field('name'), 5, 2));
     $params = array();
     $this->assertEquals('SUBSTRING(`t0`.`name`, 5, 2)', $f->getSql($params));
     $this->assertEquals('substring', $f->getName());
     try {
         $f = new SqlFunction('there_is_no_such_function', new Field('name'));
         $this->fail();
     } catch (InvalidArgumentException $e) {
     }
     try {
         $f = new SqlFunction('substring', new AndOp(array()));
         $this->fail('wrong value accepted');
     } catch (InvalidArgumentException $e) {
     }
     $q = new SelectQuery('test');
     $q->setWhere(new Condition('>', new SqlFunction('length', new Field('foo')), 5));
     $params = array();
     $this->assertEquals('SELECT `t0`.* FROM `test` AS `t0` WHERE LENGTH(`t0`.`foo`) > :p1', $q->sql());
     $parameters = $q->parameters();
     $this->assertEquals('5', $parameters[':p1']);
 }
Esempio n. 2
0
 /**
  * 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;
 }
Esempio n. 3
0
 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();
 }
 protected function getSqlFunction(EntityQueryBuilder $entityQueryBuilder)
 {
     return SqlFunction::aggregateDistinct($this->getFunc($entityQueryBuilder), $this->getValueExpression($entityQueryBuilder));
 }
Esempio n. 5
0
 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();
 }