protected function _thread($options)
{
$thread_criteria = array(Horde_Imap_Client::THREAD_ORDEREDSUBJECT => 'ORDEREDSUBJECT', Horde_Imap_Client::THREAD_REFERENCES => 'REFERENCES', Horde_Imap_Client::THREAD_REFS => 'REFS');
$tsort = isset($options['criteria']) ? is_string($options['criteria']) ? Horde_String::upper($options['criteria']) : $thread_criteria[$options['criteria']] : 'ORDEREDSUBJECT';
if (!$this->_capability('THREAD', $tsort)) {
switch ($tsort) {
case 'ORDEREDSUBJECT':
if (empty($options['search'])) {
$ids = $this->getIdsOb(Horde_Imap_Client_Ids::ALL, !empty($options['sequence']));
} else {
$search_res = $this->search($this->_selected, $options['search'], array('sequence' => !empty($options['sequence'])));
$ids = $search_res['match'];
}
/* Do client-side ORDEREDSUBJECT threading. */
$query = new Horde_Imap_Client_Fetch_Query();
$query->envelope();
$query->imapDate();
$fetch_res = $this->fetch($this->_selected, $query, array('ids' => $ids));
if (!isset($this->_temp['clientsort'])) {
$this->_temp['clientsort'] = new Horde_Imap_Client_Socket_ClientSort($this);
}
return $this->_temp['clientsort']->threadOrderedSubject($fetch_res, empty($options['sequence']));
case 'REFERENCES':
case 'REFS':
throw new Horde_Imap_Client_Exception_NoSupportExtension('THREAD', sprintf('Server does not support "%s" thread sort.', $tsort));
}
}
$cmd = $this->_command(empty($options['sequence']) ? 'UID THREAD' : 'THREAD')->add($tsort);
/* If UTF-8 support is activated, a client MUST NOT
* send the charset specification (RFC 6855 [3]; Errata 4029). */
if (empty($options['search'])) {
if (!$this->_capability()->isEnabled('UTF8=ACCEPT')) {
$cmd->add('US-ASCII');
}
$cmd->add('ALL');
} else {
$search_query = $options['search']->build();
if (!$this->_capability()->isEnabled('UTF8=ACCEPT')) {
$cmd->add(is_null($search_query['charset']) ? 'US-ASCII' : $search_query['charset']);
}
$cmd->add($search_query['query'], true);
}
return new Horde_Imap_Client_Data_Thread($this->_sendCmd($cmd)->data['threadparse'], empty($options['sequence']) ? 'uid' : 'sequence');
}