public function add($org_id) { if ($this->validate()) { $product = new OrganizationsProducts(); $product->name = $this->name; $product->min_count = $this->min_count; $product->measure = $this->measure; $product->organization_id = $this->org_id; $product->price = $this->price; $product->product_rate = $this->product_rate; $product->articul = $this->articul; $product->category_id = $this->category_id; $product->product_description = $this->description; return $product->save(); } return false; }
public function parsePrices() { require_once Yii::getAlias('@frontend') . '/components/third_party/PHPExcel/IOFactory.php'; $this->reader = \PHPExcel_IOFactory::load($this->uploaded_file->tempName); $column_titles = []; foreach ($this->reader->getWorksheetIterator() as $worksheet) { $highestRow = $worksheet->getHighestRow(); $highestColumn = $worksheet->getHighestColumn(); $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn); for ($col = 0; $col < $highestColumnIndex; ++$col) { $cell = $worksheet->getCellByColumnAndRow($col, 1); $val = $cell->getValue(); if (!in_array($val, $this->all_titles)) { continue; } $column_titles[] = $val; } if (!$this->validateExcelFormat($column_titles)) { return false; } for ($row = 2; $row <= $highestRow; ++$row) { $product = []; $valid_row = true; for ($col = 0; $col < $highestColumnIndex; ++$col) { $title_cell = $worksheet->getCellByColumnAndRow($col, 1); $title_cell_value = $title_cell->getValue(); if (!in_array($title_cell_value, $this->all_titles)) { continue; } $cell = $worksheet->getCellByColumnAndRow($col, $row); $val = $cell->getValue(); if (in_array($title_cell_value, $this->required_titles) && empty($val)) { $valid_row = false; break; } $dataType = \PHPExcel_Cell_DataType::dataTypeForValue($val); $product[$column_titles[$col]] = $val; } if (!$valid_row) { continue; } $product['organization_id'] = $this->organization->id; $this->products_data[] = $product; } } $column_titles[] = 'organization_id'; $category_array = $this->addCategories(); foreach ($this->products_data as &$product) { foreach ($category_array as $id => $category_name) { if ($product['category'] == $category_name) { $product['category'] = $id; } } } return OrganizationsProducts::insertFromExcel($this->products_data, $column_titles); }