Paperyard
Public Member Functions | List of all members
pdfSorter Class Reference

Sorts thru PDF documents and puts them into corresponding folders etc. More...

Inheritance diagram for pdfSorter:
ppyrd

Public Member Functions

 __construct ($pdf, $db)
 constructor More...
 
 splitUpFilename ()
 function gets from file name the information what the date, company and subject is. More...
 
 checkRules ()
 checks rules More...
 
 run ()
 runs the main process More...
 
- Public Member Functions inherited from ppyrd
 __construct ($db)
 constructor More...
 
 checkCliVsWebserver ()
 
 output ($string, $debug=0)
 outputs string More...
 

Additional Inherited Members

- Public Attributes inherited from ppyrd
 $db
 

Detailed Description

Sorts thru PDF documents and puts them into corresponding folders etc.

Parameters
none
Returns
none

Definition at line 11 of file ppyrd.sorter.php.

Constructor & Destructor Documentation

pdfSorter::__construct (   $pdf,
  $db 
)

constructor

Parameters
string$pdffile name to be processed
string$dbdatabase handler
Returns
none

Definition at line 19 of file ppyrd.sorter.php.

20  {
21  $this->pdf = $pdf;
22 
23  // creating db handler to talk to DB
24  $this->db=$db;
25  }

Member Function Documentation

pdfSorter::checkRules ( )

checks rules

Definition at line 90 of file ppyrd.sorter.php.

91  {
92  $rules = $this->db->getActiveArchiveRules();
93  while ($row = $rules->fetchArray()) {
94  // we have a rule match if the company found matches the specified string
95  // * is the wild card like in file names
96  $match = fnmatch($row['company'], $this->company)
97  && fnmatch($row['subject'], $this->subject)
98  && fnmatch($row['recipient'], $this->recipient)
99  && fnmatch($row['tags'], $this->tags);
100 
101  // if everything matched - go ahead
102  if ($match) {
103  // processing the folder to which document shall be moved
104  $toFolder = $row['toFolder'];
105 
106  // in case the [year] variable has been used etc.
107  $toFolder = str_replace('[year]', $this->year, $toFolder);
108  $toFolder = str_replace('[month]', $this->month, $toFolder);
109  $toFolder = str_replace('[day]', $this->day, $toFolder);
110  $toFolder = str_replace('[recipient]', $this->recipient, $toFolder);
111 
112  // adding a trailing slash in case none existed
113  $toFolder = rtrim($toFolder, '/') . '/';
114 
115  // create folders in case required
116  exec("mkdir -p '$toFolder'");
117 
118  // move the file to destination folder
119  exec('mv --backup=numbered "' . $this->pdf . '" "' . $toFolder . $this->pdf . '"');
120 
121  $this->db->writeLog($this->pdf, $this->pdf, "", "Moved file to: " . $toFolder);
122  }
123  }
124  }
pdfSorter::run ( )

runs the main process

Definition at line 129 of file ppyrd.sorter.php.

130  {
131 
132  // process the file name first
133  $this->splitUpFilename();
134 
135  // then see if there is any rule to process
136  $this->checkRules();
137  }
checkRules()
checks rules
splitUpFilename()
function gets from file name the information what the date, company and subject is.
pdfSorter::splitUpFilename ( )

function gets from file name the information what the date, company and subject is.

Definition at line 30 of file ppyrd.sorter.php.

31  {
32  $this->output("working on: " . $this->pdf);
33 
34  // getting the file name template - needed to see what part (date, company, subject) is at which part of the string
35  $newFilenameStructure=$this->db->getConfigValue('newFilenameStructure');
36 
37  // getting things via regex
38  // removing closing parts of the brackets as they are not needed in this case
39  $unwanted = array(')',']');
40  $separators = "/ - | \(| \[| \-\- /";
41  $templateName = str_replace($unwanted, '', $newFilenameStructure);
42 
43  // splitting up template name into parts
44  $templateParts = array_flip(preg_split($separators, $templateName));
45 
46  // now doing the same stuff with the actual file
47  $tmpName = str_replace($unwanted, '', $this->pdf);
48  $filenameParts = preg_split($separators, $tmpName);
49 
50 
51 
52  // separating the file name into its parts
53  $parts = explode(" - ", $this->pdf);
54 
55  // date is 1st
56  $this->date = $filenameParts[$templateParts['ddatum']];
57  $this->year = date('Y',strtotime($this->date));
58  $this->month = date('m',strtotime($this->date));
59  $this->day = date('d',strtotime($this->date));
60 
61  // company etc.
62  $this->company = $filenameParts[$templateParts['ffirma']];
63  $this->recipient = $filenameParts[$templateParts['wwer']];
64  $this->subject = $filenameParts[$templateParts['bbetreff']];
65  $this->amount = $filenameParts[$templateParts['bbetrag']];
66 
67 
68 
69  // getting all tags @todo - needs to be least greedy ...
70  preg_match_all('/\[(\d|\w)*\]/',$this->pdf,$tags);
71  $this->tags = implode($tags[0]);
72 
73  //
74  $this->output( "date: " . $this->date);
75  $this->output( "year: " . $this->year);
76  $this->output( "month: " . $this->month);
77  $this->output( "day: " . $this->day);
78 
79  $this->output( "company: " . $this->company);
80  $this->output( "recipient: " . $this->recipient);
81  $this->output( "subject: " . $this->subject);
82  $this->output( "amount: " . $this->amount);
83  $this->output( "tags: " . $this->tags);
84 
85  }
output($string, $debug=0)
outputs string
Definition: ppyrd.base.php:78

The documentation for this class was generated from the following file: