/** * Load all assets for the given material. * * @param integer $materialId * * @return \self */ public static function loadListMaterial($materialId) { $sql = ' SELECT `materialAssetId` FROM materialAssets WHERE `materialId` = ' . \sqlval($materialId) . ' AND !deleted ORDER BY percentage '; $materialAssetIds = query($sql, true); $obj = new self(); if (empty($materialAssetIds)) { return $obj; } $list = array(); foreach ($materialAssetIds as $materialAsset) { $list[$materialAsset['materialAssetId']] = \Model\MaterialAsset::loadById($materialAsset['materialAssetId']); } $obj->setList($list); return $obj; }
/** * Create a new material and the material assets from the given array. * array( * 'name' => 'test', * 'materialTypeId' => 1, * 'additional' => 'one additional', * 'percentage' => array( * 0 => 50, * 1 => 100, * ), * 'timeFactor' => array( * 0 => 1, * 1 => 1, * ), * 'priceFactor' => array( * 0 => 2, * 0 => 1.5, * ), * 'priceWeight' => array(), * 'currency' => array( * 0 => 'S', * 1 => 'D', * ), * 'proof' => array( * 0 => 0, * 1 => -1, * ), * 'breakFactor' => array( * 0 => 1, * 1 => -1, * ), * 'hitPoints' => array( * 0 => 0, * 1 => 1, * ), * 'armor' => array( * 0 => 0, * 1 => 0, * ), * 'weaponModificator' => array( * 0 => '0/0', * 1 => '-1/0', * ), * ) * * @param array $data * * @return boolean */ public static function create($data) { if (!$data['name'] || !$data['materialTypeId']) { return false; } $additional = array(); $data['additional'] = trim($data['additional']); if (!empty($data['additional'])) { preg_match_all('/(\\w.*?) +(\\w.*?)(?=(\\r\\n|\\r|\\n))/m', trim($data['additional']), $matches, PREG_SET_ORDER); foreach ($matches as $match) { $additional[$match[1]] = $match[2]; } } $sql = ' INSERT INTO materials SET materialTypeId = ' . \sqlval($data['materialTypeId']) . ', name = ' . \sqlval($data['name']) . ', additional = ' . \sqlval(json_encode($additional)) . ' '; $id = \query($sql); foreach ($data['percentage'] as $key => $value) { \Model\MaterialAsset::create(array('materialId' => $id, 'percentage' => $value, 'timeFactor' => $data['timeFactor'][$key], 'priceFactor' => $data['priceFactor'][$key], 'priceWeight' => $data['priceWeight'][$key], 'currency' => $data['currency'][$key], 'proof' => $data['proof'][$key], 'breakFactor' => $data['breakFactor'][$key], 'hitPoints' => $data['hitPoints'][$key], 'armor' => $data['armor'][$key], 'weaponModificator' => $data['weaponModificator'][$key])); } return true; }
/** * Compare method for sorting the material assets along their percentages * * @param \Model\MaterialAsset $a * @param \Model\MaterialAsset $b * * @return int */ public static function compareMaterialAssetsPercentage($a, $b) { if ($a->getPercentage() == $b->getPercentage()) { return 0; } return $a->getPercentage() < $b->getPercentage() ? +1 : -1; }