summaryrefslogtreecommitdiff
path: root/plugins/filter.attachment.php
blob: 2aa0b4c6e0c5a80dd19ee2a3229fa3ceca6450d5 (plain)
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
<?php
namespace Bitweaver\Liberty;

use Bitweaver\BitBase;

/**
 * @version  $Header$
 * @package  liberty
 * @subpackage plugins_filter
 */

/**
 * definitions ( guid character limit is 16 chars )
 */
define( 'PLUGIN_GUID_FILTERATTACHMENT', 'filterattachment' );

global $gLibertySystem;

$pluginParams = [
	// plugin title
	'title'                    => 'Attachment Tracker',
	// help page on bitweaver org that explains this plugin
	'help_page'                => 'Attachment Tracker Filter',
	// brief description of the plugin
	'description'              => 'Track attachment usage in content pages.',
	// should this plugin be active or not when loaded for the first time
	'auto_activate'            => false,
	// type of plugin
	'plugin_type'              => FILTER_PLUGIN,
	// url to page with options for this plugin
	//'plugin_settings_url'      => LIBERTY_PKG_URL.'admin/filter_attachment.php',

	// various filter functions and when they are called
	'requirement_function'     => '\Bitweaver\Liberty\attachment_filter_reqirements',
	'prestore_function'        => '\Bitweaver\Liberty\attachment_filter',
	'expunge_function'         => 'attachment_filter_expunge',
];
$gLibertySystem->registerPlugin( PLUGIN_GUID_FILTERATTACHMENT, $pluginParams );

/**
 * To use this plugin you need to create a table in your database such as:
 *
 * CREATE TABLE liberty_attachment_usage (
 *     content_id INT NOT null,
 *     attachment_id INT NOT null
 * );
 * ALTER TABLE liberty_attachment_usage ADD CONSTRAINT liberty_att_usage_content_ref FOREIGN KEY (content_id) REFERENCES liberty_content(content_id);
 * ALTER TABLE liberty_attachment_usage ADD CONSTRAINT liberty_att_usage_att_ref FOREIGN KEY (attachment_id) REFERENCES liberty_attachments(attachment_id);
 */

/**
 * attachment_filter_reqirements 
 * 
 * @param boolean $pInstall 
 * @access private
 * @return array information hash
 */
function attachment_filter_reqirements( $pInstall = false ) {
	global $gLibertySystem;
	$ret = [];
	if( $pInstall ) {
		$ret['schema'] = [
			'tables' => [
				'liberty_attachment_usage' => "
					content_id I4 NOTNULL,
					attachment_id I4 NOTNULL
					CONSTRAINT '
						, CONSTRAINT `lib_att_usage_content_ref` FOREIGN KEY (`content_id`) REFERENCES `".BIT_DB_PREFIX."liberty_content`( `content_id` )
						, CONSTRAINT `lib_att_usage_attachment_ref` FOREIGN KEY (`attachment_id`) REFERENCES `".BIT_DB_PREFIX."liberty_attachments`( `attachment_id` )
					'
				",
			],
			'indexes' => [
				'lib_att_usage_content_idx' => [ 'table' => 'liberty_attachment_usage', 'cols' => 'content_id', 'opts' => null ],
				'lib_att_usage_attachment_idx' => [ 'table' => 'liberty_attachment_usage', 'cols' => 'attachment_id', 'opts' => null ],
			],
//			'sequences' => [
//				'liberty_attachment_usage_id_seq' => [ 'start' => 1 ],
//			],
		];
	}

//	$ret['output']['important'][] = "This plugin will install a new table in your database as soon as you enable it.<br />If you don't want to use this plugin anymore, we recommend that you remove the 'liberty_attachment_usage' table from your database after you have disabled the plugin. You need to do this manually.";
	return $ret;
}

/**
 * attachment_filter will find out what attachments are used where.
 * 
 * @param string $pString 
 * @param array $pFilterHash 
 * @access public
 * @return void
 */
function attachment_filter( &$pString, &$pFilterHash ) {
	global $gLibertySystem, $gBitSystem;
	if( $gLibertySystem->isPluginActive( 'dataattachment' ) && BitBase::verifyId( $pFilterHash['content_id'] ?? 0 )) {
		// make sure we have a blank slate in the db since we might have removed some {attachment}s in the content
		attachment_filter_expunge( null, $pFilterHash );

		preg_match_all( "#{(attachment|image|file)[^}]*\bid\s?=[\s'\"]*(\d+)#i", $pString, $matches );
		if( $count = count( $matches[0] )) {
			for( $i = 0; $i < $count; $i++ ) {
				// if we included this file using {image} or {file} we get the correct attachment_id of the file
				$whereSql = "WHERE `" . ( $matches[1][$i] != 'attachment' ? "attachment_id" : "content_id" ) . "` = ?";
				$attachment_id = $gBitSystem->mDb->getOne( "SELECT `attachment_id` FROM `".BIT_DB_PREFIX."liberty_attachments` $whereSql", [ $matches[2][$i] ] );

				if( BitBase::verifyId( $attachment_id )) {
					$store = [
						'content_id'    => $pFilterHash['content_id'],
						'attachment_id' => $attachment_id,
					];
					$gBitSystem->mDb->associateInsert( BIT_DB_PREFIX."liberty_attachment_usage", $store );
				}
			}
		}
	}
}

/**
 * attachment_filter_expunge 
 * 
 * @param string $pString 
 * @param array $pFilterHash 
 * @access public
 * @return void
 */
function attachment_filter_expunge( $pString, &$pFilterHash ) {
	global $gBitSystem;
	if( BitBase::verifyId( $pFilterHash['content_id'] )) {
		$gBitSystem->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."liberty_attachment_usage` WHERE `content_id` = ?", [ $pFilterHash['content_id'] ] );
	}
}

/**
 * attachment_filter_get_usage this function will return all content that uses a given attachment in their content
 * 
 * @param array $pAttachmentId Attachment ID of the attachment we want usage stats for
 * @access public
 * @return array of content that uses the attachment
 */
function attachment_filter_get_usage( $pAttachmentId ) {
	global $gBitSystem;
	$sql = "
		SELECT lc.`title`,lc.`content_id`
		FROM `".BIT_DB_PREFIX."liberty_attachment_usage` lau
			INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lau.`content_id` = lc.`content_id` )
		WHERE lau.`attachment_id` = ?";
	return $gBitSystem->mDb->getAll( $sql, [ $pAttachmentId ] );
}