Changeset View
Changeset View
Standalone View
Standalone View
plugins/csv_export/csv_export.php
Show All 18 Lines | |||||
* GNU Affero General Public License for more details. | * GNU Affero General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU Affero General Public License | * You should have received a copy of the GNU Affero General Public License | ||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
class csv_export extends rcube_plugin | class csv_export extends rcube_plugin | ||||
{ | { | ||||
public $task = 'addressbook'; | public $task = 'addressbook|tasks'; | ||||
/** | /** | ||||
* Startup method of a Roundcube plugin | * Startup method of a Roundcube plugin | ||||
*/ | */ | ||||
public function init() | public function init() | ||||
{ | { | ||||
$rcmail = rcube::get_instance(); | $rcmail = rcube::get_instance(); | ||||
// register hooks | // register hooks | ||||
$this->add_hook('addressbook_export', array($this, 'addressbook_export')); | $this->add_hook('addressbook_export', array($this, 'addressbook_export')); | ||||
$this->add_hook('tasks_export', array($this, 'tasks_export')); | |||||
// Add localization and js script | // Add localization and js script | ||||
if ($this->api->output->type == 'html' && !$this->rcmail->action) { | if ($this->api->output->type == 'html' && !$rcmail->action) { | ||||
$this->add_texts('localization', true); | $this->add_texts('localization', true); | ||||
$this->api->output->add_label('export', 'cancel'); | $this->api->output->add_label('export', 'cancel'); | ||||
$this->include_script('csv_export.js'); | $this->include_script('csv_export.js'); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Handler for the addressbook_export hook. | * Handler for the addressbook_export hook. | ||||
* | * | ||||
* @param array $p Hash array with hook parameters | * @param array $p Hash array with hook parameters | ||||
* | * | ||||
* @return array Hash array with modified hook parameters | * @return array Hash array with modified hook parameters | ||||
*/ | */ | ||||
public function addressbook_export($p) | public function addressbook_export($p) | ||||
{ | { | ||||
if ($_GET['_format'] != 'csv') { | if ($_GET['_format'] != 'csv') { | ||||
return $p; | return $p; | ||||
} | } | ||||
global $CONTACTS; | global $CONTACTS; | ||||
require_once(__DIR__ . '/vcard2csv.php'); | require_once(__DIR__ . '/vcard2csv.php'); | ||||
$csv = new vcard2csv; | $csv = new vcard2csv; | ||||
$rcmail = rcube::get_instance(); | |||||
$rcmail->output->nocacheing_headers(); | |||||
// send downlaod headers | // send downlaod headers | ||||
$csv->headers(); | $csv->headers(); | ||||
if (!$p['result']) { | if (!$p['result']) { | ||||
exit; | exit; | ||||
} | } | ||||
echo $csv->head(); | echo $csv->head(); | ||||
while ($row = $p['result']->next()) { | while ($row = $p['result']->next()) { | ||||
if ($CONTACTS) { | if ($CONTACTS) { | ||||
prepare_for_export($row, $CONTACTS); | prepare_for_export($row, $CONTACTS); | ||||
} | } | ||||
echo $csv->record($row['vcard']); | echo $csv->record($row['vcard']); | ||||
} | } | ||||
exit; | exit; | ||||
} | } | ||||
/** | |||||
* Handler for the tasks_export hook. | |||||
* | |||||
* @param array $p Hash array with hook parameters | |||||
* | |||||
* @return array Hash array with modified hook parameters | |||||
*/ | |||||
public function tasks_export($p) | |||||
{ | |||||
if ($_GET['_format'] != 'csv' && $_POST['_format'] != 'csv') { | |||||
return $p; | |||||
} | |||||
require_once(__DIR__ . '/event2csv.php'); | |||||
$csv = new event2csv; | |||||
$rcmail = rcube::get_instance(); | |||||
$rcmail->output->nocacheing_headers(); | |||||
// don't kill the connection if download takes more than 30 sec. | |||||
@set_time_limit(300); | |||||
// send downlaod headers | |||||
$csv->headers(preg_replace('/\.ics$/', '.csv', $p['filename'])); | |||||
// sent format line | |||||
echo $csv->head(); | |||||
foreach ((array) $p['result'] as $record) { | |||||
echo $csv->record($record); | |||||
} | |||||
exit; | |||||
} | |||||
} | } |