em = $em; } /** * Return a string that of the SettingItem that match $name if found, * return $default (null default) otherwise * * if $createIfNotSet is set to TRUE this will create the value in database * * @param $name * @param null $default * @param bool $createIfNotSet * @param string $section * @return mixed */ public function get($name, $default = null, $createIfNotSet = false, $section = ""){ if(empty($name)) return $default; $itm = $this->getItem($name); if(is_bool($itm)){ if($createIfNotSet) $this->update($name,$default,$section); return $default; } return $itm->getValue(); } /** * Return a SettingItem that match $name if found, * return false otherwise * * @param $name * @return bool|SettingItem */ public function getItem($name){ if(empty($name)) return false; $repository = $this->em->getRepository('SettingsManagerBundle:SettingItem'); /** * @var $itm SettingItem */ $itm = $repository->findOneByName($name); if(!is_null($itm) || !empty($itm)){ if($itm->isSerialized()) $itm->setValue(unserialize($itm->getValue())); return $itm; } return false; } /** * Update/Create value and (optional) section of the given SettingItem * * @param $name * @param mixed $value * @param string $section * @return bool */ public function update($name, $value = "", $section = ""){ $itm = $this->getItem($name); if(!$itm instanceof SettingItem){ $itm = new SettingItem(); $itm->setName($name); } if(is_array($value)){ $value = serialize($value); $itm->setSerialized(true); } else { $itm->setSerialized(false); } $itm->setValue($value); if(!is_null($section)) $itm->setSection($section); try { if(!$this->em->contains($itm)) $this->em->persist($itm); $this->em->flush(); } catch (OptimisticLockException $e) { return false; } catch (ORMException $e) { return false; } return true; } }