00001 <?php
00002
00009 class aliroFolder extends aliroDatabaseRow {
00010 protected $DBclass = 'aliroDatabase';
00011 protected $tableName = '#__folders';
00012 protected $rowKey = 'id';
00013 protected $loaded = 0;
00014 protected $children = array();
00015 protected $isTrash = false;
00016
00017 public function load ($key=null) {
00018 $database = aliroDatabase::getInstance();
00019 if ($this->loaded == 0 AND $this->id) {
00020 $database->setQuery("SELECT * FROM #__folders WHERE id=$this->id");
00021 $database->loadObject($this);
00022 $this->loaded = 1;
00023 }
00024 }
00025
00026 public function trash () {
00027 $this->isTrash = true;
00028 }
00029
00030 public function isTrash () {
00031 return $this->isTrash;
00032 }
00033
00034 public function addChild ($id) {
00035 $this->children[] = $id;
00036 }
00037
00038 public function deleteAll () {
00039 $folders = $this->getChildren(false);
00040 foreach ($folders as $folder) $folder->deleteAll ();
00041
00042 $this->trash();
00043 }
00044
00045 public function setMetaData () {
00046 $mainframe = mosMainFrame::getInstance();
00047 $mainframe->prependMetaTag('description', strip_tags($this->name));
00048 if ($this->keywords) $mainframe->prependMetaTag('keywords', $this->keywords);
00049 else $mainframe->prependMetaTag('keywords', $this->name);
00050 }
00051
00052 public function isCategory () {
00053 if ($this->parentid == 0) return true;
00054 else return false;
00055 }
00056
00057 public function getCategoryName ($showself=false) {
00058 $category = $this->getCategory();
00059 if ($this->parentid OR $showself) return $category->name;
00060 return '*';
00061 }
00062
00063 public function getCategory () {
00064 $folder = $this;
00065 while ($folder->parentid) $folder = $folder->getParent();
00066 return $folder;
00067 }
00068
00069 public function getFamilyNames ($include=false) {
00070 $names = $include ? '/'.$this->name : '';
00071 $generation = 1;
00072 $ancestor = $this;
00073 while ($ancestor->parentid AND $generation < 3) {
00074 $ancestor = $ancestor->getParent();
00075 $generation++;
00076 $names = '/'.$ancestor->name.$names;
00077 }
00078 if ($ancestor->parentid) $names = '..'.$names;
00079 if ($names) return $names;
00080 return '-';
00081 }
00082
00083 public function addChildren (&$descendants, $published=true, $search='', $recurse=false) {
00084 $children = array();
00085 $handler = aliroFolderHandler::getInstance();
00086 foreach ($this->children as $i) {
00087 $folder = $handler->getBasicFolder($i);
00088 if ($published AND $folder->published == 0) continue;
00089 if ($search AND strpos(strtolower($folder->name), strtolower($search)) === false) continue;
00090 $children[] = $folder;
00091 $descendants[] = $folder;
00092 }
00093 if ($recurse) foreach ($children as $child) $child->addChildren ($descendants, $published, $search, $recurse);
00094 return $children;
00095 }
00096
00097 public function getChildren ($published=true, $search='') {
00098 $children = array();
00099 $this->addChildren($children, $published, $search);
00100 return $children;
00101 }
00102
00103 public function getDescendants ($search='') {
00104 $descendants = array();
00105 $this->addChildren ($descendants, false, $search, true);
00106 return $descendants;
00107 }
00108
00109 public function getParent () {
00110 $handler = aliroFolderHandler::getInstance();
00111 $parent = $handler->getBasicFolder($this->parentid);
00112 return $parent;
00113 }
00114
00115 public function getSelectList ($type, $parm, $published, $notThis=0) {
00116 $alirohtml = aliroHTML::getInstance();
00117 $selector[] = $alirohtml->makeOption(0,T_('No parent'));
00118 $handler = aliroFolderHandler::getInstance();
00119 foreach ($handler->getCategories() as $category) $category->addSelectList('',$selector,$notThis,$published);
00120 return $alirohtml->selectList( $selector, $type, $parm, 'value', 'text', $this->id );
00121 }
00122
00123 public function addSelectList ($prefix, &$selector, $notThis, $published) {
00124 if (($notThis == 0) OR ($this->id != $notThis)) $selector[] = aliroHTML::getInstance()->makeOption($this->id, $prefix.htmlspecialchars($this->name));
00125 foreach ($this->getChildren($published) as $folder) $folder->addSelectList($prefix.$this->name.'/',$selector,$notThis,$published);
00126 }
00127
00128 function getURL () {
00129 }
00130
00131 function setPathway () {
00132 }
00133
00134 public static function getIcons () {
00135 $iconList = '';
00136 $live_site = aliroCore::get('mosConfig_live_site');
00137 $iconDir = new aliroDirectory (aliroCore::get('mosConfig_admin_absolute_path').'/components/com_folders/images/folder_icons');
00138 $files = $iconDir->listAll();
00139 $ss = 0;
00140 foreach ($files as $file) {
00141 $iconList.="\n<a href=\"JavaScript:paste_strinL('{$file}')\" onmouseover=\"window.status='{$file}'; return true\"><img src=\"{$live_site}/administrator/components/com_folders/images/folder_icons/{$file}\" width=\"32\" height=\"32\" border=\"0\" alt=\"{$file}\" /></a> ";
00142 $ss++;
00143 if ($ss % 10 == 0) $iconList.="<br/>\n";
00144 }
00145 return $iconList;
00146 }
00147
00148
00149 public function togglePublished ($idlist, $value) {
00150 $cids = implode( ',', $idlist );
00151 $sql = "UPDATE #__folders SET published=$value". "\nWHERE id IN ($cids)";
00152 remositoryRepository::doSQL ($sql);
00153 }
00154
00155 public function imageURL($imageName, $width=32, $height=32) {
00156 $live_site = aliroCore::get('mosConfig_live_site');
00157 $element = '<img src="';
00158 $element .= $live_site.'/administrator/components/com_folders/images/'.$imageName;
00159 $element .= '" width="';
00160 $element .= $width;
00161 $element .= '" height="';
00162 $element .= $height;
00163 $element .= '" border="0" align="middle" alt="';
00164 $element .= $imageName;
00165 $element .= '"/>';
00166 return $element;
00167 }
00168
00169 }
00170
00171 class aliroFolderHandler extends cachedSingleton {
00172 protected static $instance = __CLASS__;
00173 private $rows = array();
00174 private $byancestor = array();
00175 private $anchor = '';
00176
00177 protected function __construct () {
00178 $database = aliroDatabase::getInstance();
00179 $this->anchor = new aliroFolder();
00180 $sql = 'SELECT id, parentid, name, published, ordering FROM #__folders ORDER BY ordering, name';
00181 $this->rows = $database->doSQLget($sql, 'aliroFolder', 'id');
00182 foreach ($this->rows as $row) {
00183 $folder = $row;
00184 do $this->byancestor[$folder->parentid][$row->id] = $folder->id;
00185 while ($folder->parentid AND $folder = $this->rows[$folder->parentid]);
00186 if ($row->parentid) {
00187 $parent = $this->rows[$row->parentid];
00188 $parent->addChild($row->id);
00189 }
00190 else $this->anchor->addChild($row->id);
00191 }
00192 }
00193
00194 public static function getInstance () {
00195 return is_object(self::$instance) ? self::$instance : (self::$instance = parent::getCachedSingleton(self::$instance));
00196 }
00197
00198 public function getBasicFolder ($id) {
00199 if ($id == 0) return $this->anchor;
00200 return isset($this->rows[$id]) ? $this->rows[$id] : null;
00201 }
00202
00203 public function getFolder ($id) {
00204 global $database;
00205 $result = $this->getBasicFolder($id);
00206 if ($result) $result->load();
00207 return $result;
00208 }
00209
00210 public function getCategories ($published = false, $search = null) {
00211 $categories = array();
00212 foreach ($this->anchor->getChildren() as $category) {
00213 if ($published AND $category->published == 0) continue;
00214 if ($search AND strpos(strtolower($category->name), strtolower($search)) === false) continue;
00215 $categories[] = $category;
00216 }
00217 return $categories;
00218 }
00219
00220 public function getDescendantIDList ($id, $search='') {
00221 $top = $this->getBasicFolder ($id);
00222 $descendants = $top->getDescendants ($search);
00223 $list = $id;
00224 foreach ($descendants as $descendant) $list .= ','.$descendant->id;
00225 return $list;
00226 }
00227
00228 public function getSelectList ($allowTop, $default, $type, $parm, &$user) {
00229 $alirohtml = aliroHTML::getInstance();
00230 if ($allowTop) $selector[] = $alirohtml->makeOption(0,_DOWN_NO_PARENT);
00231 foreach ($this->getCategories() as $category) $category->addSelectList('', $selector, null, $user);
00232 if (isset($selector)) return $alirohtml->selectList( $selector, $type, $parm, 'value', 'text', $default );
00233 else return '';
00234 }
00235
00236
00237 public function displayChildren (&$folder) {
00238 echo '<br />'.$folder->name.' has children:<br />';
00239 foreach ($folder->children as $child) echo $child->name.' whose parent is '.$child->parent->name.'<br />';
00240 foreach ($folder->children as $child) displayChildren ($child);
00241 }
00242
00243 public function delete ($deletelist) {
00244 $database = aliroDatabase::getInstance();
00245 $sql = "DELETE FROM #__folders WHERE id IN ($deletelist)";
00246 $database->doSQL($sql);
00247 $this->clearCache();
00248 self::$instance = __CLASS__;
00249 }
00250
00251 public function changeOrder ($id, $direction) {
00252 $folder = $this->getFolder($id);
00253 $movement = 'down' == $direction ? 15 : -15;
00254 $this->updateOrdering (array($id => $folder->ordering + $movement), $folder->parentid);
00255 }
00256
00257 public function updateOrdering ($orders, $parentid) {
00258 foreach ($orders as $id=>$order) {
00259 $folder = $this->getFolder($id);
00260 if ($folder->ordering != $order) $changes[$id] = $order;
00261 }
00262 $parent = $this->getFolder($parentid);
00263 foreach ($parent->getChildren(false) as $folder) {
00264 $ordering = isset($changes[$folder->id]) ? $changes[$folder->id] : $folder->ordering;
00265 $allfolders[$ordering] = $folder->id;
00266 }
00267 $changed = false;
00268 $query = "UPDATE #__folders SET ordering = CASE ";
00269 $order = 10;
00270 ksort($allfolders);
00271 foreach ($allfolders as $ordering=>$id) {
00272 $folder = $this->getFolder($id);
00273 if ($order != $folder->ordering) {
00274 $query .= "WHEN id = $id THEN $order ";
00275 $changed = true;
00276 }
00277 $order += 10;
00278 }
00279 if ($changed) {
00280 $query .= 'ELSE ordering END';
00281 aliroDatabase::getInstance()->doSQL ($query);
00282 $this->clearCache();
00283 }
00284 }
00285
00286 public function ancestorsWithParent ($parentid, $folderIDs) {
00287 $ancestors = array();
00288 if (!is_array($folderIDs)) return $ancestors;
00289 foreach ($folderIDs as $folderID) if (isset($this->byancestor[$parentid][$folderID])) $results[$this->byancestor[$parentid][$folderID]] = 1;
00290 if (isset($results)) foreach (array_keys($results) as $id) $ancestors[] = $this->getFolder($id);
00291 return $ancestors;
00292 }
00293
00294 public function setFolderPathway ($folder, $baseurl) {
00295 if ($folder->parentid) $this->setFolderPathway ($folder->getParent(), $baseurl);
00296 $link = aliroSEF::getInstance()->sefRelToAbs($baseurl.$folder->id);
00297 aliroPathway::getInstance()->addItem ($folder->name, $link);
00298 }
00299
00300 }