*/ /** * Klasa stProducerWebApi * * @package stWebApiPlugin * @subpackage libs */ class stProducerWebApi extends autoStProducerWebApi { public static function getLink(Producer $producer, StProducerWebApi $api) { return $api->__getProducerUrl($producer); } public function AddProducer($object) { if (isset($object->_culture)) $this->__setCulture($object->_culture); stWebApi::getLogin($object->_session_hash, 'webapi_write'); $this->TestAndValidateAddProducerFields($object); $item = new Producer(); if ($item) { $this->setFieldsForAddProducer($object, $item); try { $item->save(); if (isset($object->image) && isset($object->image_filename)) $this->setProducerImage($item, $object->image_filename, $object->image); if (isset($object->url)) { $item->setUrl($object->url); $item->save(); } } catch (Exception $e) { throw new SoapFault('2', sprintf($this->__(WEBAPI_ADD_ERROR), $e->getMessage())); } $object = new StdClass(); $this->getFieldsForAddProducer($object, $item); return $object; } else { throw new SoapFault('1', $this->__(WEBAPI_ADD_ERROR)); } } public function TestAndValidateAddProducerFields($object) { parent::TestAndValidateAddProducerFields($object); $this->validateProducerName($object); } protected function validateProducerName($object) { $c = new Criteria(); // $c->addJoin(ProducerPeer::ID, ProducerI18nPeer::ID); $c->add(ProducerI18nPeer::NAME, $object->name); $c->add(ProducerI18nPeer::CULTURE, $this->__getCulture()); if (isset($object->id) && $object->id) { $c->add(ProducerI18nPeer::ID, $object->id, Criteria::NOT_EQUAL); } if (ProducerI18nPeer::doCount($c) > 0) { throw new SoapFault("3", sprintf($this->__(WEBAPI_VALIDATE_UNIQUE_ERROR), 'name')); } } public function setProducerImage($item, $filename, $image) { $tmpFile = sfConfig::get('sf_cache_dir') . '/webapi_producer.tmp'; if (is_object($item->getSfAsset())) { $item->getSfAsset()->delete(); $item->setSfAsset(null); } file_put_contents($tmpFile, base64_decode($image)); $item->createAsset($item->getId() . '.' . pathinfo($filename, PATHINFO_EXTENSION), $tmpFile); $item->save(); } public function UpdateProducer($object) { if (isset($object->_culture)) $this->__setCulture($object->_culture); stWebApi::getLogin($object->_session_hash, 'webapi_write'); $this->TestAndValidateUpdateProducerFields($object); $item = ProducerPeer::retrieveByPk($object->id); if (is_object($item)) { $this->setFieldsForUpdateProducer($object, $item); try { $item->save(); if (isset($object->image) && isset($object->image_filename)) $this->setProducerImage($item, $object->image_filename, $object->image); } catch (Exception $e) { throw new SoapFault('2', sprintf($this->__(WEBAPI_UPDATE_ERROR), $e->getMessage())); } $object = new StdClass(); $object->_update = 1; return $object; } else { throw new SoapFault("1", $this->__(WEBAPI_INCORRECT_ID)); } } public function TestAndValidateUpdateProducerFields($object) { parent::TestAndValidateUpdateProducerFields($object); if (isset($object->name)) { $this->validateProducerName($object); } } public function GetProducer($object) { if (isset($object->_culture)) $this->__setCulture($object->_culture); stWebApi::getLogin($object->_session_hash, 'webapi_read'); $this->TestAndValidateGetProducerFields($object); $item = ProducerPeer::retrieveByPk($object->id); if ($item) { $object = new StdClass(); $this->getFieldsForGetProducer($object, $item); if (is_object($item->getSfAsset())) { $object->image_filename = basename($item->getSfAsset()->getPath()); $object->image = base64_encode(file_get_contents(sfConfig::get('sf_web_dir') . '/' . $item->getSfAsset()->getPath())); } return $object; } else { throw new SoapFault('1', $this->__(WEBAPI_INCORRECT_ID)); } } public function GetProducerList($object) { if (isset($object->_culture)) $this->__setCulture($object->_culture); stWebApi::getLogin($object->_session_hash, 'webapi_read'); $this->TestAndValidateGetProducerListFields($object); $c = new Criteria(); if (isset($object->_modified_from) && isset($object->_modified_to)) { $criterion = $c->getNewCriterion(ProducerPeer::UPDATED_AT, $object->_modified_from, Criteria::GREATER_EQUAL); $criterion->addAnd($c->getNewCriterion(ProducerPeer::UPDATED_AT, $object->_modified_to, Criteria::LESS_EQUAL)); $c->add($criterion); } else { if (isset($object->_modified_from)) { $criterion = $c->getNewCriterion(ProducerPeer::UPDATED_AT, $object->_modified_from, Criteria::GREATER_EQUAL); $c->add($criterion); } if (isset($object->_modified_to)) { $criterion = $c->getNewCriterion(ProducerPeer::UPDATED_AT, $object->_modified_to, Criteria::LESS_EQUAL); $c->add($criterion); } } if (!isset($object->_limit)) $object->_limit = 20; $c->setLimit($object->_limit); $c->setOffset($object->_offset); $items = ProducerPeer::doSelect($c); if ($items) { $itemsArray = array(); foreach ($items as $item) { $object = new StdClass(); $this->getFieldsForGetProducerList($object, $item); if (is_object($item->getSfAsset())) { $object->image_filename = basename($item->getSfAsset()->getPath()); $object->image = base64_encode(file_get_contents(sfConfig::get('sf_web_dir') . '/' . $item->getSfAsset()->getPath())); } $itemsArray[] = $object; } return $itemsArray; } else { return array(); } } }