Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Dcp_Core_vidExtractor.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp\Core\vidExtractor;
8 
9 class Exception extends \Dcp\Exception
10 {
11 }
12 /**
13  * Class vidExtractor
14  *
15  * Extract VIDs from documents or families (suitable for updating and maintaining the "docvaultindex" table)
16  *
17  * @package Dcp\Core
18  */
20 {
21  /**
22  * Get list of distinct VIDs (files vault identifier) from a "raw" document (i.e. a row from a SQL query resultset)
23  *
24  * @param array $raw Raw document
25  * @param array $fileAttrIdList List of attribute's name of type 'file' (if null, the list will be dynamically created from $raw['id'])
26  * @return array
27  * @throws Exception
28  */
29  public static function getVidsFromRawDoc($raw, $fileAttrIdList = null)
30  {
31  if (!is_array($raw)) {
32  throw new Exception('VIDEXTRACTOR0001', gettype($raw));
33  }
34  if ($fileAttrIdList === null) {
35  if (!isset($raw['id'])) {
36  throw new Exception('VIDEXTRACTOR0002');
37  }
38  $fileAttrIdList = array();
39  $doc = new_Doc('', $raw['id']);
40  if (!is_object($doc)) {
41  throw new Exception('VIDEXTRACTOR0003', $raw['id']);
42  }
43  $fileAttrList = $doc->getFileAttributes();
44  foreach ($fileAttrList as $attrId => $attr) {
45  $fileAttrIdList[] = $attrId;
46  }
47  }
48  $vidList = array();
49  /* icon */
50  if (!empty($raw['icon']) && ($vid = self::parseVid($raw['icon'])) !== false) {
51  $vidList[] = $vid;
52  }
53  /* file attributes */
54  foreach ($fileAttrIdList as $attr) {
55  if (!isset($raw[$attr])) {
56  continue;
57  }
58  $values = $raw[$attr];
59  $values = preg_split('/\n/', str_replace('<BR>', "\n", $values));
60  foreach ($values as $value) {
61  if (($vid = self::parseVid($value)) !== false) {
62  $vidList[] = $vid;
63  }
64  }
65  }
66  return $vidList;
67  }
68  /**
69  * Extract VIDs from a family object
70  *
71  * @param \DocFam $docfam
72  * @return array
73  */
74  public static function getVidsFromDocFam(\DocFam & $docfam)
75  {
76  $values = array();
77  /*
78  * Track files from docfam.param and docfam.defval
79  */
80  foreach (array(
81  $docfam->getOwnParams() ,
82  $docfam->getOwnDefValues()
83  ) as $list) {
84  foreach ($list as $aid => $value) {
85  if (($oattr = $docfam->getAttribute($aid)) === false) {
86  $docfam->log->warning(\ErrorCode::getError('VIDEXTRACTOR0004', $aid, $docfam->name));
87  continue;
88  }
89  if ($oattr->type !== 'file' && $oattr->type !== 'image') {
90  continue;
91  }
92  if ($oattr->inArray()) {
93  $values = array_merge($values, $docfam->rawValueToArray($value));
94  } else {
95  $values[] = $value;
96  }
97  }
98  }
99  /*
100  * Track files from icon
101  */
102  if (isset($docfam->icon)) {
103  $values[] = $docfam->icon;
104  }
105  /*
106  * Extract vids from values
107  */
108  $vids = array();
109  foreach ($values as $value) {
110  if (($vid = self::parseVid($value)) !== false) {
111  $vids[$vid] = $vid;
112  }
113  }
114  return $vids;
115  }
116  /**
117  * Extract VIDs from a document object
118  *
119  * @param \Doc $doc
120  * @return array
121  */
122  public static function getVidsFromDoc(\Doc & $doc)
123  {
124  $vids = array();
125  $fa = $doc->GetFileAttributes();
126  /* Track files from attributes */
127  foreach ($fa as $aid => $oattr) {
128  if ($oattr->inArray()) {
129  $ta = $doc->getMultipleRawValues($aid);
130  } else {
131  $ta = array(
132  $doc->getRawValue($aid)
133  );
134  }
135  foreach ($ta as $k => $v) {
136  if (($vid = self::parseVid($v)) !== false) {
137  $vids[$vid] = $vid;
138  }
139  }
140  }
141  /* Track file from icon */
142  if (isset($doc->icon)) {
143  if (($vid = self::parseVid($doc->icon)) !== false) {
144  $vids[$vid] = $vid;
145  }
146  }
147  return $vids;
148  }
149  /**
150  * Parse and extract VID from string
151  *
152  * @param $vid
153  * @return bool
154  */
155  public static function parseVid($vid)
156  {
157  if (!preg_match(PREGEXPFILE, $vid, $m)) {
158  return false;
159  }
160  if (!isset($m[2])) {
161  return false;
162  }
163  if ($m[2] === '') {
164  return false;
165  }
166  return $m[2];
167  }
168 }
& getAttribute($idAttr, &$oa=null, $useMask=true)
Definition: Class.Doc.php:2152
static rawValueToArray($v)
Definition: Class.Doc.php:6228
Exception class use exceptionCode to identifiy correctly exception.
Definition: exceptions.php:19
const PREGEXPFILE
Definition: Class.Doc.php:54
static getError($code, $args=null)
Definition: ErrorCode.php:27
static getVidsFromRawDoc($raw, $fileAttrIdList=null)
getMultipleRawValues($idAttr, $def="", $index=-1)
Definition: Class.Doc.php:3240
getOwnParams()
getOwnDefValues()
new_Doc($dbaccess, $id= '', $latest=false)
getRawValue($idAttr, $def="")
Definition: Class.Doc.php:3117
$value
← centre documentaire © anakeen