[imp] auto create folder

Laurent Foucher laurent.foucher at iut-tlse3.fr
Fri May 13 15:31:50 UTC 2011


Michael M Slusarz <slusarz at horde.org> a écrit :

> Quoting Laurent Foucher <laurent.foucher at iut-tlse3.fr>:
>
>> Hi,
>>
>> Based on the script found on the wiki, default folders are created  
>> if not exists with imp4. Here the script i use :
>>
>> if (!function_exists('_imp_hook_postlogin')) {
>>     function _imp_hook_postlogin($actionID, $isLogin)
>>     {
>>        global $prefs;
>>        require_once '/usr/share/horde3/imp/lib/Folder.php';
>>
>>        $imp_folder = &IMP_Folder::singleton();
>>        $folder_options = array(
>>            'sent_mail_folder', 'drafts_folder', 'trash_folder',  
>> 'spam_folder'
>>        );
>>        foreach ($folder_options as $this_folder) {
>>            $folder = $prefs->getValue($this_folder, true);
>>            if ($folder) {
>>                $folder = IMP::folderPref($folder, true);
>>                if (!$imp_folder->exists($folder)) {
>>                    $imp_folder->create($folder, true);
>>                }
>>            }
>>        }
>>     }
>> }
>
> These folders will all be auto-created the first time they are needed.
>
> If you want to auto-create on first login so that the user actually  
> sees the mailbox in folder lists, you should use the authinitialized  
> hook.
>

I would like auto-create on the first login. To do that, with your  
advice, i have just write the following script
in imp/config/hooks.php :

  public function appinitialized()
{
         $vars = Horde_Variables::getDefaultVariables();
         $imp_folder = $GLOBALS['injector']->getInstance('IMP_Folder');
         $imaptree = $GLOBALS['injector']->getInstance('IMP_Imap_Tree');
         $folder_list = isset($vars->folder_list)
             ? array_map(array('IMP_Mailbox', 'formFrom'), $vars->folder_list)
             : array();
//
         $folder_options = array(
             'sent_mail_folder', 'drafts_folder', 'trash_folder', 'spam_folder'
         );
//
         foreach ($folder_options as $this_folder) {
         $folder_to_create = Horde_Api::getPreference('imp',$this_folder);
         Horde::logMessage(sprintf('FOLDER : %s', $folder_to_create),  
'NOTICE');
                 try {
                     $imaptree->createMailboxName(
                         $folder_list[0],
                          
Horde_String::convertCharset($folder_to_create, 'UTF-8', 'UTF7-IMAP')
                     )->create();
                 } catch (Horde_Exception $e) {
                     $notification->push($e);
                 }
         }
}

It seems to work fine. If you think that there is no mistakes, i can  
upgrade the wiki with it.

Laurent.




More information about the imp mailing list