Пример #1
0
 public function getUnit()
 {
     if (empty($this->unit)) {
         $this->unit = Pimcore\Model\Object\QuantityValue\Unit::getById($this->unitId);
     }
     return $this->unit;
 }
 public function unitProxyAction()
 {
     if ($this->getParam("data")) {
         if ($this->getParam("xaction") == "destroy") {
             $data = Zend_Json::decode($this->getParam("data"));
             $id = $data["id"];
             $unit = \Pimcore\Model\Object\QuantityValue\Unit::getById($id);
             if (!empty($unit)) {
                 $unit->delete();
                 $this->_helper->json(array("data" => array(), "success" => true));
             } else {
                 throw new Exception("Unit with id " . $id . " not found.");
             }
         } elseif ($this->getParam("xaction") == "update") {
             $data = Zend_Json::decode($this->getParam("data"));
             $unit = Pimcore\Model\Object\QuantityValue\Unit::getById($data['id']);
             if (!empty($unit)) {
                 $unit->setValues($data);
                 $unit->save();
                 $this->_helper->json(array("data" => get_object_vars($unit), "success" => true));
             } else {
                 throw new Exception("Unit with id " . $data['id'] . " not found.");
             }
         } elseif ($this->getParam("xaction") == "create") {
             $data = Zend_Json::decode($this->getParam("data"));
             unset($data['id']);
             $unit = new Pimcore\Model\Object\QuantityValue\Unit();
             $unit->setValues($data);
             $unit->save();
             $this->_helper->json(array("data" => get_object_vars($unit), "success" => true));
         }
     } else {
         $list = new Pimcore\Model\Object\QuantityValue\Unit\Listing();
         $list->setOrder("asc");
         $list->setOrderKey("abbreviation");
         if ($this->getParam("dir")) {
             $list->setOrder($this->getParam("dir"));
         }
         if ($this->getParam("sort")) {
             $list->setOrderKey($this->getParam("sort"));
         }
         $list->setLimit($this->getParam("limit"));
         $list->setOffset($this->getParam("start"));
         $condition = "1 = 1";
         if ($this->getParam("filter")) {
             $filterString = $this->getParam("filter");
             $filters = json_decode($filterString);
             $db = \Pimcore\Db::get();
             foreach ($filters as $f) {
                 if ($f->type == "string") {
                     $condition .= " AND " . $db->getQuoteIdentifierSymbol() . $f->field . $db->getQuoteIdentifierSymbol() . " LIKE " . $db->quote("%" . $f->value . "%");
                 } elseif ($f->type == "numeric") {
                     $operator = $this->getOperator($f->comparison);
                     $condition .= " AND " . $db->getQuoteIdentifierSymbol() . $f->field . $db->getQuoteIdentifierSymbol() . " " . $operator . " " . $db->quote($f->value);
                 }
             }
             $list->setCondition($condition);
         }
         $list->load();
         $units = array();
         foreach ($list->getUnits() as $u) {
             $units[] = get_object_vars($u);
         }
         $this->_helper->json(array("data" => $units, "success" => true, "total" => $list->getTotalCount()));
     }
 }