partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 727fb832bdcdf9f11e5c73c751dd3bc41d4ceb94
parent 61a216d9e17a8fadc6d23333d7a6a93517d42f74
Author: Timo A. Hummel <timo@netraver.de>
Date:   Thu,  9 Jun 2011 03:27:06 +0200

Added unit editor

Diffstat:
Mfrontend/index.php | 6++++++
Mfrontend/js/Components/MenuBar.js | 13+++++++++++++
Afrontend/js/Components/Unit/UnitEditor.js | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Afrontend/js/Components/Unit/UnitEditorComponent.js | 21+++++++++++++++++++++
Afrontend/js/Components/Unit/UnitGrid.js | 14++++++++++++++
Afrontend/js/Models/SiPrefix.js | 11+++++++++++
Afrontend/js/Models/Unit.js | 14++++++++++++++
Mfrontend/js/PartDB2.js | 10++++++++++
Msrc/de/RaumZeitLabor/PartDB2/Part/Part.php | 2+-
Msrc/de/RaumZeitLabor/PartDB2/PartDB2.php | 4+++-
Asrc/de/RaumZeitLabor/PartDB2/SiPrefix/SiPrefix.php | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartDB2/SiPrefix/SiPrefixService.php | 29+++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartDB2/Unit/Unit.php | 104+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartDB2/Unit/UnitManager.php | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/de/RaumZeitLabor/PartDB2/Unit/UnitService.php | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtesting/SetupDatabase.php | 48++++++++++++++++++++++++++++++++++++++++++++++++
16 files changed, 637 insertions(+), 2 deletions(-)

diff --git a/frontend/index.php b/frontend/index.php @@ -57,6 +57,8 @@ <script type="text/javascript" src="js/Models/Part.js"></script> <script type="text/javascript" src="js/Models/PartUnit.js"></script> <script type="text/javascript" src="js/Models/PartDistributor.js"></script> + <script type="text/javascript" src="js/Models/Unit.js"></script> + <script type="text/javascript" src="js/Models/SiPrefix.js"></script> <script type="text/javascript" src="js/Models/StockEntry.js"></script> <script type="text/javascript" src="js/Models/PartManufacturer.js"></script> @@ -105,6 +107,10 @@ <script type="text/javascript" src="js/Components/PartUnit/PartUnitEditorComponent.js"></script> <script type="text/javascript" src="js/Components/PartUnit/PartUnitGrid.js"></script> + <script type="text/javascript" src="js/Components/Unit/UnitEditor.js"></script> + <script type="text/javascript" src="js/Components/Unit/UnitEditorComponent.js"></script> + <script type="text/javascript" src="js/Components/Unit/UnitGrid.js"></script> + <script type="text/javascript" src="js/Components/Statistics/CurrentStatisticsDialog.js"></script> <script type="text/javascript" src="js/Components/Part/PartsManager.js"></script> diff --git a/frontend/js/Components/MenuBar.js b/frontend/js/Components/MenuBar.js @@ -28,6 +28,10 @@ Ext.define('PartDB2.MenuBar', { text: i18n("Statistics"), handler: this.showStatistics, icon: 'resources/silkicons/chart_bar.png' + },{ + text: i18n("Units"), + handler: this.editUnits, + icon: 'resources/silkicons/lightbulb.png' }] }); @@ -53,6 +57,15 @@ Ext.define('PartDB2.MenuBar', { PartDB2.getApplication().addItem(j); j.show(); }, + editUnits: function () { + var j = Ext.create("PartDB2.UnitEditorComponent", { + title: i18n("Units"), + closable: true + }); + + PartDB2.getApplication().addItem(j); + j.show(); + }, editManufacturers: function () { var j = Ext.create("PartDB2.ManufacturerEditorComponent", { title: i18n("Manufacturers"), diff --git a/frontend/js/Components/Unit/UnitEditor.js b/frontend/js/Components/Unit/UnitEditor.js @@ -0,0 +1,74 @@ +Ext.define('PartDB2.UnitEditor', { + extend: 'PartDB2.Editor', + alias: 'widget.UnitEditor', + saveText: i18n("Save Unit"), + model: 'PartDB2.Unit', + initComponent: function () { + + var sm = Ext.create('Ext.selection.CheckboxModel',{ + checkOnly: true + }); + + this.gridPanel = Ext.create("Ext.grid.Panel", { + store: PartDB2.getApplication().getSiPrefixStore(), + selModel: sm, + columnLines: true, + columns: [ + { text: i18n("Prefix"), dataIndex: "prefix", width: 60 }, + { text: i18n("Symbol"), dataIndex: "symbol", width: 60 }, + { text: i18n("Power"), dataIndex: "power", flex: 1, renderer: function (val) { return "10<sup>"+val+"</sup>"; } } + ] + }); + + var container = Ext.create("Ext.form.FieldContainer", { + fieldLabel: i18n("Allowed SI-Prefixes"), + labelWidth: 150, + items: this.gridPanel + }); + + this.items = [{ + xtype: 'textfield', + name: 'name', + fieldLabel: i18n("Unit Name") + },{ + xtype: 'textfield', + name: 'symbol', + fieldLabel: i18n("Symbol") + }, + container]; + + this.callParent(); + + this.on("startEdit", this.onStartEdit, this); + }, + onStartEdit: function () { + var records = this.record.prefixes().getRange(); + + var toSelect = []; + var pfxStore = PartDB2.getApplication().getSiPrefixStore(); + + for (var i=0;i<records.length;i++) { + console.log(PartDB2.getApplication().getSiPrefixStore().find("id", records[i].get("id"))); + + toSelect.push(pfxStore.getAt(pfxStore.find("id", records[i].get("id")))); + } + + // @todo I don't like defer too much, can we fix that somehow? + Ext.defer(function () { this.gridPanel.getSelectionModel().select(toSelect); }, 100, this); + }, + onItemSave: function () { + + var selection = this.gridPanel.getSelectionModel().getSelection(); + var records = []; + for (var i=0;i<selection.length;i++) { + records.push(selection[i].get("id")); + } + + var call = new PartDB2.ServiceCall("Unit", "setUnitPrefixes"); + call.setParameter("prefixes", records); + call.setParameter("id", this.record.get("id")); + call.doCall(); + + this.callParent(); + } +}); diff --git a/frontend/js/Components/Unit/UnitEditorComponent.js b/frontend/js/Components/Unit/UnitEditorComponent.js @@ -0,0 +1,20 @@ +Ext.define('PartDB2.UnitEditorComponent', { + extend: 'PartDB2.EditorComponent', + alias: 'widget.UnitEditorComponent', + gridClass: 'PartDB2.UnitGrid', + editorClass: 'PartDB2.UnitEditor', + newItemText: i18n("New Unit"), + deleteMessage: i18n("Do you really wish to delete the unit'%s'?"), + deleteTitle: i18n("Delete Unit"), + initComponent: function () { + this.createStore({ + model: "Unit", + sorters: [{ + property: 'name', + direction:'ASC' + }] + }); + + this.callParent(); + } +});+ \ No newline at end of file diff --git a/frontend/js/Components/Unit/UnitGrid.js b/frontend/js/Components/Unit/UnitGrid.js @@ -0,0 +1,13 @@ +Ext.define('PartDB2.UnitGrid', { + extend: 'PartDB2.EditorGrid', + alias: 'widget.UnitGrid', + columns: [ + {header: i18n("Unit"), dataIndex: 'name', flex: 1}, + {header: i18n("Symbol"), dataIndex: 'symbol', width: 60} + ], + addButtonText: i18n("Add Unit"), + deleteButtonText: i18n("Delete Unit"), + initComponent: function () { + this.callParent(); + } +});+ \ No newline at end of file diff --git a/frontend/js/Models/SiPrefix.js b/frontend/js/Models/SiPrefix.js @@ -0,0 +1,10 @@ +PartDB2.SiPrefix = Ext.define("SiPrefix", { + extend: "Ext.data.Model", + fields: [ + { id: 'id', name: 'id', type: 'int' }, + { name: 'prefix', type: 'string'}, + { name: 'symbol', type: 'string'}, + { name: 'power', type: 'int'} + ], + proxy: PartDB2.getRESTProxy("SiPrefix") +});+ \ No newline at end of file diff --git a/frontend/js/Models/Unit.js b/frontend/js/Models/Unit.js @@ -0,0 +1,13 @@ +PartDB2.Unit = Ext.define("Unit", { + extend: "Ext.data.Model", + fields: [ + { id: 'id', name: 'id', type: 'int' }, + { name: 'name', type: 'string'}, + { name: 'symbol', type: 'string'} + ], + hasMany: { model: 'SiPrefix', name: 'prefixes'}, + proxy: PartDB2.getRESTProxy("Unit"), + getName: function () { + return this.get("name"); + } +});+ \ No newline at end of file diff --git a/frontend/js/PartDB2.js b/frontend/js/PartDB2.js @@ -36,6 +36,13 @@ Ext.application({ autoLoad: false }); + this.siPrefixStore = Ext.create("Ext.data.Store", + { + model: 'SiPrefix', + pageSize: -1, + autoLoad: true + }); + this.distributorStore = Ext.create("Ext.data.Store", { model: 'Distributor', @@ -72,6 +79,9 @@ Ext.application({ getDistributorStore: function () { return this.distributorStore; }, + getSiPrefixStore: function () { + return this.siPrefixStore; + }, /** * Reload all global stores each 100 seconds. * diff --git a/src/de/RaumZeitLabor/PartDB2/Part/Part.php b/src/de/RaumZeitLabor/PartDB2/Part/Part.php @@ -12,7 +12,7 @@ class Part { /** * @Id @Column(type="integer") * @GeneratedValue(strategy="AUTO") - * @var unknown_type + * @var integer */ private $id; diff --git a/src/de/RaumZeitLabor/PartDB2/PartDB2.php b/src/de/RaumZeitLabor/PartDB2/PartDB2.php @@ -183,7 +183,9 @@ class PartDB2 { 'de\RaumZeitLabor\PartDB2\Image\TempImage', 'de\RaumZeitLabor\PartDB2\Manufacturer\ManufacturerICLogo', 'de\RaumZeitLabor\PartDB2\Statistic\StatisticSnapshot', - 'de\RaumZeitLabor\PartDB2\Statistic\StatisticSnapshotUnit' + 'de\RaumZeitLabor\PartDB2\Statistic\StatisticSnapshotUnit', + 'de\RaumZeitLabor\PartDB2\SiPrefix\SiPrefix', + 'de\RaumZeitLabor\PartDB2\Unit\Unit' ); } diff --git a/src/de/RaumZeitLabor/PartDB2/SiPrefix/SiPrefix.php b/src/de/RaumZeitLabor/PartDB2/SiPrefix/SiPrefix.php @@ -0,0 +1,115 @@ +<?php +namespace de\RaumZeitLabor\PartDB2\SiPrefix; +declare(encoding = 'UTF-8'); + +use de\RaumZeitLabor\PartDB2\PartDB2, + de\RaumZeitLabor\PartDB2\Util\Exceptions\OutOfRangeException; + + +/** @Entity **/ +class SiPrefix { + /** + * @Id @Column(type="integer") + * @GeneratedValue(strategy="AUTO") + * @var integer + */ + private $id; + + /** + * The prefix of the Si-Prefix (e.g. yotta, deca, deci, centi) + * @Column(type="string") + * @var string + */ + private $prefix; + + /** + * The symbol of the Si-Prefix (e.g. m, M, G) + * @Column(type="string",length=2) + * @var string + */ + private $symbol; + + /** + * The power of the Si-Prefix (e.g. milli = 10^-3) + * @Column(type="integer") + * @var int + */ + private $power; + + /** + * Sets the prefix name. + * @param string $prefix + */ + public function setPrefix ($prefix) { + $this->prefix = $prefix; + } + + /** + * Returns the prefix name + * @return string The prefix name + */ + public function getPrefix () { + return $this->prefix; + } + + /** + * Sets the symbol for the prefix + * @param string $symbol The symbol + */ + public function setSymbol ($symbol) { + $this->symbol = $symbol; + } + + /** + * Returns the symbol for the prefix + * @return string The symbol + */ + public function getSymbol () { + return $this->symbol; + } + + /** + * Sets the power in a 10^n power (n=power) + * @param int $power The 10^power + */ + public function setPower ($power) { + $this->power = $power; + } + + /** + * Returns the power (10^n) + * @return int The power + */ + public function getPower () { + return $this->power; + } + + /** + * Returns the ID for this object. + * @param none + * @return int The ID for this object + */ + public function getId () { + return $this->id; + } + + /** + * Serializes the object into an array format. + * @return array the object in serialized format. + */ + public function serialize () { + return array( + "id" => $this->getId(), + "symbol" => $this->getSymbol(), + "prefix" => $this->getPrefix(), + "power" => $this->getPower()); + } + + /** + * Loads a prefix by ID + * @param int $id The ID to load + */ + public static function loadById ($id) { + return PartDB2::getEM()->find(get_called_class(), $id); + } +}+ \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartDB2/SiPrefix/SiPrefixService.php b/src/de/RaumZeitLabor/PartDB2/SiPrefix/SiPrefixService.php @@ -0,0 +1,29 @@ +<?php +namespace de\RaumZeitLabor\PartDB2\SiPrefix; +use de\RaumZeitLabor\PartDB2\Service\RestfulService; + +declare(encoding = 'UTF-8'); + +use de\RaumZeitLabor\PartDB2\Service\Service; +use de\RaumZeitLabor\PartDB2\PartDB2, + de\RaumZeitLabor\PartDB2\Session\SessionManager; + +class SiPrefixService extends Service implements RestfulService { + public function get () { + $query = PartDB2::getEM()->createQuery("SELECT si.id, si.prefix, si.symbol, si.power FROM de\RaumZeitLabor\PartDB2\SiPrefix\SiPrefix si"); + + return array("data" => $query->getArrayResult()); + } + + public function create () { + throw new \Exception("Not yet implemented"); + } + + public function update () { + throw new \Exception("Not yet implemented"); + } + + public function destroy () { + throw new \Exception("Not yet implemented"); + } +} diff --git a/src/de/RaumZeitLabor/PartDB2/Unit/Unit.php b/src/de/RaumZeitLabor/PartDB2/Unit/Unit.php @@ -0,0 +1,103 @@ +<?php +namespace de\RaumZeitLabor\PartDB2\Unit; +declare(encoding = 'UTF-8'); + +use de\RaumZeitLabor\PartDB2\PartDB2, + de\RaumZeitLabor\PartDB2\Util\Exceptions\OutOfRangeException, + de\RaumZeitLabor\PartDB2\SiPrefix\SiPrefix; + + +/** + * This object represents an unit. Units can be: Volt, Hertz etc. + * + * @Entity + **/ +class Unit { + /** + * @Id @Column(type="integer") + * @GeneratedValue(strategy="AUTO") + * @var integer + */ + private $id; + + /** + * The name of the unit (e.g. Volts, Ampere, Farad, Metres) + * @Column(type="string") + * @var string + */ + private $name; + + /** + * The symbol of the unit (e.g. V, A, F, m) + * @Column(type="string") + * @var string + */ + private $symbol; + + /** + * Defines the allowed SiPrefixes for this parameter unit + * @ManyToMany(targetEntity="de\RaumZeitLabor\PartDB2\SiPrefix\SiPrefix") + * @JoinTable(name="UnitSiPrefixes", + * joinColumns={@JoinColumn(name="unit_id", referencedColumnName="id")}, + * inverseJoinColumns={@JoinColumn(name="siprefix_id", referencedColumnName="id")} + * ) + * @var ArrayCollection + */ + private $prefixes; + + /** + * Creates a new Unit. + */ + public function __construct () { + $this->prefixes = new \Doctrine\Common\Collections\ArrayCollection(); + } + + /** + * Sets the name for this unit + * @param string $name the name for this unit + */ + public function setName ($name) { + $this->name = $name; + } + + /** + * Returns the name for this unit + * @return string The unit name + */ + public function getName () { + return $this->name; + } + + /** + * Sets the symbol for this unit + * @param string $symbol The symbol + */ + public function setSymbol ($symbol) { + $this->symbol = $symbol; + } + + /** + * Returns the symbol for this unit + * @return string The symbol + */ + public function getSymbol () { + return $this->symbol; + } + + /** + * Returns the si-prefix list for this unit + * @return array An array of SiPrefix objects + */ + public function getPrefixes () { + return $this->prefixes; + } + + /** + * Returns the ID for this object. + * @param none + * @return int The ID for this object + */ + public function getId () { + return $this->id; + } +}+ \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartDB2/Unit/UnitManager.php b/src/de/RaumZeitLabor/PartDB2/Unit/UnitManager.php @@ -0,0 +1,80 @@ +<?php +namespace de\RaumZeitLabor\PartDB2\Unit; +declare(encoding = 'UTF-8'); + +use de\RaumZeitLabor\PartDB2\Util\Singleton, + de\RaumZeitLabor\PartDB2\PartDB2, + de\RaumZeitLabor\PartDB2\Unit\Exceptions\UnitNotFoundException; + +class UnitManager extends Singleton { + public function getUnits ($start = 0, $limit = 10, $sort = "name", $dir = "asc", $filter = "") { + + $qb = PartDB2::getEM()->createQueryBuilder(); + $qb->select("u.id, u.name, u.symbol")->from("de\RaumZeitLabor\PartDB2\Unit\Unit","u"); + + if ($filter != "") { + $qb = $qb->where("u.name LIKE :filter"); + $qb->setParameter("filter", "%".$filter."%"); + } + + if ($limit > -1) { + $qb->setMaxResults($limit); + $qb->setFirstResult($start); + } + + $qb->orderBy("u.".$sort, $dir); + + $query = $qb->getQuery(); + + $result = $query->getResult(); + + $totalQueryBuilder = PartDB2::getEM()->createQueryBuilder(); + $totalQueryBuilder->select("COUNT(u.id)")->from("de\RaumZeitLabor\PartDB2\Unit\Unit","u"); + + + + if ($filter != "") { + $totalQueryBuilder = $totalQueryBuilder->where("u.name LIKE :filter"); + $totalQueryBuilder->setParameter("filter", "%".$filter."%"); + } + + $totalQuery = $totalQueryBuilder->getQuery(); + + foreach ($result as $key => $value) { + $result[$key]["prefixes"] = $this->getSiPrefixes($value["id"]); + } + + return array("data" => $result, "totalCount" => $totalQuery->getSingleScalarResult()); + } + + public function getSiPrefixes ($id) { + $unit = UnitManager::getInstance()->getUnit($id); + + $aData = array(); + + foreach ($unit->getPrefixes() as $prefix) { + $aData[] = $prefix->serialize(); + } + + return array("response" => array("totalCount" => count($aData), "data" => $aData)); + } + + public function getUnit ($id) { + $unit = PartDB2::getEM()->find("de\RaumZeitLabor\PartDB2\Unit\Unit", $id); + + if ($unit) { + return $unit; + } else { + throw new UnitNotFoundException(); + } + } + + public function deleteUnit ($id) { + $unit = $this->getUnit($id); + + PartDB2::getEM()->remove($unit); + PartDB2::getEM()->flush(); + } + + +}+ \ No newline at end of file diff --git a/src/de/RaumZeitLabor/PartDB2/Unit/UnitService.php b/src/de/RaumZeitLabor/PartDB2/Unit/UnitService.php @@ -0,0 +1,91 @@ +<?php +namespace de\RaumZeitLabor\PartDB2\Unit; +use de\RaumZeitLabor\PartDB2\Service\RestfulService; + +declare(encoding = 'UTF-8'); + +use de\RaumZeitLabor\PartDB2\Service\Service; +use de\RaumZeitLabor\PartDB2\PartDB2, + de\RaumZeitLabor\PartDB2\Part\PartUnit, + de\RaumZeitLabor\PartDB2\SiPrefix\SiPrefix, + de\RaumZeitLabor\PartDB2\Session\SessionManager; + +class UnitService extends Service implements RestfulService { + public function get () { + if ($this->hasParameter("id")) { + return UnitManager::getInstance()->getUnit($this->getParameter("id"))->serialize(); + } else { + if ($this->hasParameter("sort")) { + $tmp = json_decode($this->getParameter("sort"), true); + + $aSortParams = $tmp[0]; + } else { + $aSortParams = array( + "property" => "name", + "direction" => "ASC"); + } + return UnitManager::getInstance()->getUnits( + $this->getParameter("start", $this->getParameter("start", 0)), + $this->getParameter("limit", $this->getParameter("limit", 25)), + $this->getParameter("sortby", $aSortParams["property"]), + $this->getParameter("dir", $aSortParams["direction"]), + $this->getParameter("query", "")); + } + } + + public function setUnitPrefixes () { + $this->requireParameter("id"); + + $unit = UnitManager::getInstance()->getUnit($this->getParameter("id")); + + $prefixes = $unit->getPrefixes(); + + $prefixes->clear(); + + foreach ($this->getParameter("prefixes") as $prefix) { + $prefix = SiPrefix::loadById($prefix); + $prefixes->add($prefix); + } + + } + + public function create () { + $this->requireParameter("name"); + + $unit = new Unit; + + $this->setUnitData($unit); + + PartDB2::getEM()->persist($unit); + PartDB2::getEM()->flush(); + + return array("data" => $unit->serialize()); + } + + private function setUnitData (Unit $unit) { + $unit->setName($this->getParameter("name")); + $unit->setSymbol($this->getParameter("symbol")); + } + + public function update () { + $this->requireParameter("id"); + $this->requireParameter("name"); + + $unit = UnitManager::getInstance()->getUnit($this->getParameter("id")); + + $this->setUnitData($unit); + PartDB2::getEM()->flush(); + + return array("data" => $unit->serialize()); + + } + + public function destroy () { + $this->requireParameter("id"); + + UnitManager::getInstance()->deleteUnit($this->getParameter("id")); + + return array("data" => null); + } + +}+ \ No newline at end of file diff --git a/testing/SetupDatabase.php b/testing/SetupDatabase.php @@ -1,5 +1,9 @@ <?php namespace de\RaumZeitLabor\PartDB2\Tests; +use de\RaumZeitLabor\PartDB2\Unit\Unit; + +use de\RaumZeitLabor\PartDB2\SiPrefix\SiPrefix; + declare(encoding = 'UTF-8'); include("../src/de/RaumZeitLabor/PartDB2/PartDB2.php"); @@ -151,6 +155,50 @@ while ($store = mysql_fetch_assoc($r)) { echo "\n"; +/* Add Si-Prefixes */ +$data = \Symfony\Component\Yaml\Yaml::load("../setup/data/siprefixes.yaml"); + +$aSiPrefixes = array(); + +foreach ($data as $prefixName => $data) { + $prefix = new SiPrefix(); + $prefix->setPrefix($prefixName); + $prefix->setPower($data["power"]); + $prefix->setSymbol($data["symbol"]); + + $aSiPrefixes[$data["symbol"]] = $prefix; + PartDB2::getEM()->persist($prefix); + +} + +PartDB2::getEM()->flush(); + +/* Add units */ +$data = \Symfony\Component\Yaml\Yaml::load("../setup/data/units.yaml"); + +print_r($data); + +foreach ($data as $unitName => $data) { + $unit = new Unit(); + $unit->setName($unitName); + $unit->setSymbol($data["symbol"]); + + if (array_key_exists("prefixes", $data)) { + if (!is_array($data["prefixes"])) { + echo "Obacht ".$unitName." ist falsch\n"; + } + foreach ($data["prefixes"] as $prefix) { + $unit->getPrefixes()->add($aSiPrefixes[$prefix]); + } + } + + PartDB2::getEM()->persist($unit); + +} + +PartDB2::getEM()->flush(); + + /* Add manufacturers and IC logos */ $data = \Symfony\Component\Yaml\Yaml::load("../setup/data/manufacturers/manufacturers.yaml");