/** * @param array $args (name, type, target, value) * @throws InvalidConditionException */ public function __construct(array $args) { $this->args = $args; if (isMultiArray($args)) { throw new InvalidConditionException("Multi dimensional array is not supported."); } else { $this->validate($this->args); } }
/** * 城市区三级联动中的 AJAX 部分 */ public function pcAjaxAction() { Helper::import('Array'); $flag = $this->getQuery('flag'); $targetID = $this->getQuery('targetID'); switch ($flag) { case 'p': $function = 'getCityByProvinceId'; $obj = $this->m_city; break; case 'c': $function = 'getRegionByCityId'; $obj = $this->m_region; break; } $finalArr = $obj->{$function}($targetID); $finalStr = ''; switch ($flag) { case 'p': $finalStr = "<select errorDiv=\"city_error\" tipsDiv=\"city_tips\" eleType='select' id='" . $this->cityID . "' name='" . $this->cityID . "' class='select_box selecttxt3' onchange='javascript:cityChange();' >"; $finalStr .= "<option value=''>请选择城市</option>"; if (isMultiArray($finalArr)) { foreach ($finalArr as $val) { $finalStr .= "<option value=" . $val['cityID'] . ">" . $val['city'] . "</option>"; } } else { $finalStr .= "<option value=" . $finalArr['cityID'] . ">" . $finalArr['city'] . "</option>"; } $finalStr .= "</select>"; break; case 'c': $finalStr = "<select errorDiv=\"region_error\" eleType='select' tipsDiv=\"city_tips\" id='" . $this->regionID . "' name='" . $this->regionID . "' class='select_box selecttxt3' onchange='javascript:regionChange();'>"; $finalStr .= "<option value=''>请选择区域</option>"; if (isMultiArray($finalArr)) { foreach ($finalArr as $val) { $finalStr .= "<option value=" . $val['regionID'] . ">" . $val['region'] . "</option>"; } } else { $finalStr .= "<option value=" . $finalArr['regionID'] . ">" . $finalArr['region'] . "</option>"; } $finalStr .= "</select>"; break; } echo $finalStr; die; }
/** * Search the info from the DB * * Uses a smarter search engine to search through the fields and return certain fields, puts the results in a local $results array * * @param $keywords A string that we are going to be searching the DB for * @param $search_fields An array of the field keys that we are going to be searching through * @param $return_fields An array of the field keys that are going to be returned * @return int */ public function Search($keywords, $search_fields, $return_fields) { // Use the global db class global $db; $returns = array(); // Push $this into the array array_unshift($this->join_class, $this); array_unshift($this->join_type, ''); array_unshift($this->join_on, ''); // Setup the return fields if (!isMultiArray($return_fields)) { array_unshift($returns, $return_fields); } else { $returns = $return_fields; } // Split up the terms $terms = search_split_terms($keywords); $terms_db = search_db_escape_terms($terms); $terms_rx = search_rx_escape_terms($terms); // Create list of statements $parts = array(); foreach ($terms_db as $term_db) { if (is_array($search_fields)) { foreach ($search_fields as $field) { $parts[] = '`' . $this->database . '`.`' . $this->table . '`.' . $field . ' RLIKE \'' . $term_db . '\''; } } else { $parts[] = '`' . $this->database . '`.`' . $this->table . '`.' . $search_fields . ' RLIKE \'' . $term_db . '\''; } } $parts = '(' . implode(' OR ', $parts) . ')'; if ($this->conditions != '') { $parts .= ' AND ' . $this->conditions; } // Loop through all the joined classes foreach ($this->join_class as $key => $class) { // Create the return fields if (is_array($returns[$key]) && count($returns[$key]) > 0) { // Require primary key returned if (!in_array($class->primary, $returns[$key])) { $return .= '`' . $class->database . '`.`' . $class->table . '`.' . $class->primary . ', '; } // List all other fields to be returned foreach ($returns[$key] as $field) { if (!is_array($field)) { $return .= trim($field) != '' ? '`' . $class->database . '`.`' . $class->table . '`.' . $field . ', ' : ''; } else { foreach ($field as $sub_field) { $return .= trim($sub_field) != '' ? '`' . $class->database . '`.`' . $class->table . '`.' . $sub_field . ', ' : ''; } } } } else { // Local class return values if (is_array($returns[$key])) { foreach ($returns[$key] as $field) { $fields[] = '`' . $this->database . '`.`' . $this->table . '`.' . $field; } $return .= implode(', ', $fields); } else { $return .= '`' . $class->database . '`.`' . $class->table . '`.*, '; } } // Create the Joins if ($key > 0) { $join .= ' ' . $this->join_type[$key] . ' JOIN `' . $this->join_class[$key]->database . '`.`' . $this->join_class[$key]->table . '` ON (`' . $this->join_class[$key]->database . '`.`' . $this->join_class[$key]->table . '`.' . $this->join_on[$key][0] . ' = `' . $this->database . '`.`' . $this->table . '`.' . $this->join_on[$key][1] . ') '; } } $query = 'SELECT ' . substr($return, 0, -2) . ' FROM `' . $this->database . '`.`' . $this->table . '`' . $join . ' WHERE ' . $parts; $query .= $this->group_by != '' ? ' GROUP BY ' . $this->group_by : ''; $result = $db->Query($query, $this->database); $results = array(); while ($info = $db->FetchArray($result)) { $info['score'] = 0; foreach ($terms_rx as $term_rx) { if (is_array($search_fields)) { foreach ($search_fields as $field) { $info['score'] += preg_match_all("/{$term_rx}/i", $info[$field], $null); } } else { $info['score'] += preg_match_all("/{$term_rx}/i", $info[$search_fields], $null); } } $results[] = $info; } // Pop $this from the array array_shift($this->join_class); array_shift($this->join_type); array_shift($this->join_on); uasort($results, 'search_sort_results'); $this->results = $results; return count($results); }
function ul($list, $ID = NULL, $class = NULL) { $ID = !is_null($ID) ? ' id="' . $ID . '"' : NULL; $class = !is_null($class) ? ' class="' . $class . '"' : NULL; $HTML = '<ul' . $ID . $class . '>' . char("\t"); if (isMultiArray($list)) { foreach ($list as $li) { $class = isset($li["class"]) ? ' class="' . $li["class"] . '"' : NULL; $HTML .= char("\t", 2) . '<li' . $class . '>' . $li["item"] . '</li>' . char("\n"); } } elseif (is_array($list)) { for ($i = 0; $i <= count($list) - 1; $i++) { $HTML .= char("\t", 2) . '<li>' . $list[$i] . '</li>' . char("\n"); } } $HTML .= char("\t") . '</ul>' . char("\n"); return $HTML; }
/** * Add item to the cart, it can be an array or multi dimensional array * * @param string|array $id * @param string $name * @param float $price * @param int $quantity * @param array $attributes * @param CartCondition|array $conditions * * @return $this * * @throws InvalidItemException */ public function add($id, $name = null, $price = null, $quantity = null, $attributes = [], $conditions = []) { // TODO Sift id, name, price and quantity and add remainings to the attirbutes array. // If the first argument is an array, we will need to call add again if (is_array($id)) { // The first argument is an array, now we will need to check if it is a multi dimensional // array, if so, we will iterate through each item and call add again if (isMultiArray($id)) { foreach ($id as $item) { $this->add($item["id"], $item["name"], $item["price"], $item["quantity"], issetAndHasValueOrAssignDefault($item["attributes"], []), issetAndHasValueOrAssignDefault($item["conditions"], [])); } } else { $this->add($id["id"], $id["name"], $id["price"], $id["quantity"], issetAndHasValueOrAssignDefault($id["attributes"], []), issetAndHasValueOrAssignDefault($id["conditions"], [])); } return $this; } // Validate data $item = $this->validate(["id" => $id, "name" => $name, "price" => normalizePrice($price), "quantity" => $quantity, "attributes" => new ItemAttributeCollection($attributes), "conditions" => $conditions]); // Get the cart $cart = $this->getContent(); // If the item is already in the cart we will just update it if ($cart->has($id)) { Event::fire(new Events\ItemsUpdating($this, [$item])); // Check if $item changed $this->update($id, $item); Event::fire(new Events\ItemsUpdated($this, [$item])); } else { Event::fire(new Events\ItemsAdding($this, [$item])); // Check if $item changed $this->addRow($id, $item); Event::fire(new Events\ItemsAdded($this, [$item])); } return $this; }
function ul($list, $ID = null, $class = null) { $ID = !is_null($ID) ? ' id="' . $ID . '"' : null; $class = !is_null($class) ? ' class="' . $class . '"' : null; $HTML = '<ul' . $ID . $class . '>'; if (isMultiArray($list)) { foreach ($list as $li) { $class = isset($li["class"]) ? ' class="' . $li["class"] . '"' : null; $HTML .= '<li' . $class . '>' . $li["item"] . '</li>'; } } elseif (is_array($list)) { for ($i = 0; $i <= count($list) - 1; $i++) { $HTML .= '<li>' . $list[$i] . '</li>'; } } return $HTML . '</ul>'; }