2013-09-25 03:04:44 +08:00
|
|
|
<?php
|
|
|
|
|
2014-10-17 18:15:19 +08:00
|
|
|
/*
|
|
|
|
* This file is part of MailSo.
|
|
|
|
*
|
|
|
|
* (c) 2014 Usenko Timur
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2013-09-25 03:04:44 +08:00
|
|
|
namespace MailSo\Mail;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @category MailSo
|
|
|
|
* @package Mail
|
|
|
|
*/
|
|
|
|
class FolderCollection extends \MailSo\Base\Collection
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-05-15 04:10:58 +08:00
|
|
|
public $Namespace;
|
2013-09-25 03:04:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $FoldersHash;
|
|
|
|
|
2013-10-24 03:11:15 +08:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $IsThreadsSupported;
|
|
|
|
|
2015-01-08 06:50:59 +08:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $Optimized;
|
|
|
|
|
2013-10-08 06:52:30 +08:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $SystemFolders;
|
|
|
|
|
2013-09-25 03:04:44 +08:00
|
|
|
/**
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
2015-05-15 04:10:58 +08:00
|
|
|
$this->Namespace = '';
|
2013-09-25 03:04:44 +08:00
|
|
|
$this->FoldersHash = '';
|
2013-10-08 06:52:30 +08:00
|
|
|
$this->SystemFolders = array();
|
2013-10-24 03:11:15 +08:00
|
|
|
$this->IsThreadsSupported = false;
|
2015-01-08 06:50:59 +08:00
|
|
|
$this->Optimized = false;
|
2013-09-25 03:04:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \MailSo\Mail\FolderCollection
|
|
|
|
*/
|
|
|
|
public static function NewInstance()
|
|
|
|
{
|
|
|
|
return new self();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $sFullNameRaw
|
|
|
|
*
|
|
|
|
* @return \MailSo\Mail\Folder|null
|
|
|
|
*/
|
2014-11-06 03:40:20 +08:00
|
|
|
public function GetByFullNameRaw($sFullNameRaw)
|
2013-09-25 03:04:44 +08:00
|
|
|
{
|
|
|
|
$mResult = null;
|
|
|
|
foreach ($this->aItems as /* @var $oFolder \MailSo\Mail\Folder */ $oFolder)
|
|
|
|
{
|
|
|
|
if ($oFolder->FullNameRaw() === $sFullNameRaw)
|
|
|
|
{
|
|
|
|
$mResult = $oFolder;
|
|
|
|
break;
|
|
|
|
}
|
2014-11-06 03:40:20 +08:00
|
|
|
else if ($oFolder->HasSubFolders())
|
|
|
|
{
|
|
|
|
$mResult = $oFolder->SubFolders(true)->GetByFullNameRaw($sFullNameRaw);
|
|
|
|
if ($mResult)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$mResult = null;
|
|
|
|
}
|
|
|
|
}
|
2013-09-25 03:04:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $mResult;
|
|
|
|
}
|
|
|
|
|
2015-05-15 04:10:58 +08:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function CountRec()
|
|
|
|
{
|
|
|
|
$iResult = $this->Count();
|
|
|
|
foreach ($this->aItems as /* @var $oFolder \MailSo\Mail\Folder */ $oFolder)
|
|
|
|
{
|
|
|
|
if ($oFolder)
|
|
|
|
{
|
|
|
|
$oSub = $oFolder->SubFolders();
|
|
|
|
$iResult += $oSub ? $oSub->CountRec() : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $iResult;
|
|
|
|
}
|
|
|
|
|
2013-09-25 03:04:44 +08:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function GetNamespace()
|
|
|
|
{
|
2015-05-15 04:10:58 +08:00
|
|
|
return $this->Namespace;
|
2013-09-25 03:04:44 +08:00
|
|
|
}
|
|
|
|
|
2015-03-04 05:15:17 +08:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function FindDelimiter()
|
|
|
|
{
|
|
|
|
$sDelimiter = '/';
|
2015-05-15 04:10:58 +08:00
|
|
|
|
2015-03-04 05:15:17 +08:00
|
|
|
$oFolder = $this->GetByFullNameRaw('INBOX');
|
|
|
|
if (!$oFolder)
|
|
|
|
{
|
|
|
|
$oFolder = $this->GetByIndex(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($oFolder)
|
|
|
|
{
|
|
|
|
$sDelimiter = $oFolder->Delimiter();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sDelimiter;
|
|
|
|
}
|
|
|
|
|
2013-09-25 03:04:44 +08:00
|
|
|
/**
|
|
|
|
* @param string $sNamespace
|
|
|
|
*
|
|
|
|
* @return \MailSo\Mail\FolderCollection
|
|
|
|
*/
|
|
|
|
public function SetNamespace($sNamespace)
|
|
|
|
{
|
2015-05-15 04:10:58 +08:00
|
|
|
$this->Namespace = $sNamespace;
|
2013-09-25 03:04:44 +08:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2014-11-06 03:40:20 +08:00
|
|
|
|
2013-09-25 03:04:44 +08:00
|
|
|
/**
|
|
|
|
* @param array $aUnsortedMailFolders
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function InitByUnsortedMailFolderArray($aUnsortedMailFolders)
|
|
|
|
{
|
|
|
|
$this->Clear();
|
|
|
|
|
|
|
|
$aSortedByLenImapFolders = array();
|
|
|
|
foreach ($aUnsortedMailFolders as /* @var $oMailFolder \MailSo\Mail\Folder */ &$oMailFolder)
|
|
|
|
{
|
|
|
|
$aSortedByLenImapFolders[$oMailFolder->FullNameRaw()] =& $oMailFolder;
|
|
|
|
unset($oMailFolder);
|
|
|
|
}
|
|
|
|
unset($aUnsortedMailFolders);
|
|
|
|
|
|
|
|
$aAddedFolders = array();
|
|
|
|
foreach ($aSortedByLenImapFolders as /* @var $oMailFolder \MailSo\Mail\Folder */ $oMailFolder)
|
|
|
|
{
|
|
|
|
$sDelimiter = $oMailFolder->Delimiter();
|
|
|
|
$aFolderExplode = \explode($sDelimiter, $oMailFolder->FullNameRaw());
|
|
|
|
|
|
|
|
if (1 < \count($aFolderExplode))
|
|
|
|
{
|
|
|
|
\array_pop($aFolderExplode);
|
|
|
|
|
|
|
|
$sNonExistenFolderFullNameRaw = '';
|
|
|
|
foreach ($aFolderExplode as $sFolderExplodeItem)
|
|
|
|
{
|
|
|
|
$sNonExistenFolderFullNameRaw .= (0 < \strlen($sNonExistenFolderFullNameRaw))
|
|
|
|
? $sDelimiter.$sFolderExplodeItem : $sFolderExplodeItem;
|
|
|
|
|
|
|
|
if (!isset($aSortedByLenImapFolders[$sNonExistenFolderFullNameRaw]))
|
|
|
|
{
|
2015-05-19 01:23:48 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$aAddedFolders[$sNonExistenFolderFullNameRaw] =
|
|
|
|
Folder::NewNonExistenInstance($sNonExistenFolderFullNameRaw, $sDelimiter);
|
|
|
|
}
|
|
|
|
catch (\Exception $oExc)
|
|
|
|
{
|
|
|
|
unset($oExc);
|
|
|
|
}
|
2013-09-25 03:04:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$aSortedByLenImapFolders = \array_merge($aSortedByLenImapFolders, $aAddedFolders);
|
|
|
|
unset($aAddedFolders);
|
|
|
|
|
|
|
|
\uasort($aSortedByLenImapFolders, function ($oFolderA, $oFolderB) {
|
|
|
|
return \strnatcmp($oFolderA->FullNameRaw(), $oFolderB->FullNameRaw());
|
|
|
|
});
|
|
|
|
|
|
|
|
foreach ($aSortedByLenImapFolders as /* @var $oMailFolder \MailSo\Mail\Folder */ &$oMailFolder)
|
|
|
|
{
|
|
|
|
$this->AddWithPositionSearch($oMailFolder);
|
|
|
|
unset($oMailFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($aSortedByLenImapFolders);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \MailSo\Mail\Folder $oMailFolder
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function AddWithPositionSearch($oMailFolder)
|
|
|
|
{
|
|
|
|
$oItemFolder = null;
|
|
|
|
$bIsAdded = false;
|
|
|
|
$aList =& $this->GetAsArray();
|
|
|
|
|
|
|
|
foreach ($aList as /* @var $oItemFolder \MailSo\Mail\Folder */ $oItemFolder)
|
|
|
|
{
|
|
|
|
if ($oMailFolder instanceof \MailSo\Mail\Folder &&
|
|
|
|
0 === \strpos($oMailFolder->FullNameRaw(), $oItemFolder->FullNameRaw().$oItemFolder->Delimiter()))
|
|
|
|
{
|
|
|
|
if ($oItemFolder->SubFolders(true)->AddWithPositionSearch($oMailFolder))
|
|
|
|
{
|
|
|
|
$bIsAdded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$bIsAdded && $oMailFolder instanceof \MailSo\Mail\Folder)
|
|
|
|
{
|
|
|
|
$bIsAdded = true;
|
|
|
|
$this->Add($oMailFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bIsAdded;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable $fCallback
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function SortByCallback($fCallback)
|
|
|
|
{
|
|
|
|
if (\is_callable($fCallback))
|
|
|
|
{
|
|
|
|
$aList =& $this->GetAsArray();
|
|
|
|
|
|
|
|
\usort($aList, $fCallback);
|
|
|
|
|
|
|
|
foreach ($aList as &$oItemFolder)
|
|
|
|
{
|
|
|
|
if ($oItemFolder->HasSubFolders())
|
|
|
|
{
|
|
|
|
$oItemFolder->SubFolders()->SortByCallback($fCallback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|