* @package tao */ class tao_models_classes_ListService extends tao_models_classes_GenerisService { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- /** * the list parent class * * @access protected * @var Class */ protected $parentListClass = null; // --- OPERATIONS --- /** * initialize the service * * @access protected * @author Joel Bout, * @return mixed */ protected function __construct() { $this->parentListClass = new core_kernel_classes_Class(TaoOntology::CLASS_URI_LIST); } /** * get all the lists class * * @access public * @author Joel Bout, * @return array */ public function getLists() { $returnValue = []; $returnValue[] = new core_kernel_classes_Class(GenerisRdf::GENERIS_BOOLEAN); foreach ($this->parentListClass->getSubClasses(false) as $list) { $returnValue[] = $list; } return (array) $returnValue; } /** * Get a list class from the uri in parameter * * @access public * @author Joel Bout, * @param string uri * @return core_kernel_classes_Class */ public function getList($uri) { $returnValue = null; foreach ($this->getLists() as $list) { if ($list->getUri() == $uri) { $returnValue = $list; break; } } return $returnValue; } /** * get the element of the list defined by listClass and identified by the * in parameter * * @access public * @author Joel Bout, * @param Class listClass * @param string uri * @return core_kernel_classes_Resource */ public function getListElement(core_kernel_classes_Class $listClass, $uri) { $returnValue = null; if (!empty($uri)) { foreach ($this->getListElements($listClass, false) as $element) { if ($element->getUri() == $uri) { $returnValue = $element; break; } } } return $returnValue; } /** * get all the elements of the list * * @access public * * @param core_kernel_classes_Class $listClass * @param boolean sort * @param int $limit * * @return array * @throws core_kernel_persistence_Exception * @author Joel Bout, */ public function getListElements(core_kernel_classes_Class $listClass, $sort = true, $limit = 0) { $returnValue = []; $parameters = $limit === 0 ? [] : ['limit' => $limit]; if ($sort) { $levelProperty = new core_kernel_classes_Property(TaoOntology::PROPERTY_LIST_LEVEL); foreach ($listClass->getInstances(false, $parameters) as $element) { $literal = $element->getOnePropertyValue($levelProperty); $level = is_null($literal) ? 0 : (string) $literal; while (isset($returnValue[$level])) { $level++; } $returnValue[$level] = $element; } uksort($returnValue, 'strnatcasecmp'); } else { $returnValue = $listClass->getInstances(false, $parameters); } return (array) $returnValue; } /** * remove a list and it's elements * * @access public * @author Joel Bout, * @param Class listClass * @return boolean */ public function removeList(core_kernel_classes_Class $listClass) { $returnValue = (bool) false; if (!is_null($listClass)) { foreach ($this->getListElements($listClass) as $element) { $this->removeListElement($element); } $returnValue = $listClass->delete(); } return (bool) $returnValue; } /** * remove a list element * * @access public * @author Joel Bout, * @param Resource element * @return boolean */ public function removeListElement(core_kernel_classes_Resource $element) { $returnValue = (bool) false; if (!is_null($element)) { $returnValue = $element->delete(); } return (bool) $returnValue; } /** * create a new list class * * @access public * @author Joel Bout, * @param string label * @return core_kernel_classes_Class */ public function createList($label = '') { $returnValue = null; if (empty($label)) { $label = __('List') . ' ' . (count($this->getLists()) + 1); } $returnValue = $this->createSubClass($this->parentListClass, $label); return $returnValue; } /** * add a new element to a list * * @access public * @author Joel Bout, * @param Class listClass * @param string label * @return core_kernel_classes_Resource */ public function createListElement(core_kernel_classes_Class $listClass, $label = '') { $returnValue = null; if (!is_null($listClass)) { $level = count($this->getListElements($listClass)) + 1; if (empty($label)) { $label = __('Element') . ' ' . $level; } $returnValue = $this->createInstance($listClass, $label); $this->bindProperties($returnValue, [TaoOntology::PROPERTY_LIST_LEVEL => count($this->getListElements($listClass, false))]); } return $returnValue; } }