vcategories.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Thomas Tanghus
  6. * @copyright 2012 Thomas Tanghus <thomas@tanghus.net>
  7. * @copyright 2012 Bart Visscher bartv@thisnet.nl
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /**
  24. * Class for easy access to categories in VCARD, VEVENT, VTODO and VJOURNAL.
  25. * A Category can be e.g. 'Family', 'Work', 'Chore', 'Special Occation' or
  26. * anything else that is either parsed from a vobject or that the user chooses
  27. * to add.
  28. * Category names are not case-sensitive, but will be saved with the case they
  29. * are entered in. If a user already has a category 'family' for an app, and
  30. * tries to add a category named 'Family' it will be silently ignored.
  31. * NOTE: There is a limitation in that the the configvalue field in the
  32. * preferences table is a varchar(255).
  33. */
  34. class OC_VCategories {
  35. const PREF_CATEGORIES_LABEL = 'extra_categories';
  36. /**
  37. * Categories
  38. */
  39. private $categories = array();
  40. private $app = null;
  41. private $user = null;
  42. /**
  43. * @brief Constructor.
  44. * @param $app The application identifier e.g. 'contacts' or 'calendar'.
  45. * @param $user The user whos data the object will operate on. This
  46. * parameter should normally be omitted but to make an app able to
  47. * update categories for all users it is made possible to provide it.
  48. * @param $defcategories An array of default categories to be used if none is stored.
  49. */
  50. public function __construct($app, $user=null, $defcategories=array()) {
  51. $this->app = $app;
  52. $this->user = is_null($user) ? OC_User::getUser() : $user;
  53. $categories = trim(OC_Preferences::getValue($this->user, $app, self::PREF_CATEGORIES_LABEL, ''));
  54. if ($categories) {
  55. $categories = @unserialize($categories);
  56. }
  57. $this->categories = is_array($categories) ? $categories : $defcategories;
  58. }
  59. /**
  60. * @brief Get the categories for a specific user.
  61. * @returns array containing the categories as strings.
  62. */
  63. public function categories() {
  64. //OC_Log::write('core','OC_VCategories::categories: '.print_r($this->categories, true), OC_Log::DEBUG);
  65. if(!$this->categories) {
  66. return array();
  67. }
  68. usort($this->categories, 'strnatcasecmp'); // usort to also renumber the keys
  69. return $this->categories;
  70. }
  71. /**
  72. * @brief Checks whether a category is already saved.
  73. * @param $name The name to check for.
  74. * @returns bool
  75. */
  76. public function hasCategory($name) {
  77. return $this->in_arrayi($name, $this->categories);
  78. }
  79. /**
  80. * @brief Add a new category name.
  81. * @param $names A string with a name or an array of strings containing
  82. * the name(s) of the categor(y|ies) to add.
  83. * @param $sync bool When true, save the categories
  84. * @returns bool Returns false on error.
  85. */
  86. public function add($names, $sync=false) {
  87. if(!is_array($names)) {
  88. $names = array($names);
  89. }
  90. $names = array_map('trim', $names);
  91. $newones = array();
  92. foreach($names as $name) {
  93. if(($this->in_arrayi($name, $this->categories) == false) && $name != '') {
  94. $newones[] = $name;
  95. }
  96. }
  97. if(count($newones) > 0) {
  98. $this->categories = array_merge($this->categories, $newones);
  99. if($sync === true) {
  100. $this->save();
  101. }
  102. }
  103. return true;
  104. }
  105. /**
  106. * @brief Extracts categories from a vobject and add the ones not already present.
  107. * @param $vobject The instance of OC_VObject to load the categories from.
  108. */
  109. public function loadFromVObject($vobject, $sync=false) {
  110. $this->add($vobject->getAsArray('CATEGORIES'), $sync);
  111. }
  112. /**
  113. * @brief Reset saved categories and rescan supplied vobjects for categories.
  114. * @param $objects An array of vobjects (as text).
  115. * To get the object array, do something like:
  116. * // For Addressbook:
  117. * $categories = new OC_VCategories('contacts');
  118. * $stmt = OC_DB::prepare( 'SELECT `carddata` FROM `*PREFIX*contacts_cards`' );
  119. * $result = $stmt->execute();
  120. * $objects = array();
  121. * if(!is_null($result)) {
  122. * while( $row = $result->fetchRow()) {
  123. * $objects[] = $row['carddata'];
  124. * }
  125. * }
  126. * $categories->rescan($objects);
  127. */
  128. public function rescan($objects, $sync=true, $reset=true) {
  129. if($reset === true) {
  130. $this->categories = array();
  131. }
  132. foreach($objects as $object) {
  133. //OC_Log::write('core','OC_VCategories::rescan: '.substr($object, 0, 100).'(...)', OC_Log::DEBUG);
  134. $vobject = OC_VObject::parse($object);
  135. if(!is_null($vobject)) {
  136. $this->loadFromVObject($vobject, $sync);
  137. } else {
  138. OC_Log::write('core','OC_VCategories::rescan, unable to parse. ID: '.', '.substr($object, 0, 100).'(...)', OC_Log::DEBUG);
  139. }
  140. }
  141. $this->save();
  142. }
  143. /**
  144. * @brief Save the list with categories
  145. */
  146. private function save() {
  147. if(is_array($this->categories)) {
  148. usort($this->categories, 'strnatcasecmp'); // usort to also renumber the keys
  149. $escaped_categories = serialize($this->categories);
  150. OC_Preferences::setValue($this->user, $this->app, self::PREF_CATEGORIES_LABEL, $escaped_categories);
  151. OC_Log::write('core','OC_VCategories::save: '.print_r($this->categories, true), OC_Log::DEBUG);
  152. } else {
  153. OC_Log::write('core','OC_VCategories::save: $this->categories is not an array! '.print_r($this->categories, true), OC_Log::ERROR);
  154. }
  155. }
  156. /**
  157. * @brief Delete categories from the db and from all the vobject supplied
  158. * @param $names An array of categories to delete
  159. * @param $objects An array of arrays with [id,vobject] (as text) pairs suitable for updating the apps object table.
  160. */
  161. public function delete($names, array &$objects=null) {
  162. if(!is_array($names)) {
  163. $names = array($names);
  164. }
  165. OC_Log::write('core','OC_VCategories::delete, before: '.print_r($this->categories, true), OC_Log::DEBUG);
  166. foreach($names as $name) {
  167. OC_Log::write('core','OC_VCategories::delete: '.$name, OC_Log::DEBUG);
  168. if($this->hasCategory($name)) {
  169. //OC_Log::write('core','OC_VCategories::delete: '.$name.' got it', OC_Log::DEBUG);
  170. unset($this->categories[$this->array_searchi($name, $this->categories)]);
  171. }
  172. }
  173. $this->save();
  174. OC_Log::write('core','OC_VCategories::delete, after: '.print_r($this->categories, true), OC_Log::DEBUG);
  175. if(!is_null($objects)) {
  176. foreach($objects as $key=>&$value) {
  177. $vobject = OC_VObject::parse($value[1]);
  178. if(!is_null($vobject)) {
  179. $categories = $vobject->getAsArray('CATEGORIES');
  180. //OC_Log::write('core','OC_VCategories::delete, before: '.$key.': '.print_r($categories, true), OC_Log::DEBUG);
  181. foreach($names as $name) {
  182. $idx = $this->array_searchi($name, $categories);
  183. //OC_Log::write('core','OC_VCategories::delete, loop: '.$name.', '.print_r($idx, true), OC_Log::DEBUG);
  184. if($idx !== false) {
  185. OC_Log::write('core','OC_VCategories::delete, unsetting: '.$categories[$this->array_searchi($name, $categories)], OC_Log::DEBUG);
  186. unset($categories[$this->array_searchi($name, $categories)]);
  187. //unset($categories[$idx]);
  188. }
  189. }
  190. //OC_Log::write('core','OC_VCategories::delete, after: '.$key.': '.print_r($categories, true), OC_Log::DEBUG);
  191. $vobject->setString('CATEGORIES', implode(',', $categories));
  192. $value[1] = $vobject->serialize();
  193. $objects[$key] = $value;
  194. } else {
  195. OC_Log::write('core','OC_VCategories::delete, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 50).'(...)', OC_Log::DEBUG);
  196. }
  197. }
  198. }
  199. }
  200. // case-insensitive in_array
  201. private function in_arrayi($needle, $haystack) {
  202. if(!is_array($haystack)) {
  203. return false;
  204. }
  205. return in_array(strtolower($needle), array_map('strtolower', $haystack));
  206. }
  207. // case-insensitive array_search
  208. private function array_searchi($needle, $haystack) {
  209. if(!is_array($haystack)) {
  210. return false;
  211. }
  212. return array_search(strtolower($needle),array_map('strtolower',$haystack));
  213. }
  214. }