-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathContributionPlugin.php
814 lines (737 loc) · 30.4 KB
/
ContributionPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
<?php
/**
* @version $Id$
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @copyright Center for History and New Media, 2010
* @package Contribution
*/
define('CONTRIBUTION_PLUGIN_DIR', dirname(__FILE__));
define('CONTRIBUTION_HELPERS_DIR', CONTRIBUTION_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'helpers');
define('CONTRIBUTION_FORMS_DIR', CONTRIBUTION_PLUGIN_DIR . DIRECTORY_SEPARATOR . 'forms');
require_once CONTRIBUTION_HELPERS_DIR . DIRECTORY_SEPARATOR . 'ThemeHelpers.php';
/**
* Contribution plugin class
*
* @copyright Center for History and New Media, 2010
* @package Contribution
*/
class ContributionPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'initialize',
'install',
'uninstall',
'upgrade',
'define_acl',
'define_routes',
'uninstall_message',
'admin_items_search',
'admin_items_show_sidebar',
'admin_items_browse_detailed_each',
'items_browse_sql',
'before_save_item',
'after_delete_item',
);
/**
* @var array Filters for the plugin.
*/
protected $_filters = array(
'admin_navigation_main',
'public_navigation_main',
'simple_vocab_routes',
'item_citation',
'item_search_filters',
'guest_user_links',
'guest_user_widgets',
'api_resources',
'api_import_omeka_adapters'
);
/**
* @var array Options and their default values.
*/
protected $_options = array(
'contribution_page_path',
'contribution_email_sender',
'contribution_email_recipients',
'contribution_consent_text',
'contribution_collection_id',
'contribution_default_type',
'contribution_user_profile_type',
'contribution_open',
'contribution_email',
'contribution_strict_anonymous'
);
public function setUp()
{
parent::setUp();
if (plugin_is_active('UserProfiles')) {
$this->_hooks[] = 'user_profiles_user_page';
}
if (! is_admin_theme()) {
//dig up all the elements being used, and add their ElementForm hook
$elementsTable = $this->_db->getTable('Element');
$select = $elementsTable->getSelect();
$select->join(array('contribution_type_elements' => $this->_db->ContributionTypeElement),
'element_id = elements.id', array());
$elements = $elementsTable->fetchObjects($select);
foreach ($elements as $element) {
add_filter(array('ElementForm', 'Item', $element->set_name, $element->name ), array($this, 'elementFormFilter'), 2);
add_filter(array('ElementInput', 'Item', $element->set_name, $element->name ), array($this, 'elementInputFilter'), 2);
}
}
}
/**
* Add the translations.
*/
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
}
/**
* Contribution install hook
*/
public function hookInstall()
{
$db = $this->_db;
$sql = "CREATE TABLE IF NOT EXISTS `$db->ContributionType` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`item_type_id` INT UNSIGNED NOT NULL,
`display_name` VARCHAR(255) NOT NULL,
`file_permissions` ENUM('Disallowed', 'Allowed', 'Required') NOT NULL DEFAULT 'Disallowed',
PRIMARY KEY (`id`),
KEY `item_type_id` (`item_type_id`)
) ENGINE=MyISAM;";
$this->_db->query($sql);
$sql = "CREATE TABLE IF NOT EXISTS `$db->ContributionTypeElement` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`type_id` INT UNSIGNED NOT NULL,
`element_id` INT UNSIGNED NOT NULL,
`prompt` VARCHAR(255) NOT NULL,
`order` INT UNSIGNED NOT NULL,
`long_text` BOOLEAN DEFAULT TRUE,
PRIMARY KEY (`id`),
UNIQUE KEY `type_id_element_id` (`type_id`, `element_id`),
KEY `order` (`order`)
) ENGINE=MyISAM;";
$this->_db->query($sql);
$sql = "CREATE TABLE IF NOT EXISTS `$db->ContributionContributedItem` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`item_id` INT UNSIGNED NOT NULL,
`public` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`anonymous` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `item_id` (`item_id`)
) ENGINE=MyISAM;";
$this->_db->query($sql);
$this->_createDefaultContributionTypes();
set_option('contribution_email_recipients', get_option('administrator_email'));
}
/**
* Contribution uninstall hook
*/
public function hookUninstall()
{
// Delete all the Contribution options
foreach ($this->_options as $option) {
delete_option($option);
}
$db = $this->_db;
// Drop all the Contribution tables
$sql = "DROP TABLE IF EXISTS
`$db->ContributionType`,
`$db->ContributionTypeElement`,
`$db->ContributionContributor`,
`$db->ContributionContributedItem`,
`$db->ContributionContributorField`,
`$db->ContributionContributorValue`;";
$this->_db->query($sql);
}
public function hookUpgrade($args)
{
$oldVersion = $args['old_version'];
$newVersion = $args['new_version'];
// Catch-all for pre-2.0 versions
if (version_compare($oldVersion, '2.0-dev', '<=')) {
// Clean up old options
delete_option('contribution_plugin_version');
delete_option('contribution_db_migration');
$emailSender = get_option('contribution_contributor_email');
if (!empty($emailSender)) {
set_option('contribution_email_sender', $emailSender);
}
$pagePath = get_option('contribution_page_path');
if ($pagePath = 'contribution/') {
delete_option('contribution_page_path');
} else {
set_option('contribution_page_path', trim($pagePath, '/'));
}
// Since this is an upgrade from an old version, we need to install
// all our tables.
$this->hookInstall();
}
if (version_compare($oldVersion, '3.0', '<')) {
if(!is_writable(CONTRIBUTION_PLUGIN_DIR . "/upgrade_files")) {
throw new Omeka_Plugin_Installer_Exception("'upgrade_files' directory must be writable by the web server");
}
require_once(CONTRIBUTION_PLUGIN_DIR . '/libraries/ContributionImportUsers.php');
//change contributors to real guest users
Zend_Registry::get('bootstrap')->getResource('jobs')->sendLongRunning('ContributionImportUsers');
//if the optional UserProfiles plugin is installed, handle the upgrade via the configuration page
$sql = "ALTER TABLE `{$this->_db->ContributionTypeElement}` ADD `long_text` BOOLEAN DEFAULT TRUE";
try {
$this->_db->query($sql);
} catch(Exception $e) {
_log($e);
}
$contributionTypeElements = $this->_db->getTable('ContributionTypeElement')->findAll();
foreach($contributionTypeElements as $typeElement) {
$typeElement->long_text = true;
$typeElement->save();
}
$sql = "
ALTER TABLE `{$this->_db->ContributionContributedItem}` CHANGE `contributor_posting` `anonymous` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0';
";
$this->_db->query($sql);
$sql = "
ALTER TABLE `{$this->_db->ContributionContributedItem}` DROP `contributor_id` ;
";
$this->_db->query($sql);
//clean up contributed item records if the corresponding item has been deleted
//earlier verison of the plugin did not use the delete hook
$sql = "DELETE FROM `{$this->_db->ContributionContributedItem}` WHERE NOT EXISTS (SELECT 1 FROM `{$this->_db->prefix}items` WHERE `{$this->_db->prefix}contribution_contributed_items`.`item_id` = `{$this->_db->prefix}items`.`id`)";
$this->_db->query($sql);
}
if (version_compare($oldVersion, '3.0.2', '<')) {
//fix some previous bad upgrades
//need to check if contributor_posting was properly changed to anonymous
$sql = "SHOW COLUMNS IN `{$this->_db->ContributionContributedItem}`";
$result = $this->_db->query($sql);
$cols = $result->fetchAll(Zend_Db::FETCH_COLUMN);
if(in_array('contributor_posting', $cols)) {
$sql = "
ALTER TABLE `{$this->_db->ContributionContributedItem}` CHANGE `contributor_posting` `anonymous` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0';
";
$this->_db->query($sql);
} else if (! in_array('anonymous', $cols)) {
$sql = "
ALTER TABLE `{$this->_db->ContributionContributedItem}` ADD `anonymous` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0';
";
$this->_db->query($sql);
}
if(in_array('contributor_id', $cols)) {
$sql = "
ALTER TABLE `{$this->_db->ContributionContributedItem}` DROP `contributor_id` ;
";
$this->_db->query($sql);
}
}
if (version_compare($oldVersion, 3.1, '<')) {
set_option('contribution_open', get_option('contribution_simple'));
delete_option('contribution_simple');
}
if (version_compare($oldVersion, '3.2', '<')) {
$this->_db->query("ALTER TABLE `{$this->_db->ContributionType}`
DROP INDEX `item_type_id`,
ADD INDEX `item_type_id` (`item_type_id`)");
}
}
public function hookUninstallMessage()
{
echo '<p><strong>Warning</strong>: Uninstalling the Contribution plugin
will remove all information about contributors, as well as the
data that marks which items in the archive were contributed.</p>
<p>The contributed items themselves will remain.</p>';
}
/**
* Contribution define_acl hook
* Restricts access to admin-only controllers and actions.
*/
public function hookDefineAcl($args)
{
$acl = $args['acl'];
$acl->addRole(new Zend_Acl_Role('contribution_anonymous'), null);
$acl->addResource('Contribution_Contribution');
$acl->allow(array('super', 'admin', 'researcher', 'contributor'), 'Contribution_Contribution');
if (get_option('contribution_open')) {
$acl->allow(null, 'Contribution_Contribution', array('show', 'contribute', 'thankyou', 'my-contributions', 'type-form'));
} else {
$acl->allow('guest', 'Contribution_Contribution', array('show', 'contribute', 'thankyou', 'my-contributions', 'type-form'));
}
$acl->allow(null, 'Contribution_Contribution', array('contribute', 'terms', 'thankyou'));
$acl->addResource('Contribution_Contributors');
$acl->allow(null, 'Contribution_Contributors');
$acl->addResource('Contribution_Items');
$acl->allow(null, 'Contribution_Items');
$acl->allow('guest', 'Items', 'showSelfNotPublic');
$acl->deny('guest', 'Contribution_Items');
$acl->deny(array('researcher', 'contributor'), 'Contribution_Items', 'view-anonymous');
$acl->addResource('Contribution_Types');
$acl->allow(array('super', 'admin'), 'Contribution_Types');
$acl->addResource('Contribution_Settings');
$acl->allow(array('super', 'admin'), 'Contribution_Settings');
}
/**
* Contribution define_routes hook
* Defines public-only routes that set the contribution controller as the
* only accessible one.
*/
public function hookDefineRoutes($args)
{
$router = $args['router'];
// Only apply custom routes on public theme.
// The wildcards on both routes make these routes always apply for the
// contribution controller.
// get the base path
$bp = get_option('contribution_page_path');
if ($bp) {
$router->addRoute('contributionCustom',
new Zend_Controller_Router_Route("$bp/:action/*",
array('module' => 'contribution',
'controller' => 'contribution',
'action' => 'contribute')));
} else {
$router->addRoute('contributionDefault',
new Zend_Controller_Router_Route('contribution/:action/*',
array('module' => 'contribution',
'controller' => 'contribution',
'action' => 'contribute')));
}
if (is_admin_theme()) {
$router->addRoute('contributionAdmin',
new Zend_Controller_Router_Route('contribution/:controller/:action/*',
array('module' => 'contribution',
'controller' => 'index',
'action' => 'index')));
}
}
public function filterApiResources($apiResources)
{
$apiResources['contributions'] = array(
'record_type' => 'ContributionContributedItem',
'actions' => array('get', 'index'),
//'index_params' => array('record_type', 'record_id')
);
$apiResources['contribution_types'] = array(
'record_type' => 'ContributionType',
'actions' => array('get', 'index')
);
$apiResources['contribution_type_elements'] = array(
'record_type' => 'ContributionTypeElement',
'actions' => array('get', 'index')
);
return $apiResources;
}
public function filterApiImportOmekaAdapters($adapters, $args)
{
if (strpos($args['endpointUri'], 'omeka.net') !== false) {
$contributedItemAdapter =
new ApiImport_ResponseAdapter_Omeka_GenericAdapter(null, $args['endpointUri'], 'ContributionContributedItem');
$contributedItemAdapter->setResourceProperties(array('item' => 'Item'));
$adapters['contributions'] = $contributedItemAdapter;
$contributionTypeAdapter =
new ApiImport_ResponseAdapter_Omeka_GenericAdapter(null, $args['endpointUri'], 'ContributionType');
$contributionTypeAdapter->setResourceProperties(array('item_type' => 'ItemType'));
$adapters['contribution_types'] = $contributionTypeAdapter;
$contributionTypeElementsAdapter =
new ApiImport_ResponseAdapter_Omeka_GenericAdapter(null, $args['endpointUri'], 'ContributionTypeElement');
$contributionTypeElementsAdapter->setResourceProperties(
array(
'element' => 'Element',
'type' => 'ContributionType'
)
);
$adapters['contribution_type_elements'] = $contributionTypeElementsAdapter;
} else {
$contributionContributorsAdapter =
new ApiImport_ResponseAdapter_OmekaNet_ContributorsAdapter(
null, $args['endpointUri'], 'User'
);
$adapters['contribution_contributors'] = $contributionContributorsAdapter;
$contributedItemAdapter =
new ApiImport_ResponseAdapter_OmekaNet_ContributedItemsAdapter(
null, $args['endpointUri'], 'ContributionContributedItem'
);
$adapters['contribution_contributed_items'] = $contributedItemAdapter;
$typesAdapter =
new ApiImport_ResponseAdapter_Omeka_GenericAdapter(null, $args['endpointUri'], 'ContributionType');
$typesAdapter->setResourceProperties(array('item_type' => 'ItemType'));
$adapters['contribution_types'] = $typesAdapter;
$typeElementsAdapter =
new ApiImport_ResponseAdapter_Omeka_GenericAdapter(null, $args['endpointUri'], 'ContributionTypeElement');
$typeElementsAdapter->setResourceProperties(
array('type' => 'ContributionType',
'element' => 'Element'
));
$adapters['contribution_type_elements'] = $typeElementsAdapter;
}
return $adapters;
}
/**
* Append a Contribution entry to the admin navigation.
*
* @param array $nav
* @return array
*/
public function filterAdminNavigationMain($nav)
{
$contributionCount = get_db()->getTable('ContributionContributedItems')->count();
if ($contributionCount > 0) {
$uri = url('contribution/items?sort_field=added&sort_dir=d');
$label = __('Contributed Items');
} else {
$uri = url('contribution/index');
$label = __('Contribution');
}
$nav[] = array(
'label' => $label,
'uri' => $uri,
'resource' => 'Contribution_Contribution',
'privilege' => 'browse'
);
return $nav;
}
/**
* Append a Contribution entry to the public navigation.
*
* @param array $nav
* @return array
*/
public function filterPublicNavigationMain($nav)
{
$nav[] = array(
'label' => __('Contribute an Item'),
'uri' => contribution_contribute_url(),
'visible' => true,
);
return $nav;
}
/**
* Append routes that render element text form input.
*
* @param array $routes
* @return array
*/
public function filterSimpleVocabRoutes($routes)
{
$routes[] = array('module' => 'contribution',
'controller' => 'contribution',
'actions' => array('type-form', 'contribute'));
return $routes;
}
public function filterItemSearchFilters($displayArray, $args)
{
$request_array = $args['request_array'];
if (isset($request_array['status'])) {
$displayArray['Status'] = $request_array['status'];
}
if (isset($request_array['contributor'])) {
$displayArray['Contributor'] = $this->_db->getTable('User')->find($request_array['contributor'])->name;
}
return $displayArray;
}
/**
* Append Contribution search selectors to the advanced search page.
*
* @return string HTML
*/
public function hookAdminItemsSearch()
{
$html = '<div class="field">';
$html .= '<div class="two columns alpha">';
$html .= get_view()->formLabel('contributed', __('Contribution Status'));
$html .= '</div>';
$html .= '<div class="inputs five columns omega">';
$html .= '<div class="input-block">';
$html .= get_view()->formSelect('contributed', null, null, array(
'' => __('Select Below'),
'1' => __('Only Contributed Items'),
'0' => __('Only Non-Contributed Items')
));
$html .= '</div></div></div>';
echo $html;
}
public function hookAdminItemsShowSidebar($args)
{
$htmlBase = $this->_adminBaseInfo($args);
if ($htmlBase) {
echo "<div class='panel'>";
echo "<h4>" . __("Contribution") . "</h4>";
echo $htmlBase;
echo "</div>";
}
}
public function hookAdminItemsBrowseDetailedEach($args)
{
echo $this->_adminBaseInfo($args);
}
/**
* Deal with Contribution-specific search terms.
*
* @param Omeka_Db_Select $select
* @param array $params
*/
public function hookItemsBrowseSql($args)
{
$select = $args['select'];
$params = $args['params'];
if (($request = Zend_Controller_Front::getInstance()->getRequest())) {
$db = get_db();
$contributed = $request->get('contributed');
if (isset($contributed)) {
if ($contributed === '1') {
$select->joinInner(
array('cci' => $db->ContributionContributedItem),
'cci.item_id = items.id',
array()
);
} else if ($contributed === '0') {
$select->where("items.id NOT IN (SELECT `item_id` FROM {$db->ContributionContributedItem})");
}
}
$contributor_id = $request->get('contributor_id');
if (is_numeric($contributor_id)) {
$select->joinInner(
array('cci' => $db->ContributionContributedItem),
'cci.item_id = items.id',
array('contributor_id')
);
$select->where('cci.contributor_id = ?', $contributor_id);
}
}
}
/**
* Create reasonable default entries for contribution types.
*/
private function _createDefaultContributionTypes()
{
$elementTable = $this->_db->getTable('Element');
$itemTypeTable = $this->_db->getTable('ItemType');
$textItemType = $itemTypeTable->findByName('Text');
if ($textItemType) {
$storyType = new ContributionType;
$storyType->item_type_id = $textItemType->id;
$storyType->display_name = 'Story';
$storyType->file_permissions = 'Allowed';
$storyType->save();
$textElement = new ContributionTypeElement;
$textElement->type_id = $storyType->id;
$dcTitleElement = $elementTable->findByElementSetNameAndElementName('Dublin Core', 'Title');
$textElement->element_id = $dcTitleElement->id;
$textElement->prompt = 'Title';
$textElement->order = 1;
$textElement->long_text = false;
$textElement->save();
$textElement = new ContributionTypeElement;
$textElement->type_id = $storyType->id;
$itemTypeMetadataTextElement = $elementTable->findByElementSetNameAndElementName('Item Type Metadata', 'Text');
$textElement->element_id = $itemTypeMetadataTextElement->id;
$textElement->prompt = 'Story Text';
$textElement->order = 2;
$textElement->long_text = true;
$textElement->save();
}
$imageItemType = $itemTypeTable->findByName('Still Image');
if ($imageItemType) {
$imageType = new ContributionType;
$imageType->item_type_id = 6;
$imageType->display_name = 'Image';
$imageType->file_permissions = 'Required';
$imageType->save();
$descriptionElement = new ContributionTypeElement;
$descriptionElement->type_id = $imageType->id;
$dcDescriptionElement = $elementTable->findByElementSetNameAndElementName('Dublin Core', 'Description');
$descriptionElement->element_id = $dcDescriptionElement->id;
$descriptionElement->prompt = 'Image Description';
$descriptionElement->order = 1;
$descriptionElement->long_text = true;
$descriptionElement->save();
}
}
public function hookBeforeSaveItem($args)
{
$item = $args['record'];
if ($item->exists()) {
//prevent admins from overriding the contributer's assertion of public vs private
$contributionItem = $this->_db->getTable('ContributionContributedItem')->findByItem($item);
if ($contributionItem) {
if (!$contributionItem->public && $item->public) {
$item->public = false;
Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->addMessage("Cannot override contributor's desire to leave contribution private", 'error');
}
}
}
}
public function hookAfterDeleteItem($args)
{
$item = $args['record'];
$contributionItem = $this->_db->getTable('ContributionContributedItem')->findByItem($item);
if ($contributionItem) {
$contributionItem->delete();
}
}
public function hookUserProfilesUserPage($args)
{
$user = $args['user'];
$contributionCount = $this->_db->getTable('ContributionContributedItem')->count(array('contributor' => $user->id));
if ($contributionCount !=0) {
echo "<a href='" . url('contribution/contributors/show/id/' . $user->id) . "'>Contributed Items ($contributionCount)";
}
}
public function filterItemCitation($cite,$args)
{
$item = $args['item'];
if (!$item) {
return $cite;
}
$contribItem = $this->_db->getTable('ContributionContributedItem')->findByItem($item);
if (!$contribItem) {
return $cite;
}
$title = metadata($item, array('Dublin Core', 'Title'));
$siteTitle = strip_formatting(option('site_title'));
$itemId = $item->id;
$accessDate = date('F j, Y');
$uri = html_escape(record_url($item, 'show', true));
if ($contribItem->anonymous) {
$cite = __("Anonymous, ");
} else {
$cite = $contribItem->Contributor->name . ", ";
}
$cite .= "“$title,” ";
if ($siteTitle) {
$cite .= "<em>$siteTitle</em>, ";
}
$cite .= "accessed $accessDate, ";
$cite .= "$uri.";
return $cite;
}
public function filterGuestUserLinks($nav)
{
$nav['Contribution'] = array(
'label' => 'My Contributions',
'uri' => contribution_contribute_url('my-contributions'),
);
return $nav;
}
public function filterGuestUserWidgets($widgets)
{
$user = current_user();
$widget = array('label' => __('My Contributions'));
$contributedItems = get_db()->getTable('ContributionContributedItem')->findBy(array('contributor' => $user->id), 5);
if ($contributedItems) {
$html = "<ul>";
foreach ($contributedItems as $contributedItem) {
$item = $contributedItem->Item;
$html .= sprintf("<li>%s</li>", link_to($item, 'show', metadata($item, array('Dublin Core', 'Title'))));
}
$html .= "</ul>";
$html .= sprintf('<a href="%s">%s</a>',
contribution_contribute_url('my-contributions'),
__('See all my contributions'));
}
else {
$html = '<p>' . __('No contribution yet, or removed contributions.') . '</p>';
}
$widget['content'] = $html;
$widgets[] = $widget;
return $widgets;
}
private function _adminBaseInfo($args)
{
$item = $args['item'];
$contributedItem = $this->_db->getTable('ContributionContributedItem')->findByItem($item);
if ($contributedItem) {
$html = '';
$name = $contributedItem->getContributor()->name;
$html .= "<p><strong>" . __("Contributed by:") . "</strong><span class='contribution-contributor'> $name</span></p>";
$publicMessage = '';
if (is_allowed($item, 'edit')) {
if ($contributedItem->public) {
$publicMessage = __("This item can be made public.");
} else {
$publicMessage = __("This item cannot be made public.");
}
$html .= "<p><strong>$publicMessage</strong></p>";
}
return $html;
}
}
private function _contributorsToGuestUsers($contributorsData)
{
$map = array();
foreach ($contributorsData as $index => $contributor) {
$user = new User();
$user->email = $contributor['email'];
$user->name = $contributor['name'];
//make sure username is 6 chars long and unique
//base it on the email to lessen character restriction problems
$explodedEmail = explode('@', $user->email);
$username = $explodedEmail[0];
$username = str_replace('.', '', $username);
$user->username = $username;
$user->active = true;
$user->role = 'guest';
$user->setPassword($user->email);
$user->save();
$map[$contributor['id']] = $user->id;
$activation = UsersActivations::factory($user);
$activation->save();
release_object($user);
release_object($activation);
}
return $map;
}
public function _mapOwners($contribItemData, $map)
{
$itemTable = $this->_db->getTable('Item');
foreach ($contribItemData as $contribItem) {
$item = $itemTable->find($contribItem['item_id']);
$item->owner_id = $map[$contribItem['contributor_id']];
$item->save();
release_object($item);
}
}
public function getOptions()
{
return $this->_options;
}
/**
* Remove the form controls
*
* @param array $components
* @param array $args
* @return NULL
*/
public function elementInputFilter($components, $args)
{
$view = get_view();
$element = $args['element'];
$type = $view->type;
$contributionElement = $this->_db->getTable('ContributionTypeElement')->findByElementAndType($element, $type);
if ($contributionElement->long_text == 0) {
$components['input'] = $view->formText($args['input_name_stem'] . '[text]', $args['value']);
}
$components['form_controls'] = null;
$components['html_checkbox'] = null;
return $components;
}
/**
* Replace the prompt and remove the add input button
* @param array $components
* @param array $args
*/
public function elementFormFilter($components, $args)
{
$element = $args['element'];
$view = get_view();
$type = $view->type;
$contributionElement = $this->_db->getTable('ContributionTypeElement')->findByElementAndType($element, $type);
$prompt = $contributionElement->prompt;
$components['label'] = '<label>' . $prompt . '</label>';
$components['add_input'] = null;
return $components;
}
}