/** * Method to toggle a value, including integer values * * @access public * @param string $fieldName The field to increment. * @param integer $pk The id of the item. * @param integer $max Max possible values (modulo). Reset to 0 when the value is superior to max. * * @return boolean True when changed. False if error. * * @since Cook 2.0 */ public function toggle($fieldName, $pk = null, $max = 1) { // If field do not exists return false if (!property_exists($this, $fieldName)) { return false; } $this->load($pk); //Calculate the new value $value = $this->{$fieldName} + 1; if ($value > $max) { $value = 0; } // Check the row in by primary key. $query = $this->_db->getQuery(true); $query->update($this->_tbl); $query->set(qn($this->_db, $fieldName) . ' = ' . (int) $value); $query->where($this->_tbl_key . ' = ' . $this->_db->quote($pk)); $this->_db->setQuery($query); // Check for a database error. if (!$this->_db->query()) { return false; } // Set table values in the object. $this->fieldName = $value; return true; }
/** * Method to set default to the item. * * @access public * @param int $id Id of the item to become default. * @param varchar $field Default field name. * @param string $where Distinct the defaulting basing on this condition. * * @return boolean True on success. False if error. */ public function makeDefault($id, $field = 'default', $where = '') { $table = $this->getTable(); if (!$table->load($id)) { return false; } if (!$this->canEditDefault($table)) { return false; } $pk = $table->getKeyName(); $db = $this->getDbo(); $query = $db->getQuery(true); $query->update(qn($db, $table->getTableName())); $query->set(qn($db, $field) . ' = (' . qn($db, $pk) . ' = ' . (int) $id . ' )'); if (trim($where) != '') { $query->where($where); } $db->setQuery($query); $db->query(); return true; }
/** * Method to test all common rules (Required, Unique). * * @access public static * @param JXMLElement &$element The JXMLElement object representing the <field /> tag for the form field object. * @param mixed $value The form field value to validate. * @param string $group The field name group control value. This acts as as an array container for the field. * @param JRegistry &$input An optional JRegistry object with the entire data set to validate against the entire form. * @param object &$form The form object for which the field is being tested. * * @return boolean True if the value is valid, false otherwise. * * @since Cook V2.0 */ public static function testDefaults(&$element, $value, $group = null, &$input = null, &$form = null) { // If the field is empty and not required, the field is valid. $required = (string) $element['required'] == 'true' || (string) $element['required'] == 'required'; if ($required && empty($value)) { return false; } // Check if we should test for uniqueness. $unique = (string) $element['unique'] == 'true' || (string) $element['unique'] == 'unique'; if ($unique) { $jinput = JFactory::getApplication()->input; $parts = explode(".", $form->getName()); $extension = preg_replace("/com_/", "", $parts[0]); $table = JTable::getInstance($parts[1], $extension . 'Table', array()); // Get the database object and a new query object. $db = JFactory::getDBO(); $query = $db->getQuery(true); $id = $jinput->get('cid', 0, 'array'); if (count($id)) { $id = $id[0]; } if (in_array($jinput->get('task'), array('save2copy'))) { $id = 0; } // Build the query. $query->select('COUNT(*)'); $query->from($table->getTableName()); $query->where(qn($db, (string) $element['name']) . ' = ' . $db->quote($value)); $query->where(qn($db, 'id') . '<>' . (int) $id); // Set and query the database. $db->setQuery($query); $duplicate = (bool) $db->loadResult(); // Check for a database error. if ($db->getErrorNum()) { JError::raiseWarning(500, $db->getErrorMsg()); return false; } if ($duplicate) { JError::raiseWarning(1201, JText::sprintf("RTIPRINT_MODEL_DUPLICATE_ENTRY_FOR", JText::_($element['label']))); return false; } } return true; }