1: <?php
 2:  3:  4:  5:  6:  7:  8:  9: 
10: 
11: 12: 13: 14: 15: 16: 17: 
18: class Turba_Form_DeleteAddressBook extends Horde_Form
19: {
20:     21: 22: 
23:     protected $_addressbook;
24: 
25:     public function __construct($vars, $addressbook)
26:     {
27:         $this->_addressbook = $addressbook;
28:         parent::__construct($vars, sprintf(_("Delete %s"), $addressbook->get('name')));
29: 
30:         $this->addHidden('', 'a', 'text', true);
31:         $this->addVariable(sprintf(_("Really delete the address book \"%s\"? This cannot be undone and all contacts in this address book will be permanently removed."), $this->_addressbook->get('name')), 'desc', 'description', false);
32: 
33:         $this->setButtons(array(_("Delete"), _("Cancel")));
34:     }
35: 
36:     37: 38: 39: 40: 
41:     public function execute()
42:     {
43:         
44:         if ($this->_vars->get('submitbutton') == _("Cancel")) {
45:             return false;
46:         }
47: 
48:         if (!$GLOBALS['registry']->getAuth() ||
49:             $this->_addressbook->get('owner') != $GLOBALS['registry']->getAuth()) {
50:             throw new Turba_Exception(_("You do not have permissions to delete this address book."));
51:         }
52: 
53:         $driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver')->create($this->_addressbook->getName());
54:         if ($driver->hasCapability('delete_all')) {
55:             try {
56:                 $driver->deleteAll();
57:             } catch (Turba_Exception $e) {
58:                 Horde::logMessage($e->getMessage(), 'ERR');
59:                 throw $e;
60:             }
61:         }
62: 
63:         
64:         try {
65:             $GLOBALS['turba_shares']->removeShare($this->_addressbook);
66:         } catch (Horde_Share_Exception $e) {
67:             Horde::logMessage($e->getMessage(), 'ERR');
68:             throw new Turba_Exception($e);
69:         }
70: 
71:         if ($GLOBALS['session']->get('turba', 'source') == Horde_Util::getFormData('deleteshare')) {
72:             $GLOBALS['session']->remove('turba', 'source');
73:         }
74: 
75:         $abooks = json_decode($GLOBALS['prefs']->getValue('addressbooks'), true);
76:         if (($pos = array_search($this->_addressbook->getName(), $abooks)) !== false) {
77:             unset($abooks[$pos]);
78:             $GLOBALS['prefs']->setValue('addressbooks', json_encode($abooks));
79:         }
80: 
81:         return true;
82:     }
83: 
84: }
85: