public function createAction() { if (!$this->request->isPost()) { return $this->_forward("producttypes/index"); } $producttypes = new ProductTypes(); $producttypes->id = $this->request->getPost("id", "int"); $producttypes->name = $this->request->getPost("name"); $producttypes->name = strip_tags($producttypes->name); if (!$producttypes->save()) { foreach ($producttypes->getMessages() as $message) { Flash::error((string) $message, "alert alert-error"); } return $this->_forward("producttypes/new"); } else { Flash::success("product types was created successfully", "alert alert-success"); return $this->_forward("producttypes/index"); } }
public function addXLS($file_path) { //$file_path = './upload_dir/test.xls'; $sheet_array = Yii::app()->yexcel->readActiveSheet($file_path); $current_product = null; foreach ($sheet_array as $row) { if ($this->isRowEmpty($row)) { continue; } if ($row['A'] != null && $row['B'] != null) { continue; } if ($row['A'] != null) { $current_product = ProductTypes::model()->findByAttributes(array('name' => $row['A'])); if (!$current_product) { $current_product = new ProductTypes(); } $current_product->name = $row['A']; $properties = '['; foreach ($row as $cell) { if ($cell != null && $cell != $row['A']) { $properties = "{$properties}\"{$cell}\","; } } $properties[strlen($properties) - 1] = ']'; $current_product->properties = $properties; $objValues = json_decode($current_product->values_); if (!$objValues) { $objValues = array(); $propCount = substr_count($current_product->properties, '","') + 1; for ($i = 0; $i < $propCount; $i++) { $objValues[$i] = array(); } } $current_product->values_ = json_encode($objValues); $current_product->save(); continue; } if (!$current_product) { continue; } //$cell = $row['B']; $product = Products::model()->findByAttributes(array('article' => $row['B'])); if (!$product) { $product = new Products(); } $product->type_id = $current_product->id; $product->article = $row['B']; //$cell = next($row); $product->description = $row['C']; //$cell = next($row); $product->prices = "[\"{$row['D']}\";\"{$row['E']}\";\"{$row['F']}\"]"; $values = '['; //$cell = $row['F']; //while ($cell = next($row)) $objValues = json_decode($current_product->values_, true); $i = 'H'; $nProp = 0; $isChanged = false; while ($row[$i]) { $values = "{$values}\"{$row[$i]}\","; if (is_array($objValues[$nProp]) && !in_array(strtolower($row[$i]), array_map('strtolower', $objValues[$nProp])) || !is_array($objValues[$nProp]) && $row[$i] != $objValues[$nProp]) { $objValues[$nProp][] = $row[$i]; $isChanged = true; } $i++; $nProp++; } $values[strlen($values) - 1] = ']'; $product->values_ = $values; if ($isChanged) { $current_product->values_ = json_encode($objValues, JSON_UNESCAPED_UNICODE); $current_product->save(); } $product->save(); } //$this->actionIndex(); }
public function createtypeAction() { $request = $this->request; if (!$request->isPost()) { return $this->forward("products/addtype"); } $request = $request->getPost(); $producttypes = new ProductTypes(); $producttypes->name = $request['name']; if ($producttypes->save()) { $this->flash->notice("保存成功!"); return $this->forward('products/addtype'); } else { foreach ($products->getMessages() as $message) { $this->flash->error((string) $message); } } }